2 回答

TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊
1.構(gòu)造函數(shù)當(dāng)然沒有返回值,他只是描述了類初始化的行為。
2.但是new是有返回值的??!new返回的一定是類實(shí)例的指針啊,所以new tree返回的是tree*,所以構(gòu)造函數(shù)跟返回該類的空間是沒有關(guān)系的。
下面舉個(gè)例子給你看看:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
class tree {
int value;
public:
tree(int v):value(v){}
int get_value() {return value;}
};
int main(int argc, char *argv[])
{
//ifstream in_file("zhidao.text");
queue<tree*> q;
for (int i = 0;i< 10 ;++i )
{
tree * tmp = new tree(i);
q.push(tmp);
}
//test code
while(!q.empty())
{
cout<<q.front()->get_value()<<" ";
q.pop();
}
cout<<endl;
return 0;
}

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊
默認(rèn)構(gòu)造函數(shù)只在沒有定義任何構(gòu)造函數(shù)的時(shí)候系統(tǒng)才會(huì)自動(dòng)生成。
拷貝構(gòu)造函數(shù)也是構(gòu)造函數(shù)
拷貝構(gòu)造函數(shù)在沒有顯示定義的情況下系統(tǒng)會(huì)自動(dòng)生成一個(gè)默認(rèn)的拷貝構(gòu)造函數(shù)
b項(xiàng)如果定義了帶參數(shù)的構(gòu)造函數(shù),也不會(huì)生成默認(rèn)構(gòu)造函數(shù)了,所以是錯(cuò)的
- 2 回答
- 0 關(guān)注
- 160 瀏覽
添加回答
舉報(bào)