类定义未完成,怎么可以作为参数使用呢????
//header
#include <windows.h>
#include <iostream>
using namespace std;
class TestClass
{
public:
TestClass();
~TestClass();
TestClass(TestClass & t)
{
cout<<"copy constructor"<<endl;
}
//copy constructor
void TestFunc(TestClass ttcc);//类未定义完成,就可当类型使用吗?为什么?
TestClass m_tc; //这个可以吗?为什么
TestClass *m_ptc;//这个可以吗?为什么
};
#include "Test.h"
TestClass::TestClass()
{
cout<<"create"<<endl;
}
TestClass::~TestClass()
{
cout<<"destroy"<<endl;
}
void TestClass::TestFunc(TestClass ttcc)
{
cout<<"fuction"<<endl;
}
//main
#include "Test.h"
void main()
{
TestClass tc;
tc.TestFunc(tc);
};
[解决办法]
弱鸟一枚,表示TestClass m_tc; 肯定不行吧。
[解决办法]
可以是指针或引用
[解决办法]
可以的,实例可以看一下类的拷贝构造函数。类的拷贝构造函数的形参都是该类的引用变量。
[解决办法]
都可以
编译一个程序不是一遍就完成的
[解决办法]
不好意思整错地方了
TestClass m_tc; //不可以 类中包含自身实例, 实例中又包含实例 死循环了
其它的两个可以
[解决办法]
如果这个类的数据比较大,作为函数参数类型效率会比较低,一般建议用指针或引用