Question about using copy constructor of parent class?

Discussion in 'C++' started by flamexx7, Jul 20, 2006.

  1. flamexx7

    flamexx7 Guest

    In the programme below is it possible to call copy constructor of class A,
    inside copy constructor of class B.
    #include <iostream>
    using namespace std;
    class A{
    int a;
    public:
    A(int temp=0):a(temp){}
    A(A& right):a(right.a){}
    };
    class B:public A{
    int b;
    public:
    B(int temp=0):b(temp){}
    B(B& right){ A(right.A);
    }
    };
    int main(){
    }
    flamexx7, Jul 20, 2006
    #1
    1. Advertising

  2. flamexx7

    Rolf Magnus Guest

    flamexx7 wrote:

    > In the programme below is it possible to call copy constructor of class A,
    > inside copy constructor of class B.


    Yes, but not for the current object. You can do that in the initializer
    list.

    > #include <iostream>
    > using namespace std;
    > class A{
    > int a;
    > public:
    > A(int temp=0):a(temp){}
    > A(A& right):a(right.a){}


    Should be:

    A(const A& right):a(right.a){}

    Or even better, just leave it out completely. The compiler will then
    generate a copy constructor for you that does exactly the same.

    > };
    > class B:public A{
    > int b;
    > public:
    > B(int temp=0):b(temp){}
    > B(B& right){ A(right.A);
    > }


    B(const B& right):A(right) {}


    > };
    > int main(){
    > }
    Rolf Magnus, Jul 20, 2006
    #2
    1. Advertising

  3. flamexx7

    flamexx7 Guest

    In my book there is a question about "properly" creating a copy constructor
    of child class during inheritance. Is it ok to use upcasting here ? I've
    used upcasting and it seems to work fine.

    #include <iostream>
    using namespace std;
    class A{
    int a;
    public:
    A(int temp=0):a(temp){}
    A(A& right):a(right.a){}
    };
    class B:public A{
    int b;
    public:
    B(int temp=0):b(temp){}
    B(B& right){A(*this);
    b=right.b;
    }
    };
    int main(){
    B b;
    B b1=b;
    cin.get();
    }




    "Rolf Magnus" <> wrote in message
    news:e9ou38$mcu$02$-online.com...
    > flamexx7 wrote:
    >
    >> In the programme below is it possible to call copy constructor of class
    >> A,
    >> inside copy constructor of class B.

    >
    > Yes, but not for the current object. You can do that in the initializer
    > list.
    >
    >> #include <iostream>
    >> using namespace std;
    >> class A{
    >> int a;
    >> public:
    >> A(int temp=0):a(temp){}
    >> A(A& right):a(right.a){}

    >
    > Should be:
    >
    > A(const A& right):a(right.a){}
    >
    > Or even better, just leave it out completely. The compiler will then
    > generate a copy constructor for you that does exactly the same.
    >
    >> };
    >> class B:public A{
    >> int b;
    >> public:
    >> B(int temp=0):b(temp){}
    >> B(B& right){ A(right.A);
    >> }

    >
    > B(const B& right):A(right) {}
    >
    >
    >> };
    >> int main(){
    >> }

    >
    flamexx7, Jul 21, 2006
    #3
  4. flamexx7

    Guest

    flamexx7 wrote:
    > In my book there is a question about "properly" creating a copy constructor
    > of child class during inheritance. Is it ok to use upcasting here ? I've
    > used upcasting and it seems to work fine.
    >
    > #include <iostream>
    > using namespace std;
    > class A{
    > int a;
    > public:
    > A(int temp=0):a(temp){}
    > A(A& right):a(right.a){}
    > };
    > class B:public A{
    > int b;
    > public:
    > B(int temp=0):b(temp){}
    > B(B& right){A(*this);
    > b=right.b;
    > }
    > };
    > int main(){
    > B b;
    > B b1=b;
    > cin.get();
    > }
    >


    It compiles, but, although I don't know what you want it to do, it
    doesn't look like it does what I think you want it to do.

    In the copy constructor of B, you initialize A with *this, before B is
    initialised. So, if you in main() do:

    int main(){
    B b(10);
    B b1=b;

    you'll get a B1 who's a is uninitialised (that's what it looks like to
    me. I just compiled it with g++-3.4.4, and that shows that b1.a
    initialised to whatever default value I put in A::A(int temp=xxx)
    constructor.

    #include <iostream>
    using namespace std;
    class A{
    int a;
    public:
    A(int temp=123):a(temp){}
    A(A& right):a(right.a){}
    void show(){
    cout<<"A: a="<<a<<endl;
    }
    };
    class B:public A{
    int b;
    public:
    B(int temp=124):b(temp){}
    B(B& right){A(*this);
    b=right.b;
    }
    void show(){
    A::show();
    cout<<"B: b="<<b<<endl;
    }
    };
    int main(){
    B b(10);
    b.show();
    B b1=b;
    b1.show();
    }

    Output:

    A: a=123
    B: b=10
    A: a=123
    B: b=10


    Oh, and BTW, why didn't you do as suggested, make the ref arguments
    const?
    , Jul 21, 2006
    #4
  5. flamexx7

    Rolf Magnus Guest

    flamexx7 wrote:

    > In my book there is a question about "properly" creating a copy
    > constructor of child class during inheritance. Is it ok to use upcasting
    > here ?


    No need to cast.

    > I've used upcasting and it seems to work fine.
    >
    > #include <iostream>
    > using namespace std;
    > class A{
    > int a;
    > public:
    > A(int temp=0):a(temp){}
    > A(A& right):a(right.a){}
    > };
    > class B:public A{
    > int b;
    > public:
    > B(int temp=0):b(temp){}
    > B(B& right){A(*this);
    > b=right.b;
    > }


    This constructor probably won't do what you want. The

    A(*this);

    creates a new temprary object of type A and immediately afterwards, destroys
    it. The A part of your B object gets default initialized. It has nothing at
    all to do with the A in your constructor body. I'll repeat myself: You can
    only initialize the base class parts of your object in the initializer
    list. In the constructor body, the initialization is finished.

    > };
    > int main(){
    > B b;
    > B b1=b;
    > cin.get();
    > }


    You tricked yourself, because the initialization you were trying to do just
    happens to do the same as the default initialization - they both set the
    int member to 0.
    Rolf Magnus, Jul 21, 2006
    #5
  6. flamexx7

    flamexx7 Guest

    "flamexx7" <none@none> wrote in message
    news:44c09ac4$0$15789$...
    > In my book there is a question about "properly" creating a copy
    > constructor of child class during inheritance. Is it ok to use upcasting
    > here ? I've used upcasting and it seems to work fine.
    >
    > #include <iostream>
    > using namespace std;
    > class A{
    > int a;
    > public:
    > A(int temp=0):a(temp){}
    > A(const A& right):a(right.a){}
    > };
    > class B:public A{
    > int b;
    > public:
    > B(int temp=0):b(temp){}
    > B(const B& right){A(*this);


    You're right rolf, I tricked myelf. Now I know instead of

    > B(const B& right){A(*this);


    it should be

    B(const B& right):A(right){


    > b=right.b;
    > }
    > };
    > int main(){
    > B b;
    > B b1=b;
    > cin.get();
    > }
    >
    >
    >
    >
    > "Rolf Magnus" <> wrote in message
    > news:e9ou38$mcu$02$-online.com...
    >> flamexx7 wrote:
    >>
    >>> In the programme below is it possible to call copy constructor of class
    >>> A,
    >>> inside copy constructor of class B.

    >>
    >> Yes, but not for the current object. You can do that in the initializer
    >> list.
    >>
    >>> #include <iostream>
    >>> using namespace std;
    >>> class A{
    >>> int a;
    >>> public:
    >>> A(int temp=0):a(temp){}
    >>> A(A& right):a(right.a){}

    >>
    >> Should be:
    >>
    >> A(const A& right):a(right.a){}
    >>
    >> Or even better, just leave it out completely. The compiler will then
    >> generate a copy constructor for you that does exactly the same.
    >>
    >>> };
    >>> class B:public A{
    >>> int b;
    >>> public:
    >>> B(int temp=0):b(temp){}
    >>> B(B& right){ A(right.A);
    >>> }

    >>
    >> B(const B& right):A(right) {}
    >>
    >>
    >>> };
    >>> int main(){
    >>> }

    >>

    >
    >
    flamexx7, Jul 22, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pantalaimon
    Replies:
    3
    Views:
    51,025
    John Harrison
    Oct 9, 2004
  2. Replies:
    6
    Views:
    487
  3. ali
    Replies:
    4
    Views:
    558
    David Harmon
    Mar 5, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,191
  5. cinsk
    Replies:
    35
    Views:
    2,552
    James Kanze
    Oct 11, 2010
Loading...

Share This Page