Subclassed copy constructor not working 
Author Message
 Subclassed copy constructor not working

Hello,

I'd really appreciate it if someone could give me a (hopefully) quick
answer to this little conundrum:

I've got a class

class A
{
public:

        A(A* pA);
        A();
        virtual ~A();

Quote:
}

A::A()
{

Quote:
}

A::A(A* pA)
{
        if(pA)
        {
                //variable copying here

        }

Quote:
}

And another class

class B : public A
{
public:

        B(B* pB);
        B();
        virtual ~B();

Quote:
}

B::B()
{

Quote:
}

B::B(B* pB)
{
        if(pB)
        {
                //variable copying here

                //but I also want to copy all of the base class
variables, so
                A((A*)pB);  <--- PROBLEM

        }

Quote:
}

So if I want to make a copy of my class B

B* pX=new B();
B* pB=new B(pX);

All the variables in B are copied OK, but the base class variables
aren't. It seems to make a new class of type A and then delete it
immediately.

Question - What syntax do I need to succesfully copy B with all the
base class variables in A?

Regards,

Si



Sun, 01 May 2005 12:56:27 GMT  
 Subclassed copy constructor not working
How about this:

B::B(B* pB) :
    A(pB)    // invoke super-class
{
    if (pB)
    {
        // copy here
    }

Quote:
}

TFM3

Note: Spam-resistant e-mail address


Quote:
> Hello,

> I'd really appreciate it if someone could give me a (hopefully) quick
> answer to this little conundrum:

> I've got a class

> class A
> {
> public:

> A(A* pA);
> A();
> virtual ~A();
> }

> A::A()
> {

> }

> A::A(A* pA)
> {
> if(pA)
> {
> file://variable copying here

> }
> }

> And another class

> class B : public A
> {
> public:

> B(B* pB);
> B();
> virtual ~B();
> }

> B::B()
> {

> }

> B::B(B* pB)
> {
> if(pB)
> {
> file://variable copying here

> file://but I also want to copy all of the base class
> variables, so
> A((A*)pB);  <--- PROBLEM

> }
> }

> So if I want to make a copy of my class B

> B* pX=new B();
> B* pB=new B(pX);

> All the variables in B are copied OK, but the base class variables
> aren't. It seems to make a new class of type A and then delete it
> immediately.

> Question - What syntax do I need to succesfully copy B with all the
> base class variables in A?

> Regards,

> Si



Sun, 01 May 2005 15:13:18 GMT  
 Subclassed copy constructor not working
On Wed, 13 Nov 2002 08:13:18 -0600, "Thomas Mooney"

Quote:

>How about this:

>B::B(B* pB) :
>    A(pB)    // invoke super-class
>{
>    if (pB)
>    {
>        // copy here
>    }
>}

That seems to work just fine. Cheers.

Si



Sun, 01 May 2005 16:47:23 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Not making default copy constructor

2. Copy constructor not define? List template

3. Copy Constructor : Copying Base Class Member Variables

4. Virtual Functions Do Not Work Inside Constructors ?!?

5. String copying not working?

6. Number of Copies Not Working

7. Copy/Paste using Ctrl+C/Ctrl+V does not work in CHtmlView Class

8. Copy/Paste via keyboard not working in FormView

9. Rich Edit cut/copy does not work?

10. copy does not work

11. C++/ATL/ADO - Intellisense not working (statement completion options) not working

12. Handlers in subclassed control and twice-subclassed control

 

 
Powered by phpBB® Forum Software