您好,我需要訪問我的類中的指針成員,用於重載的賦值運算符。 我的代碼在下面?請讓我什麼我做錯了..謝謝 它的一個簡單的程序來實現的拷貝構造函數,重載的賦值運算符,析構函數如何訪問類的指針成員
#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;
}
第一眼:訪問私有成員沒有getter? – texasbruce
哇,這段代碼只是可怕的。看看所有那些毫無意義的'新'和原始數組! –