CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    How to make a short form of copy constructor in the inherited class?

    let's say I have a base class called A, and a derived class B which is derived from A
    and B has a copy constructor to A,
    This copy constructor has to initialize every property in A
    Let's say A has tons of properites, and in the copy constructor of B
    I have to type in all the properties which is quite time-consuming.
    I wonder if there are any short form to copy construct an object of B which
    has a base class of A?

    Thanks in advance
    Jack

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How to make a short form of copy constructor in the inherited class?

    Perhaps you should break A up into smaller classes that models one thing and models it well?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to make a short form of copy constructor in the inherited class?

    Perhaps you could give an example.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: How to make a short form of copy constructor in the inherited class?

    Quote Originally Posted by lucky6969b View Post
    This copy constructor has to initialize every property in A.
    No, that's wrong. That's A's (copy) constructor's job. By the time B's copy constructor is running, A is fully built and initialized. B cannot initialize A's fields.

    The only thing B's copy constructor has the freedom of doing is indicating which constructor for A it wants run.

    Quote Originally Posted by lucky6969b View Post
    I wonder if there are any short form to copy construct an object of B which
    has a base class of A
    Just call A's copy constructor before running B's constructor:

    Code:
    class A {
      private:
        T t;
    
      public:
        A(const A& a) : t(a.t) {}
    };
    
    class B : public A {
      public:
        B(const B& b) : A(b) {}
    };
    Again, A *must* have a constructor. If you omit which constructor you want, it will call the default constructor (if available). What you want to do is not possible if A does not provide an adequate constructor.

    Quote Originally Posted by lucky6969b View Post
    B has a copy constructor to A
    That's not a copy constructor. It's just a constructor.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to make a short form of copy constructor in the inherited class?

    If the classes need copy constructors then they will also need move constructors and copy/move assignments.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured