To Access Base class Copy Constructor/Assignment Operator


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: To Access Base class Copy Constructor/Assignment Operator

  1. #1
    Abi Guest

    To Access Base class Copy Constructor/Assignment Operator


    Hi,

    How can we able to access Base class Copy Constructor while calling Derived
    class Copy Constructor? Or How can we access Base Class Assignment Operator
    while calling Derived Class Assignment Operator? Any suggestions?

    Regards,

    Abi Gabriel

    Code Snippet:
    -------------

    class Base
    {
    public :
    Base(int lx = 10, int ly = 11) : x(lx), y(ly)
    {

    }

    Base(const Base & rhs) // Copy constructor
    {
    x = rhs.x;
    y = rhs.y;
    }

    operator = (const Base & rhs) // Assignment Operator
    {
    x = rhs.x;
    y = rhs.y;
    }

    void setX(int lX) { x = lX; }
    void setY(int lY) { y = lY; }

    int getX() const { return x; }
    int getY() const { return y; }

    private :
    int x;
    int y;
    };

    class Derived : public Base
    {
    public :
    Derived(int la = 12, int lb = 13) : a(la), b(lb), Base(la + 5, lb - 5)
    {

    }

    Derived(const Derived & rhs) // Copy constructor
    {
    setX(rhs.getX()); // Explicit setting of
    setY(rhs.getY()); // Base Class parameters

    a = rhs.a;
    b = rhs.b;
    }

    operator=(const Derived & rhs) // Assignment Operator
    {
    setX(rhs.getX()); // Explicit setting of
    setY(rhs.getY()); // Base Class parameters

    a = rhs.a;
    b = rhs.b;
    }

    void setA(int lA) { a = lA; }
    void setB(int lB) { b = lB; }

    private :
    int a;
    int b;
    };

    void main()
    {
    Derived d1, d2;

    d1.setX(21);
    d1.setY(22);
    d1.setA(23);
    d1.setB(24);


    // It will call only Assignment Operator of Derived Class alone,
    // not of Base Class, because of this the Base Class members
    // won't be set properly. Hence, to set the parameters properly
    // in Base Class, we need to call explicit set() member functions
    // of Base Class in the Derived Class Assignment Operator , Abi
    // Is there any way to avoid this explicit setting in Derived Class?

    d2 = d1;

    // It will call only Copy Constructor of Derived Class alone,
    // not of Base Class, because of this Base Class members won't
    // be set properly. Hence, to set the parameters properly in
    // Base Class, we need to call explicit set() member functions
    // of Base Class in the Derived Class Copy Constructor, Abi
    // Is there any way to avoid this explicit setting in Derived Class?

    Derived d3 = d1;

    }

  2. #2
    ralph Guest

    Re: To Access Base class Copy Constructor/Assignment Operator


    "Abi" <abi.gabriel@celstream.com> wrote:
    >
    >Hi,
    >
    >How can we able to access Base class Copy Constructor while calling Derived
    >class Copy Constructor? Or How can we access Base Class Assignment Operator
    >while calling Derived Class Assignment Operator? Any suggestions?
    >
    >Regards,
    >
    >Abi Gabriel
    >


    Yes it seems like a lot of work, but I believe you are missing a subtle point
    here.

    The whole concept of implementation inheritance is that when you are done
    coding and the compiler has done its thing - you have "one" object - with
    one public interface. When 'coding' classes we are not packaging functionality
    and convenient routines into classes - like we do with procedural programming
    - and then just "call" those routines from the base class as we need them.
    When you are done coding, the thing you have coded becomes a new "thing",
    a new "type of Base". We have enhanced or limited the functionality of whatever
    lays below. The rest of the world has no need to even know 'Base' is there.

    So yes, if the thing you are super-classing/sub-classing (however you want
    to think about it) needs items to get initialized then you will have to gather
    them at the highest level - the derived constructor (the public interface)
    and take care to make it all transparent to the user of your new type. Users
    of your new class are not interested in, or even care if there are "two pieces",
    they just want to use this type of thing. If the base class needs special
    attention when the new thing is copied - then yes you will have to handle
    that inside your new "specialized" creature as well.

    Another point to consider is - if you are having all this "extra" over-head
    then maybe 'Derived' and 'Base' do not actually have a "kind of" relationship.
    Maybe, we are really looking at a "using" relationship or "has a". In which
    case just include an object of 'base' in your derived object. (I suspect
    that is what we are really looking at here.)

    Hope this helps,
    -ralph



    >Code Snippet:
    >-------------
    >
    >class Base
    >{
    >public :
    > Base(int lx = 10, int ly = 11) : x(lx), y(ly)
    > {
    >
    > }
    >
    > Base(const Base & rhs) // Copy constructor
    > {
    > x = rhs.x;
    > y = rhs.y;
    > }
    >
    > operator = (const Base & rhs) // Assignment Operator
    > {
    > x = rhs.x;
    > y = rhs.y;
    > }
    >
    > void setX(int lX) { x = lX; }
    > void setY(int lY) { y = lY; }
    >
    > int getX() const { return x; }
    > int getY() const { return y; }
    >
    >private :
    > int x;
    > int y;
    >};
    >
    >class Derived : public Base
    >{
    >public :
    > Derived(int la = 12, int lb = 13) : a(la), b(lb), Base(la + 5, lb - 5)
    > {
    >
    > }
    >
    > Derived(const Derived & rhs) // Copy constructor
    > {
    > setX(rhs.getX()); // Explicit setting of
    > setY(rhs.getY()); // Base Class parameters
    >
    > a = rhs.a;
    > b = rhs.b;
    > }
    >
    > operator=(const Derived & rhs) // Assignment Operator
    > {
    > setX(rhs.getX()); // Explicit setting of
    > setY(rhs.getY()); // Base Class parameters
    >
    > a = rhs.a;
    > b = rhs.b;
    > }
    >
    > void setA(int lA) { a = lA; }
    > void setB(int lB) { b = lB; }
    >
    >private :
    > int a;
    > int b;
    >};
    >
    >void main()
    >{
    > Derived d1, d2;
    >
    > d1.setX(21);
    > d1.setY(22);
    > d1.setA(23);
    > d1.setB(24);
    >
    >
    > // It will call only Assignment Operator of Derived Class alone,
    > // not of Base Class, because of this the Base Class members
    > // won't be set properly. Hence, to set the parameters properly
    > // in Base Class, we need to call explicit set() member functions
    > // of Base Class in the Derived Class Assignment Operator , Abi
    > // Is there any way to avoid this explicit setting in Derived Class?
    >
    > d2 = d1;
    >
    > // It will call only Copy Constructor of Derived Class alone,
    > // not of Base Class, because of this Base Class members won't
    > // be set properly. Hence, to set the parameters properly in
    > // Base Class, we need to call explicit set() member functions
    > // of Base Class in the Derived Class Copy Constructor, Abi
    > // Is there any way to avoid this explicit setting in Derived Class?
    >
    > Derived d3 = d1;
    >
    >}



  3. #3
    James Curran Guest

    Re: To Access Base class Copy Constructor/Assignment Operator

    Derived:: Derived(const Derived & rhs) // Copy constructor
    : Base(rhs), a(rhs.a), b(rhs.b)
    {
    }


    Derived::operator=(const Derived & rhs) // Assignment Operator
    {
    Base::operator=(rhs);
    a = rhs.a;
    b = rhs.b;
    }



    --
    Truth,
    James Curran
    www.NovelTheory.com (Personal)
    www.NJTheater.com (Professional)
    www.aurora-inc.com (Day job)


    "Abi" <abi.gabriel@celstream.com> wrote in message
    news:3d3c9597$1@10.1.10.29...
    >
    > Hi,
    >
    > How can we able to access Base class Copy Constructor while calling

    Derived
    > class Copy Constructor? Or How can we access Base Class Assignment

    Operator
    > while calling Derived Class Assignment Operator? Any suggestions?
    >
    > Regards,
    >
    > Abi Gabriel
    >
    > Code Snippet:
    > -------------
    >
    > class Base
    > {
    > public :
    > Base(int lx = 10, int ly = 11) : x(lx), y(ly)
    > {
    >
    > }
    >
    > Base(const Base & rhs) // Copy constructor
    > {
    > x = rhs.x;
    > y = rhs.y;
    > }
    >
    > operator = (const Base & rhs) // Assignment Operator
    > {
    > x = rhs.x;
    > y = rhs.y;
    > }
    >
    > void setX(int lX) { x = lX; }
    > void setY(int lY) { y = lY; }
    >
    > int getX() const { return x; }
    > int getY() const { return y; }
    >
    > private :
    > int x;
    > int y;
    > };
    >
    > class Derived : public Base
    > {
    > public :
    > Derived(int la = 12, int lb = 13) : a(la), b(lb), Base(la + 5, lb - 5)
    > {
    >
    > }
    >
    > Derived(const Derived & rhs) // Copy constructor
    > {
    > setX(rhs.getX()); // Explicit setting of
    > setY(rhs.getY()); // Base Class parameters
    >
    > a = rhs.a;
    > b = rhs.b;
    > }
    >
    > operator=(const Derived & rhs) // Assignment Operator
    > {
    > setX(rhs.getX()); // Explicit setting of
    > setY(rhs.getY()); // Base Class parameters
    >
    > a = rhs.a;
    > b = rhs.b;
    > }
    >
    > void setA(int lA) { a = lA; }
    > void setB(int lB) { b = lB; }
    >
    > private :
    > int a;
    > int b;
    > };
    >
    > void main()
    > {
    > Derived d1, d2;
    >
    > d1.setX(21);
    > d1.setY(22);
    > d1.setA(23);
    > d1.setB(24);
    >
    >
    > // It will call only Assignment Operator of Derived Class alone,
    > // not of Base Class, because of this the Base Class members
    > // won't be set properly. Hence, to set the parameters properly
    > // in Base Class, we need to call explicit set() member functions
    > // of Base Class in the Derived Class Assignment Operator , Abi
    > // Is there any way to avoid this explicit setting in Derived Class?
    >
    > d2 = d1;
    >
    > // It will call only Copy Constructor of Derived Class alone,
    > // not of Base Class, because of this Base Class members won't
    > // be set properly. Hence, to set the parameters properly in
    > // Base Class, we need to call explicit set() member functions
    > // of Base Class in the Derived Class Copy Constructor, Abi
    > // Is there any way to avoid this explicit setting in Derived Class?
    >
    > Derived d3 = d1;
    >
    > }




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center