472,958 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

probelm on copy constructor of a derived class.

Dear All,

I am wondering how the default copy constructor of a derived class
looks like. Does it look like

class B : public A
{
B(const B& right)
: A(right)
{}
}

Ann also how to access a derived class's base object?

Thanks,

Shuisheng

Sep 26 '06 #1
8 4244
shuisheng wrote:
Dear All,

I am wondering how the default copy constructor of a derived class
looks like.
There is no "default copy constructor". You probably mean the
compiler-generated copy constructor.
Does it look like

class B : public A
{
B(const B& right)
: A(right)
{}
}
Basically, yes.
Ann also how to access a derived class's base object?
There is nothing special you need to do. The base object is just part of the
derived object.

Sep 26 '06 #2
shuisheng wrote:
I am wondering how the default copy constructor of a derived class
looks like. Does it look like

class B : public A
{
B(const B& right)
: A(right)
{}
}
;

I'd probably force the argument (since 'A' can have another c-tor
that takes a 'B'):

B(const B& right)
: A(static_cast<const A&>(right))
{
}
Ann also how to access a derived class's base object?
To do what? Members that are public and protected are accessible,
unless their names are hidden, in which case you have to help the
compiler resolve them by using qualification (A::). The object
itself is part of '*this' object, and can be obtained using the
standard conversions from derived to base (pointer or reference).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 26 '06 #3
Rolf Magnus wrote:
shuisheng wrote:
>Dear All,

I am wondering how the default copy constructor of a derived class
looks like.

There is no "default copy constructor". You probably mean the
compiler-generated copy constructor.
May I ask what the benefit of such linguistic purity is? Also, I think, the
term "compiler-generated copy constructor" is not better supported by the
standard than the term "default copy constructor". For starters, there is
no requirement that the implementation be a compiler. Thus, at best there
is a "copy constructor with predefined behavior (given by the standard)
that will be generated by the implementation unless there is a
user-declared copy constructor". I think, this copy constructor can
justifiedly be referred to as a "default copy constructor" since it is
present unless the user overrides the default behavior by declaring a copy
constructor to replace it. Also note that the standard does not define the
term "default copy constructor" for any other meaning. So using it within
the C++ community as a name for the copy constructor with predefined
behavior (given by the standard) that will be generated by the
implementation unless there is a user-declared copy constructor will not
create any ambiguity.
Best

Kai-Uwe Bux
Sep 26 '06 #4
Kai-Uwe Bux wrote:
Rolf Magnus wrote:
>shuisheng wrote:
>>Dear All,

I am wondering how the default copy constructor of a derived class
looks like.
There is no "default copy constructor". You probably mean the
compiler-generated copy constructor.

May I ask what the benefit of such linguistic purity is? Also, I think, the
term "compiler-generated copy constructor" is not better supported by the
standard than the term "default copy constructor".
The proper term is "implicitly defined" or "implicitly declared" copy
constructor (depending on what you are referring to).

The word "default" when used in conjuction with constructor means can
be called with NO arguments. In fact, you can have a constructor
that is both a copy constructor and a default constructor, although
the utility of it escapes me:

class X() {
X(int i); // non-default constructor
X(const X& that = X(0));
// default and copy constructor both.
};
Also note that the standard does not define the
term "default copy constructor" for any other meaning. So using it within
the C++ community as a name for the copy constructor with predefined
behavior (given by the standard) that will be generated by the
implementation unless there is a user-declared copy constructor will not
create any ambiguity.
Yes there is ambiguity. You might be referring to something above.

Further, you already have to distinguish between "Default" and
"implicitly defined" when you are talking about real default
constructors, so it would be extremely imprecise and confusing
to use the term "default" in one place for a meaning where it
is not useful another.
Sep 26 '06 #5
Ron Natalie wrote:
Kai-Uwe Bux wrote:
>Rolf Magnus wrote:
>>shuisheng wrote:

Dear All,

I am wondering how the default copy constructor of a derived class
looks like.
There is no "default copy constructor". You probably mean the
compiler-generated copy constructor.

May I ask what the benefit of such linguistic purity is? Also, I think,
the term "compiler-generated copy constructor" is not better supported by
the standard than the term "default copy constructor".

The proper term is "implicitly defined" or "implicitly declared" copy
constructor (depending on what you are referring to).
I like that term.

