C++ 没有调用析构函数?
我试图跟踪这个程序,但出于某种原因,它显示 进程已完成,退出代码为4。 不调用析构函数就结束了?原因可能是什么C++ 没有调用析构函数?,c++,class,oop,polymorphism,destructor,C++,Class,Oop,Polymorphism,Destructor,我试图跟踪这个程序,但出于某种原因,它显示 进程已完成,退出代码为4。 不调用析构函数就结束了?原因可能是什么 #include <iostream> using namespace std; class A { public: A() { cout << "A ctor" << endl; } A(const A& a) { cout << "A copy ctor" << endl; } virtu
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "A ctor" << endl; }
A(const A& a) { cout << "A copy ctor" << endl; }
virtual ~A() { cout << "A dtor" << endl; }
virtual void foo() { cout << "A foo()" << endl; }
virtual A& operator=(const A& rhs) { cout << "A op=" << endl; }
};
class B : public A {
public:
B() { cout << "B ctor" << endl; }
virtual ~B() { cout << "B dtor" << endl; }
virtual void foo() { cout << "B foo()" << endl; }
protected:
A mInstanceOfA; // don't forget about me!
};
A foo(A& input)
{
input.foo();
return input;
}
int main()
{
B myB;
B myOtherB;
A myA;
myOtherB = myB;
myA = foo(myOtherB);
}
#包括
使用名称空间std;
甲级{
公众:
A(){cout您的程序表现出未定义的行为,通过到达非void函数的右大括号(这里,操作符=
),而没有遇到return
语句。对于我(使用gcc 8.3.0),它工作得很好。但是我得到一个警告:
test.cpp: In member function ‘virtual A& A::operator=(const A&)’:
test.cpp:10:63: warning: no return statement in function returning non-void [-Wreturn-type]
virtual A& operator=(const A& rhs) { cout << "A op=" << endl; }
也许您应该尝试解决警告。打开/打开编译器警告!看起来像UB,请参阅编译器警告:
A ctor
A ctor
B ctor
A ctor
A ctor
B ctor
A ctor
A op=
A op=
B foo()
A copy ctor
A op=
A dtor
A dtor
B dtor
A dtor
A dtor
B dtor
A dtor
A dtor