Reputation: 21
hi i need to access a pointer member in my class, for overloaded assignment operator . my code below ? please let me what i am doing wrong .. thanks its a simple program to implement copy constructor,overloaded assignment operator,destructor
#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;
}
Upvotes: 1
Views: 150
Reputation: 71899
Sooo much wrong, where do I even start?
First, smart pointers, man! Smart pointers!
A(const A &b)
{
a=b.a;
ctr= newchar[10];
itr=new int;
*itr=b.*itr;
Correct syntax to assign the value is
*itr = *b.itr;
If you want to copy the pointer, that also looks different, but then you shared pointers and get in trouble, so don't do that.
Anyway, that's still wrong, because b.itr might be null (your default constructor makes it null). So figure out what you actually want. Should itr never be non-null? Allocate something in the constructor. Should it be possibly null? Then respect that possibility in the copy constructor.
*ctr=b.*ctr;
Again wrong syntax, but even if you correct it, it's still wrong: it only copies the first char in the array. Why do you have this array here anyway? It's fixed size. Do you want a simple fixed array? Do you perhaps want a string?
cout<<"inside copy constructor"<<endl;
}
A operator=(const A a)
Did you mean for a
to be a reference? A const value is not a good thing to pass to an assignment operator. A non-const value could be a good idea, if you implement the assignment correctly then.
{
A b;
What's this b
supposed to be? Don't you want to work with the current object?
b.a=a.a;
Should be a = a.a;
b.*itr=a.*itr;
As in the copy constructor, only worse: if itr is already allocated, you leak the old memory.
b.*ctr=a.*ctr;
As above and with the same new issue.
cout<<"Overloaded assignment operator"<<endl;
return b;
Copy assignment should return *this
. You're creating a dangling reference. You also clearly haven't got your compiler warnings turned up high enough (or you're ignoring them) if it doesn't scream at you for this.
}
~A()
{
delete(itr);
Don't use parentheses on delete
. Well, you can, but it's unusual, and misleading. delete
is not a function.
delete(ctr);
ctr
was allocated with new[]
, you must use delete[]
to free its memory.
cout<<"destructor"<<endl;
}
And some final words: smart pointers!
Upvotes: 1
Reputation: 42165
b.*itr=a.*itr;
b.*ctr=a.*ctr;
should be
*b.itr=*a.itr;
*b.ctr=*a.ctr;
Also, shouldn't the operator be returning a reference to this
?
A& operator=(const A a)
{
this->a = a.a;
*itr = *a.itr;
*ctr = *a.ctr;
return *this;
}
Upvotes: 5