Copy constructor for a class that contains a pointer to a base classtype (newbie)

Discussion in 'C++' started by =?ISO-8859-1?Q?Szabolcs_Horv=E1t?=, Oct 27, 2006.

  1. This is a newbie question. Suppose I have a class A (see the attached
    code) which contains a pointer that can point to an object of either
    type D1 or D2. How do I write a copy constructor for A, so that the
    object that it references also gets copied (I don't know whether it is a
    D1 or D2 object)?

    Thank you for your answers in advance,
    Szabolcs

    ----

    class B {
    public:
    virtual void print() = 0;
    };

    class D1 : public B {
    int i;
    public:
    void print() { cout << i; }
    };

    class D2 : public B {
    double x;
    public:
    void print() { cout << x; }
    };

    class A {
    B *b;
    };
    =?ISO-8859-1?Q?Szabolcs_Horv=E1t?=, Oct 27, 2006
    #1
    1. Advertising

  2. =?ISO-8859-1?Q?Szabolcs_Horv=E1t?=

    David Harmon Guest

    Re: Copy constructor for a class that contains a pointer to a base class type (newbie)

    On Fri, 27 Oct 2006 21:00:34 +0200 in comp.lang.c++, Szabolcs Horvát
    <> wrote,
    >This is a newbie question. Suppose I have a class A (see the attached
    >code) which contains a pointer that can point to an object of either
    >type D1 or D2. How do I write a copy constructor for A, so that the
    >object that it references also gets copied


    As your example shows, the Object-Oriented way for a pointer to
    point to different classes is for them to be derived classes of a
    common base. In that case, one answer is for the base to declare
    virtual B* clone() = 0;
    and all the derived classes to implement that virtual function and
    each class return a new copy object of its own type. Your copy
    constructor calls the virtual b->clone() and gets the new copy that
    it needs without concern about how many derived classes there are.
    David Harmon, Oct 27, 2006
    #2
    1. Advertising

  3. Re: Copy constructor for a class that contains a pointer to a baseclass type (newbie)

    David Harmon wrote:
    > In that case, one answer is for the base to declare
    > virtual B* clone() = 0;
    > and all the derived classes to implement that virtual function and
    > each class return a new copy object of its own type. Your copy
    > constructor calls the virtual b->clone() and gets the new copy that
    > it needs without concern about how many derived classes there are.
    >


    Thanks! This solves my problem, but I thought that there is a better
    solution for this. What if I cannot change B, D1 and D2 (e.g. they are
    part of a library)?

    Szabolcs

    P.S.: Sorry for the duplicate post. First I tried to post using Google
    Groups, but I got an 500 Server Error. It seems that the message has
    been sent, but it took more than 40 minutes for it to arrive.
    =?ISO-8859-1?Q?Szabolcs_Horv=E1t?=, Oct 27, 2006
    #3
  4. =?ISO-8859-1?Q?Szabolcs_Horv=E1t?=

    Kai-Uwe Bux Guest

    Re: Copy constructor for a class that contains a pointer to a base class type (newbie)

    Szabolcs Horvát wrote:

    > David Harmon wrote:
    >> In that case, one answer is for the base to declare
    >> virtual B* clone() = 0;
    >> and all the derived classes to implement that virtual function and
    >> each class return a new copy object of its own type. Your copy
    >> constructor calls the virtual b->clone() and gets the new copy that
    >> it needs without concern about how many derived classes there are.
    >>

    >
    > Thanks! This solves my problem, but I thought that there is a better
    > solution for this. What if I cannot change B, D1 and D2 (e.g. they are
    > part of a library)?


    There are smart pointer templates with deep copy semantics that will help
    you. Search the archives of this news group for copy_ptr<> or clone_ptr<>.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 27, 2006
    #4
  5. =?ISO-8859-1?Q?Szabolcs_Horv=E1t?=

    Daniel T. Guest

    Re: Copy constructor for a class that contains a pointer to a base class type (newbie)

    Szabolcs Horvát <> wrote:

    > This is a newbie question. Suppose I have a class A (see the attached
    > code) which contains a pointer that can point to an object of either
    > type D1 or D2. How do I write a copy constructor for A, so that the
    > object that it references also gets copied (I don't know whether it is a
    > D1 or D2 object)?


    http://www.parashift.com/c -faq-lite/virtual-functions.html#faq-20.8

    --
    To send me email, put "sheltie" in the subject.
    Daniel T., Oct 28, 2006
    #5
    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. mandatory
    Replies:
    2
    Views:
    299
    Donovan Rebbechi
    Mar 21, 2005
  2. mrstephengross
    Replies:
    5
    Views:
    581
    Larry I Smith
    May 18, 2005
  3. SzH
    Replies:
    1
    Views:
    333
  4. ali
    Replies:
    4
    Views:
    554
    David Harmon
    Mar 5, 2007
  5. Rahul
    Replies:
    16
    Views:
    1,171
    terminator
    Nov 7, 2007
Loading...

Share This Page