The word "default" when used in conjuction with constructor means can
be called with NO arguments. In fact, you can have a constructor
that is both a copy constructor and a default constructor, although
the utility of it escapes me:

class X() {
X(int i); // non-default constructor
X(const X& that = X(0));
// default and copy constructor both.
};
It's utility also escapes me. That is probably the reason that I was missing
this one.

>Also note that the standard does not define the
term "default copy constructor" for any other meaning. So using it within
the C++ community as a name for the copy constructor with predefined
behavior (given by the standard) that will be generated by the
implementation unless there is a user-declared copy constructor will not
create any ambiguity.

Yes there is ambiguity. You might be referring to something above.
Really? That seems to be a classical paper doubt. I cannot think of a
natural context where the above would be a reasonable candidate to be the
intended reading.

Anyway, I concede that the standard does indeed provide definition that
allow us to deduce a meaning for "default copy constructor"; and I also
concede that this meaning is never intended when the phrase "default copy
constructor" comes up in real life. That sure is a defect of the
phrase "default copy constructor".

Further, you already have to distinguish between "Default" and
"implicitly defined" when you are talking about real default
constructors, so it would be extremely imprecise and confusing
to use the term "default" in one place for a meaning where it
is not useful another.
You are exaggerating: I read quite a few postings using "default copy
constructor", and there was not a single instance where it was "extremely
confusing".

Although I really like the term "implicitly defined copy constructor", I
still consider "default copy constructor" an acceptable shorthand --
however, that might change when I sleep on it. My current feeling is that
nobody thinks that a default copy constructor is a default constructor.
Thanks a lot

Kai-Uwe Bux

Sep 26 '06 #6
Kai-Uwe Bux wrote:
Rolf Magnus wrote:
>shuisheng wrote:
>>Dear All,

I am wondering how the default copy constructor of a derived class
looks like.
There is no "default copy constructor". You probably mean the
compiler-generated copy constructor.

May I ask what the benefit of such linguistic purity is? Also, I think, the
term "compiler-generated copy constructor" is not better supported by the
standard than the term "default copy constructor". For starters, there is
no requirement that the implementation be a compiler. Thus, at best there
is a "copy constructor with predefined behavior (given by the standard)
that will be generated by the implementation unless there is a
user-declared copy constructor". I think, this copy constructor can
justifiedly be referred to as a "default copy constructor" since it is
present unless the user overrides the default behavior by declaring a copy
constructor to replace it. Also note that the standard does not define the
term "default copy constructor" for any other meaning. So using it within
the C++ community as a name for the copy constructor with predefined
behavior (given by the standard) that will be generated by the
implementation unless there is a user-declared copy constructor will not
create any ambiguity.
The appropriate term from the standard would be "implicitly-defined copy
constructor." I think "default copy constructor" is confusing, because a
default constructor, unlike a copy constructor, takes no arguments.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Sep 26 '06 #7

Kai-Uwe Bux wrote:
Although I really like the term "implicitly defined copy constructor", I
still consider "default copy constructor" an acceptable shorthand --
however, that might change when I sleep on it. My current feeling is that
nobody thinks that a default copy constructor is a default constructor.
As you relax this evening before going to sleep, consider that you
probably already understand well the differences between different
kinds of constructors and the circumstances in which the different
kinds might be used (by the programmer or the compiler) and which ones
the compiler might generate and when. Consequently, you are able to
think about and discuss the topic using relatively loose terminology
because you implicitly understand which form of constructor is being
talked about from the context.

Someone learning this part of C++ for the first time presumably does
not have all this background knowledge to aid their understanding.
Insisting on precise terminology can avoid introducing unnecessary
uncertainty and confusion. And if a precise terminology is to be used,
the terminology defined by the C++ standard is the right one to use.

Gavin Deane

Sep 26 '06 #8
Kai-Uwe Bux wrote:
Rolf Magnus wrote:
>shuisheng wrote:
>>Dear All,

I am wondering how the default copy constructor of a derived class
looks like.

There is no "default copy constructor". You probably mean the
compiler-generated copy constructor.

May I ask what the benefit of such linguistic purity is?
Mostly the fact that the term "default constructor" is already used in C++,
and the word "default" has a meaning that's completely different from that
in your "default copy constructor".

Sep 26 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
9
by: keith | last post by:
Hi, I've been through the FAQ-lite and can't see this mentioned, so here goes... I've got an abstract base class called Base which has no copy constructor at all. In the derived class I have...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.