我是一种新的C++类,我需要制作一个使用指向对象的指针的复制构造函数。 我的类看起来是这样的:复制指针对象的构造函数
class test
{
int a;
char *b;
public:
test()
{
a=0;
b="\0";
}
test(const test &p)
{
b=new char(strlen(p.b)+1);
b=p.b;
a=p.a;
cout<<"The copy constructor works";
}
void seta(int nr)
{
a=nr;
}
void setb(char *s)
{
b=s;
}
int geta()
{
return a;
}
char *getb()
{
return b;
}
};
现在我有一个指向对象
test *x;
x=new test;
我需要做的是创建另一个指向对象,* Y,将具有相同值作为x,使用复制构造函数。如果它们是对象而不是指针,那么这个拷贝构造函数会起作用。你能告诉我该怎么办吗?谢谢!
语义pedantry:复制构造*按定义*不采取指针。你在说什么更正确地称为转换构造函数。 – 2015-03-13 18:29:43