构造函数和析构函数的有关问题
构造函数和析构函数的问题
//pointer.h
class A{
public:
A();
A(int _x);
~A();
private:
int x;
};
class B{
public:
B();
B(int x,int y, A a);
~B();
private:
int m_x;
int m_y;
A m_a;
};
//pointer.cpp
#include "Pointer.h "
#include <iostream>
using namespace std;
A::A()
{
cout < < "constructor A() " < <endl;
}
A::A(int _x)
{
x = _x;
cout < < "constructor A(int _x) " < <endl;
}
A::~A()
{
cout < < "unconstructor ~A() " < <endl;
}
B::B()
{
cout < < "constructor B() " < <endl;
}
B::B(int x, int y, A a):m_a(a)
{
m_x = x;
m_y = y;
cout < < "constructor B(int x,int y, A a):m_a(a) " < <endl;
}
B::~B()
{
cout < < "m_x = " < < m_x < < " m_y = " < < m_y < <endl;
cout < < "unconstructor ~B() " < <endl;
}
//main.cpp
#include "Pointer.h "
int main()
{
A aInstance;
B bInstance(10,10,aInstance);
return 0;
}
运行结果如下:
constructor A()
constructor B(int x,int y, A a):m_a(a)
unconstructor ~A()
m_x = 10 m_y = 10
unconstructor ~B()
unconstructor ~A()
unconstructor ~A()
请按任意键继续. . .
我觉得多了一个unconstructor ~A(),为什么会这样呢?请大家帮助分析一下?
------解决方案--------------------
给你的A加一个拷贝构造函数,里面加一个打印。
------解决方案--------------------
//pointer.h
#ifndef POINTER_H
#define POINTER_H
class A{
public:
A();
A(int _x);
A(const A &a);
~A();
private:
int x;
};
class B{
public:
B();
B(int x,int y, A &a);
~B();
private:
int m_x;
int m_y;
A *m_a;
};
#endif
//pointer.cpp
#include "Pointer.h "
#include <iostream>
using namespace std;
A::A()
{
cout < < "constructor A() " < <endl;
}
A::A(int _x)
{
x = _x;
cout < < "constructor A(int _x) " < <endl;
}
A::A(const A &a)
{
x = a.x;
cout < < "constructor A(const A &a) " < <endl;
}
A::~A()
{
cout < < "unconstructor ~A() " < <endl;
}
B::B()
{
m_a = NULL;
//pointer.h
class A{
public:
A();
A(int _x);
~A();
private:
int x;
};
class B{
public:
B();
B(int x,int y, A a);
~B();
private:
int m_x;
int m_y;
A m_a;
};
//pointer.cpp
#include "Pointer.h "
#include <iostream>
using namespace std;
A::A()
{
cout < < "constructor A() " < <endl;
}
A::A(int _x)
{
x = _x;
cout < < "constructor A(int _x) " < <endl;
}
A::~A()
{
cout < < "unconstructor ~A() " < <endl;
}
B::B()
{
cout < < "constructor B() " < <endl;
}
B::B(int x, int y, A a):m_a(a)
{
m_x = x;
m_y = y;
cout < < "constructor B(int x,int y, A a):m_a(a) " < <endl;
}
B::~B()
{
cout < < "m_x = " < < m_x < < " m_y = " < < m_y < <endl;
cout < < "unconstructor ~B() " < <endl;
}
//main.cpp
#include "Pointer.h "
int main()
{
A aInstance;
B bInstance(10,10,aInstance);
return 0;
}
运行结果如下:
constructor A()
constructor B(int x,int y, A a):m_a(a)
unconstructor ~A()
m_x = 10 m_y = 10
unconstructor ~B()
unconstructor ~A()
unconstructor ~A()
请按任意键继续. . .
我觉得多了一个unconstructor ~A(),为什么会这样呢?请大家帮助分析一下?
------解决方案--------------------
给你的A加一个拷贝构造函数,里面加一个打印。
------解决方案--------------------
//pointer.h
#ifndef POINTER_H
#define POINTER_H
class A{
public:
A();
A(int _x);
A(const A &a);
~A();
private:
int x;
};
class B{
public:
B();
B(int x,int y, A &a);
~B();
private:
int m_x;
int m_y;
A *m_a;
};
#endif
//pointer.cpp
#include "Pointer.h "
#include <iostream>
using namespace std;
A::A()
{
cout < < "constructor A() " < <endl;
}
A::A(int _x)
{
x = _x;
cout < < "constructor A(int _x) " < <endl;
}
A::A(const A &a)
{
x = a.x;
cout < < "constructor A(const A &a) " < <endl;
}
A::~A()
{
cout < < "unconstructor ~A() " < <endl;
}
B::B()
{
m_a = NULL;