在一个类child里面 void show()是公有函数,它里面写到me.show(),这个me是类的一位成员吗
时间: 2023-06-12 19:03:26 浏览: 44
是的,me很有可能是一个类的成员变量,指向当前对象的指针。在C++中,每个对象都有一个指向自己的指针,即this指针,通过this指针可以访问对象的成员变量和成员函数。在类的成员函数中,可以直接使用成员变量和成员函数的名称来访问它们,但是如果成员变量和成员函数的名称和参数名称相同,就需要使用this指针来区分它们。因此,me.show()可以理解为this->show(),即调用当前对象的show()函数。
相关问题
回答不对,是已知父级,子集的集合,在一个表里面怎么写递归函数
非常抱歉,我误解了您的问题。如果您已经有了父级与子级的集合,并且这些数据存储在同一个表中,您可以通过递归函数来查询子孙级别的数据。
假设您的表名为`table_name`,包含以下字段:`id`、`name`、`parent_id`,其中`id`为主键,`parent_id`为外键,表示当前记录的父级id。下面是一个示例代码,可以获取指定父级下的所有子孙级数据:
```python
import mysql.connector
def get_children(parent_id, cursor):
query = "SELECT * FROM table_name WHERE parent_id = %s"
cursor.execute(query, (parent_id, ))
children = []
for row in cursor.fetchall():
child = dict(row)
child['children'] = get_children(child['id'], cursor)
children.append(child)
return children
# 使用示例
cnx = mysql.connector.connect(user='your_username', password='your_password',
host='your_host', database='your_database')
cursor = cnx.cursor(dictionary=True)
result = get_children(parent_id, cursor)
```
在这个函数中,我们首先定义了一个`query`变量,用于查询所有`parent_id`等于指定父级id的记录。然后遍历查询结果,将每个记录转换成字典类型,并将其加入到`children`列表中。同时,递归调用`get_children`函数获取该子级的所有子孙级数据,并将其作为该子级的`children`属性。
最后,返回`children`列表即可。这样,递归函数就可以通过不断调用自身来获取所有子孙级别的数据了。请注意,这里的示例代码使用了MySQL数据库,如果您使用的是其他数据库,需要对代码进行相应的调整。
在sv中class里面子类继承的时候,要写一个新的构造函数,super.new怎么用
在SV语言中,当一个类的子类继承父类时,可以使用`super.new`来调用父类的构造函数。这样可以确保在子类中初始化父类的成员变量和执行父类的构造函数逻辑。
以下是一个示例代码,展示了如何在SV中使用`super.new`来调用父类的构造函数:
```systemverilog
class Parent;
int value;
function new(int init_value);
value = init_value;
endfunction
endclass
class Child extends Parent;
int child_value;
function new(int init_value, int child_init_value);
super.new(init_value); // 调用父类的构造函数
child_value = child_init_value;
endfunction
endclass
```
在上面的示例中,`Child`类继承了`Parent`类。在`Child`类的构造函数中,使用`super.new(init_value)`来调用父类`Parent`的构造函数,并传递参数`init_value`进行初始化。这样可以确保在创建`Child`对象时,先调用父类的构造函数进行必要的初始化操作,然后再执行子类自己的构造函数逻辑。
希望这个例子可以帮助你理解如何在SV中使用`super.new`来调用父类的构造函数。如果还有其他问题,请随时提问!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)