嗨,我需要访问我班级中的指针成员,用于重载赋值运算符。我的代码如下?请让我做错了什么..感谢它是一个实现复制构造函数、重载赋值运算符、析构函数的简单程序
#include<iostream>
class A
{
private:
int a;
char *ctr;
int *itr;
public:
A()
{
a=10;
ctr=new char[10];
itr=NULL;
cout<<"Inside default constructor"<<endl;
}
A(const A &b)
{
a=b.a;
ctr= newchar[10];
itr=new int;
*itr=b.*itr;
*ctr=b.*ctr;
cout<<"inside copy constructor"<<endl;
}
A operator=(const A a)
{
A b;
b.a=a.a;
b.*itr=a.*itr;
b.*ctr=a.*ctr;
cout<<"Overloaded assignment operator"<<endl;
return b;
}
~A()
{
delete(itr);
delete(ctr);
cout<<"destructor"<<endl;
}
};
int main()
{
A a1 ,a2;
a1.*ctr="GOVIND";
a2.*ctr="SINGH";
a1.*itr=35;
a2.*itr=99;
A a3=a2;
cout<<"Class template program"<<endl;
cout<<a1.a<<" ",,a1.*ctr<<" "<<a1.*itr<<endl;
cout<<a2.a<<" ",,a2.*ctr<<" "<<a2.*itr<<endl;
cout<<a3.a<<" ",,a3.*ctr<<" "<<a3.*itr<<endl;
return 0;
}