class MyClass { private: QCustomPlot *CustomPlot_Magnitude; };MyClass::MyClass() { CustomPlot_Magnitude = new QCustomPlot; }C++Qt不支持这种书写
时间: 2024-03-09 12:51:04 浏览: 20
我很抱歉,我的回答可能有些错误。在C++/Qt中,类的成员变量必须在构造函数的初始化列表中初始化,而不能在构造函数的主体中初始化。因此,正确的代码应该是这样的:
```
class MyClass {
private:
QCustomPlot *CustomPlot_Magnitude;
public:
MyClass(); // 构造函数声明
};
MyClass::MyClass() : CustomPlot_Magnitude(new QCustomPlot) {
// 在构造函数的初始化列表中初始化CustomPlot_Magnitude
}
```
这样就可以将CustomPlot_Magnitude定义为类的成员变量,并在构造函数中正确地初始化它了。
相关问题
QCustomPlot *CustomPlot_Magnitude = new QCustomPlot定义后如何定义为成员变量
如果你希望将CustomPlot_Magnitude定义为类的成员变量,可以将其放在类的头文件中,并将其定义为私有成员变量。例如,你可以在类的头文件中添加以下代码:
```
class MyClass {
private:
QCustomPlot *CustomPlot_Magnitude;
};
```
然后,在类的实现文件中,你可以使用以下代码来初始化CustomPlot_Magnitude:
```
MyClass::MyClass() {
CustomPlot_Magnitude = new QCustomPlot;
}
```
这样,CustomPlot_Magnitude就可以在该类的其他成员函数中使用了。请注意,如果你希望在其他函数中访问CustomPlot_Magnitude,可以提供一个公共接口(例如getCustomPlot_Magnitude()函数)来返回CustomPlot_Magnitude指针。
class MyClass: def __new__(cls, *args, **kwargs): instance = super.__new__(cls) return instance def __init__(self, *args, **kwargs): pass这串数据有什么问题?
这段代码在 MyClass 类的 `__new__` 方法中使用了 `super`,但是没有指定调用 `super` 的具体父类。因此,这会导致 `super` 调用的是 `object` 类的 `__new__` 方法,而不是 MyClass 的父类的 `__new__` 方法。
为了解决这个问题,应该将 `super` 调用中的 `cls` 参数改为 `MyClass`,以明确调用 MyClass 的父类的 `__new__` 方法。此外,`__new__` 方法应该返回实例对象,而不是 `super.__new__(cls)`,应该是 `super().__new__(cls)`。
因此,正确的代码应该是:
```
class MyClass:
def __new__(cls, *args, **kwargs):
instance = super(MyClass, cls).__new__(cls)
return instance
def __init__(self, *args, **kwargs):
pass
```
这样,MyClass 的 `__new__` 方法就会正确地调用其父类的 `__new__` 方法,并返回实例对象。
相关推荐
![](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)