为什么我在声明 class 对象时不能初始化它?
Why I can't initialize a class object when declaring it?
我为特定的 class 重载了赋值运算符,之后我很快发现了一个问题。
在 class 对象的声明过程中,如果我用另一个已经存在的对象初始化它,
objectType object1;
objectType object2 = object1;
程序将终止并显示 program.exe 在 运行 期间停止工作的消息。
但是,如果我将声明和初始化步骤分开,
objectType object1, object2;
object2 = object1;
它将正常工作。
如果我们可以用简单的数据类型做到这一点,
int x = 6;
int y = x;
为什么我们不能对 class 对象执行此操作?我希望我的问题很清楚,我已经用相同的 .exe 停止工作结果在不同的计算机上进行了测试。
已编辑:
这是我的代码。 class 本身就是一个堆栈。
#include <iostream>
using namespace std;
struct nodeType
{
int info;
nodeType *link;
};
class objectType
{
public:
const objectType& operator=(const objectType& otherObject)
{
if(this != &otherObject)
{
copyObject(otherObject);
}
return *this;
}
void initialize() //Initialize the stack
{
nodeType *temp;
while(stackTop != nullptr)
{
temp = stackTop;
stackTop = stackTop->link;
delete temp;
}
}
objectType(const objectType& otherObject) //Copy constructor
{
copyObject(otherObject);
}
objectType() //Constructor
{
stackTop = nullptr;
}
~objectType() //Destructor
{
initialize();
}
private:
nodeType* stackTop;
//Copy function to implement copy constructor and overload assignment operator
void copyObject(const objectType& otherObject)
{
initialize();
if(otherObject.stackTop != nullptr)
{
nodeType *current, *last, *newNode;
current = otherObject.stackTop;
stackTop = new nodeType;
stackTop->info = current->info;
stackTop->link = nullptr;
last = stackTop;
current = current->link;
while(current != nullptr)
{
newNode = new nodeType;
newNode->info = current->info;
newNode->link = nullptr;
last->link = newNode;
last = newNode;
current = current->link;
}
}
}
};
int main()
{
objectType object1;
objectType object2 = object1;
return 0;
}
用调试器测试后,我发现问题出在析构函数上。据我所知,当对象超出范围时调用析构函数。在这种情况下对象是否超出范围?
P.S。现在可以将此代码视为 MCVE 吗?是的,重现代码确实有助于我找出问题的真正根源。
这意味着您的 objectType
有一个损坏的复制构造函数实现,但有一个有效的复制赋值运算符。
您可以向自己证明这不是 类 的特征,方法是对任何其他类型(例如 std::string
)执行此操作。
很遗憾,我们无法告诉您它是如何损坏的,因为我们看不到它;启动你的调试器并开始破解!
我为特定的 class 重载了赋值运算符,之后我很快发现了一个问题。
在 class 对象的声明过程中,如果我用另一个已经存在的对象初始化它,
objectType object1;
objectType object2 = object1;
程序将终止并显示 program.exe 在 运行 期间停止工作的消息。
但是,如果我将声明和初始化步骤分开,
objectType object1, object2;
object2 = object1;
它将正常工作。
如果我们可以用简单的数据类型做到这一点,
int x = 6;
int y = x;
为什么我们不能对 class 对象执行此操作?我希望我的问题很清楚,我已经用相同的 .exe 停止工作结果在不同的计算机上进行了测试。
已编辑: 这是我的代码。 class 本身就是一个堆栈。
#include <iostream>
using namespace std;
struct nodeType
{
int info;
nodeType *link;
};
class objectType
{
public:
const objectType& operator=(const objectType& otherObject)
{
if(this != &otherObject)
{
copyObject(otherObject);
}
return *this;
}
void initialize() //Initialize the stack
{
nodeType *temp;
while(stackTop != nullptr)
{
temp = stackTop;
stackTop = stackTop->link;
delete temp;
}
}
objectType(const objectType& otherObject) //Copy constructor
{
copyObject(otherObject);
}
objectType() //Constructor
{
stackTop = nullptr;
}
~objectType() //Destructor
{
initialize();
}
private:
nodeType* stackTop;
//Copy function to implement copy constructor and overload assignment operator
void copyObject(const objectType& otherObject)
{
initialize();
if(otherObject.stackTop != nullptr)
{
nodeType *current, *last, *newNode;
current = otherObject.stackTop;
stackTop = new nodeType;
stackTop->info = current->info;
stackTop->link = nullptr;
last = stackTop;
current = current->link;
while(current != nullptr)
{
newNode = new nodeType;
newNode->info = current->info;
newNode->link = nullptr;
last->link = newNode;
last = newNode;
current = current->link;
}
}
}
};
int main()
{
objectType object1;
objectType object2 = object1;
return 0;
}
用调试器测试后,我发现问题出在析构函数上。据我所知,当对象超出范围时调用析构函数。在这种情况下对象是否超出范围?
P.S。现在可以将此代码视为 MCVE 吗?是的,重现代码确实有助于我找出问题的真正根源。
这意味着您的 objectType
有一个损坏的复制构造函数实现,但有一个有效的复制赋值运算符。
您可以向自己证明这不是 类 的特征,方法是对任何其他类型(例如 std::string
)执行此操作。
很遗憾,我们无法告诉您它是如何损坏的,因为我们看不到它;启动你的调试器并开始破解!