Re: Copy C'tor - doubt
On 20 Lis, 00:04, AY <techreposit...@gmail.com> wrote:
I have a situation where I'm required to generate a copy of an object.
I have simplified the class into a tiny one, but in reality its a huge
class with multiple struct types as data member.
Here is my simplified class:
class AB
{
private:
int a;
public:
AB(int ar): a(ar){}
AB(const AB& arg)
{
a = arg.a;
}
};
int main()
{
AB* ab = new AB(7);
//AB* cd = new AB;
AB* cd(ab);
return 0;
}
My Qn is in the statement - AB* cd(ab);
Does object 'cd' uses the same memory as object 'ab' ?
Is any memory allocated in the heap for 'cd' like 'ab' [ I do not
think, maybe I'm wrong ] ?
What happens here exactly.
Thanks in advance friends,
- AY
In your example cd is a copy of ab pointer, not object pointed by ab.
You call copy constructor the same way you call other constructors:
AB* cd = new AB(ab); // on heap
or
AB cd(ab); // on stack
Cheers
Sfider
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]