Base and Derived class copy constructor help.

This is a discussion on Base and Derived class copy constructor help. within the C++ Programming forums, part of the General Programming Boards category; I have Code: class Base { int a; public: Base(); Base(int); Base(const Base& b); //copy constructor }; Base::Base(const Base& b){ ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    51

    Base and Derived class copy constructor help.

    I have
    Code:
    class Base {
    int a;
    
    public:
    Base();
    Base(int);
    Base(const Base& b);  //copy constructor 
    };
    
    Base::Base(const Base& b){
    a=b.a;}
    
    
    
    class Derived public: Base{
    int x;
    
    public:
    Derived();
    Derived(int);
    Derived(const Derived& d); //copy constructor
    };
    
    
    Derived::Derived(const Derived& d) :Base(d){   //<--MY QUESTION HERE.
    x=d.x;
    }
    how the Base copy constructor will be executed?
    d is a reference to Derived class. It is not a reference to a Base class.
    Any help will be appreciated.
    Last edited by byebyebyezzz; 08-04-2011 at 06:12 AM.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Code:
    Derived::Derived(const Derived& d) :Base(d){
    'd' will be implicitly converted to its base class 'Base'
    I never put signature, but I decided to make an exception.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,539
    With public inheritence, an object of derived class IS an object of the base class. That is why, as kmdv said, a reference to derived can be implicitly converted to a reference to base. (The reverse is not true).

    Incidentally, if all you're doing in a copy constructor is copying all members by value, you do not need to declare or define the copy constructor. The compiler will supply one for you that does the right thing (including initialising bases). A similar comment applies for the assignment operator.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    there are more variables in the two classes.
    one of them is a char*.

    Thx for clearing it up for me

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    Consider using std::string or smart pointers then.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 07-25-2008, 05:37 AM
  2. Replies: 9
    Last Post: 06-20-2008, 03:41 AM
  3. Replies: 18
    Last Post: 11-21-2007, 10:38 AM
  4. Calling constructor of the base class of a derived class..
    By CaptainPenguin in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 01:47 PM
  5. Replies: 1
    Last Post: 12-11-2002, 10:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21