define a copy constructor in a class having data member as an object of another class

Discussion in 'C++' started by dalu.gelu@gmail.com, Nov 9, 2006.

  1. Guest

    Hi,
    can anyone help me by writing a sample code of defining a copy
    constructor in a class having data member as an object of another
    class.
    for eg:
    class A{
    int x;
    public: A(){ x=6;}
    };

    class B{
    A a1;
    public:B(B &b1)
    { ???} //how i can assign the data members??
    };

    Any idea ...Thanks
    , Nov 9, 2006
    #1
    1. Advertising

  2. VJ Guest

    Re: define a copy constructor in a class having data member as anobject of another class

    wrote:
    > Hi,
    > can anyone help me by writing a sample code of defining a copy
    > constructor in a class having data member as an object of another
    > class.
    > for eg:
    > class A{
    > int x;
    > public: A(){ x=6;}
    > };
    >
    > class B{
    > A a1;
    > public:B(B &b1)
    > { ???} //how i can assign the data members??
    > };
    >
    > Any idea ...Thanks
    >


    1) add a public method to class A to assign a value to x
    2) make class B friend of class A
    VJ, Nov 9, 2006
    #2
    1. Advertising

  3. Greg Guest

    wrote:
    > can anyone help me by writing a sample code of defining a copy
    > constructor in a class having data member as an object of another
    > class.
    > for eg:
    > class A{
    > int x;
    > public: A(){ x=6;}
    > };
    >
    > class B{
    > A a1;
    > public:B(B &b1)
    > { ???} //how i can assign the data members??
    > };


    Like so:

    class B
    {
    public:
    B( B& b1 )
    : a1( b1.a1 )
    {
    }
    };

    Greg
    Greg, Nov 9, 2006
    #3
  4. Greg Guest

    wrote:
    > Hi,
    > can anyone help me by writing a sample code of defining a copy
    > constructor in a class having data member as an object of another
    > class.
    > for eg:
    > class A{
    > int x;
    > public: A(){ x=6;}
    > };
    >
    > class B{
    > A a1;
    > public:B(B &b1)
    > { ???} //how i can assign the data members??
    > };


    Like so:

    class B
    {
    A a1;

    public:
    B( B& b1 ) : a1( b1.a1 )
    { }
    };

    Greg
    Greg, Nov 9, 2006
    #4
  5. Guest

    thanks,

    if the situation is so:
    class B{
    A *a1;
    public:
    B(B &b1){???}
    , Nov 9, 2006
    #5
  6. Greg Guest

    wrote:
    > thanks,
    >
    > if the situation is so:
    > class B{
    > A *a1;
    > public:
    > B(B &b1){???}


    Pretty much as one might expect:

    class B
    {
    A *a1;
    public:
    B( B& b1)
    {
    a1 = b1.a1;
    }
    };

    Note that "a1", even as a pointer member, can still be initialized the
    same way as before (in the member list initializer of the contructor)
    instead of within the body of B's construtor - as shown here.

    When possible, it's better to initialize a class member in the
    constructor's member-initializer list than in its body.

    Greg
    Greg, Nov 9, 2006
    #6
  7. Greg Guest

    wrote:
    > thanks,
    >
    > if the situation is so:
    > class B{
    > A *a1;
    > public:
    > B(B &b1){???}


    This case is trickier since a1 is now a pointer. The question is
    whether to make a "deep copy" or a "shallow copy" of B. A deep copy
    also creates a copy of the A object that a1 points to - and then
    assigns the copied A object's address to the a1 member pointer in B's
    copy. A shallow copy would just copy the a1 pointer itself - meaning
    that both the original and copied B object would each have an a1 member
    pointing to the same A object - which can be a problem if B is supposed
    to delete the A object at some point (more on that problem later).

    In terms of advantages, though, a shallow copy is more efficient and
    economical than making a deep copy. Furthermore, a shallow copy of B
    looks easy enough to implement:

    class B
    {
    A *a1;
    public:
    B( B& b1)
    {
    a1 = b1.a1;
    }
    };

    ....but as mentioned above can be very difficult to maintain correctly,
    now that a1 is shared. So to manage a shallow copy safely, B should
    turn its a1 member into a "smart pointer" such as
    std::tr1::shared_ptr<A>.

    Note that "a1", even as a pointer member, can still be initialized the
    same way as before (in the member list initializer of the constructor)
    instead of within the body of B's constructor - as shown here. When
    possible, it's better to initialize a class member in the constructor's
    member-initializer list than in its body.

    Greg
    Greg, Nov 9, 2006
    #7
  8. Guest

    Thanks Greg, its a wonderful explanation..

    its fine in compiling and linking the program in case of shallow copy u
    provided
    class B
    > {
    > A *a1;
    > public:
    > B( B& b1)
    > {
    > a1 = b1.a1;
    > }
    > };
    >


    but when i try to run it ends with some memory unhandled exception, its
    perhaps due to shallow copy where it only copies the memory address not
    the corresponding value.

    ok then i hope the correct answer for tthe problem is to make it a deep
    copy. could u please add some more lines in the above code so that the
    output will be ok.
    , Nov 9, 2006
    #8
  9. Ron Natalie Guest

    Re: define a copy constructor in a class having data member as anobject of another class

    wrote:
    > Thanks Greg, its a wonderful explanation..
    >
    > its fine in compiling and linking the program in case of shallow copy u
    > provided
    > class B
    >> {
    >> A *a1;
    >> public:
    >> B( B& b1)
    >> {
    >> a1 = b1.a1;
    >> }
    >> };
    >>

    >
    > but when i try to run it ends with some memory unhandled exception, its
    > perhaps due to shallow copy where it only copies the memory address not
    > the corresponding value.
    >
    > ok then i hope the correct answer for tthe problem is to make it a deep
    > copy. could u please add some more lines in the above code so that the
    > output will be ok.
    >

    We can't answer your question without knowing what A is, and how a1
    was set initially. Most likely your problem is less with the shallow
    copy, but more with the fact that the object pointed by a1 is
    mismanaged (deleted twice, deleted while people are still using it,
    etc...).

    Further the rule of three applies very much here. If you need to
    define a copy constructor, a copy-assignment operator, or a destructor,
    you probably need to define all three.
    Ron Natalie, Nov 9, 2006
    #9
  10. Guest

    HI I have already define A in the begining of the thread, anyway A is a
    class and here is the defn:
    class A{
    int x;
    public: A(){ x=6;}
    };

    how to ensure that if i write overloaded assignment op and destructor
    here then evrything will be fine...:)

    Ron Natalie wrote:
    > wrote:
    > > Thanks Greg, its a wonderful explanation..
    > >
    > > its fine in compiling and linking the program in case of shallow copy u
    > > provided
    > > class B
    > >> {
    > >> A *a1;
    > >> public:
    > >> B( B& b1)
    > >> {
    > >> a1 = b1.a1;
    > >> }
    > >> };
    > >>

    > >
    > > but when i try to run it ends with some memory unhandled exception, its
    > > perhaps due to shallow copy where it only copies the memory address not
    > > the corresponding value.
    > >
    > > ok then i hope the correct answer for tthe problem is to make it a deep
    > > copy. could u please add some more lines in the above code so that the
    > > output will be ok.
    > >

    > We can't answer your question without knowing what A is, and how a1
    > was set initially. Most likely your problem is less with the shallow
    > copy, but more with the fact that the object pointed by a1 is
    > mismanaged (deleted twice, deleted while people are still using it,
    > etc...).
    >
    > Further the rule of three applies very much here. If you need to
    > define a copy constructor, a copy-assignment operator, or a destructor,
    > you probably need to define all three.
    , Nov 9, 2006
    #10
  11. LR Guest

    Re: define a copy constructor in a class having data member as anobject of another class

    wrote:

    [Please do not top post, content moved]



    > Ron Natalie wrote:
    >
    >> wrote:
    >>
    >>>Thanks Greg, its a wonderful explanation..
    >>>
    >>>its fine in compiling and linking the program in case of shallow copy u
    >>>provided
    >>> class B
    >>>
    >>>> {
    >>>> A *a1;
    >>>> public:
    >>>> B( B& b1)
    >>>> {
    >>>> a1 = b1.a1;
    >>>> }
    >>>> };
    >>>>
    >>>
    >>>but when i try to run it ends with some memory unhandled exception, its
    >>>perhaps due to shallow copy where it only copies the memory address not
    >>>the corresponding value.
    >>>
    >>>ok then i hope the correct answer for tthe problem is to make it a deep
    >>>copy. could u please add some more lines in the above code so that the
    >>>output will be ok.
    >>>

    >>
    >>We can't answer your question without knowing what A is, and how a1
    >>was set initially. Most likely your problem is less with the shallow
    >>copy, but more with the fact that the object pointed by a1 is
    >>mismanaged (deleted twice, deleted while people are still using it,
    >>etc...).
    >>
    >>Further the rule of three applies very much here. If you need to
    >>define a copy constructor, a copy-assignment operator, or a destructor,
    >>you probably need to define all three.

    >
    >
    > HI I have already define A in the begining of the thread, anyway A is a
    > class and here is the defn:
    > class A{
    > int x;
    > public: A(){ x=6;}
    > };
    >
    > how to ensure that if i write overloaded assignment op and destructor
    > here then evrything will be fine...:)


    Depends on what you want them to do.



    Heres one way.

    class A {
    int x;
    public:
    void swap(A &a) {
    std::swap(a.x,x);
    }
    A() : x(6) {}
    // note const A & in copy ctor
    A(const A &a) : x(a.x) {}
    A &operator=(const A &a) {
    // see gotw for assigment ops
    A temp(a);
    swap(temp);
    return *this;
    }
    };

    class B {
    std::auto_ptr<A> a1;
    public:
    void swap(B &b) {
    // swap pointers, not instances
    std::swap(a1,b.a1);
    }
    B() : a1() {}
    B(const B &b) : a1( b.a1.get() ? new A(*b.a1.get()) : 0 ) {}
    B &operator=(const B &b) {
    B temp(b);
    swap(temp);
    return *this;
    }
    };


    Now, if you have to use a raw pointer, then you can back out of this, by
    replacing std::auto_ptr and creating your own destructor for class B,
    where you will have to delete the thing the pointer is pointing to.
    LR, Nov 9, 2006
    #11
  12. Ron Natalie Guest

    Re: define a copy constructor in a class having data member as anobject of another class

    wrote:
    > HI I have already define A in the begining of the thread, anyway A is a
    > class and here is the defn:
    > class A{
    > int x;
    > public: A(){ x=6;}
    > };
    >
    > how to ensure that if i write overloaded assignment op and destructor
    > here then evrything will be fine...:)
    >

    You haven't answered the question of how B::a1 gets set to anything.
    Ron Natalie, Nov 9, 2006
    #12
    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. VisionSet
    Replies:
    8
    Views:
    4,891
    Tris Orendorff
    Apr 29, 2004
  2. Henrik Goldman
    Replies:
    4
    Views:
    435
    Henrik Goldman
    Sep 9, 2006
  3. ali
    Replies:
    4
    Views:
    571
    David Harmon
    Mar 5, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,217
  5. cinsk
    Replies:
    35
    Views:
    2,593
    James Kanze
    Oct 11, 2010
Loading...

Share This Page