-
March 22nd, 2016, 11:25 PM
#1
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
-
March 22nd, 2016, 11:56 PM
#2
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?
-
March 23rd, 2016, 03:59 AM
#3
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)
-
March 28th, 2016, 03:13 PM
#4
Re: How to make a short form of copy constructor in the inherited class?
Originally Posted by lucky6969b
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.
Originally Posted by lucky6969b
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.
Originally Posted by lucky6969b
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.
-
March 31st, 2016, 10:20 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|