C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Default copy operator on derivated classes

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Alexander Tumarov
Guest





PostPosted: Thu Jul 22, 2004 10:59 pm    Post subject: Default copy operator on derivated classes Reply with quote



I tried to run next code compiles with g++

//--------------------------------
#include <stdio.h>

class B
{
public:
B& operator=(const B &b)
{
printf("BBBBBBBB n");
return *this;
};
};

class C
{
public:
C& operator=(const C &c)
{
printf("CCCCCCC n");
return *this;
};
};

class D : public B,public C
{
public:
D& operator=(const C &c)
{
printf("DDDDDD n");
return *this;
};
};



int main(int argc, char* argv[])
{
C c1;
D d1,d2;

d1=d2;
d1=c1;

return 0;
}

// --------------------------

And got next output

BBBBBBBB
CCCCCCC
DDDDDD

The question is why compiler builds call to both
B& operator=(const B &b)
C& operator=(const C &c)

on the line
d1=d2;
???

A much as I remember on the case of missing copy operator the compiler
should build default one that performs bit-to-bit copy and not call base
copy operators. Am I wrong?
Can anybody point me to the specific paragraph in the ANSI standard?

Thank you.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Victor Bazarov
Guest





PostPosted: Fri Jul 23, 2004 2:23 am    Post subject: Re: Default copy operator on derivated classes Reply with quote



"Alexander Tumarov" <pure-magic (AT) narod (DOT) ru> wrote...
Quote:
I tried to run next code compiles with g++

//--------------------------------
#include
class B
{
public:
B& operator=(const B &b)
{
printf("BBBBBBBB n");
return *this;
};
};

class C
{
public:
C& operator=(const C &c)
{
printf("CCCCCCC n");
return *this;
};
};

class D : public B,public C
{
public:
D& operator=(const C &c)
{
printf("DDDDDD n");
return *this;
};
};



int main(int argc, char* argv[])
{
C c1;
D d1,d2;

d1=d2;
d1=c1;

return 0;
}

// --------------------------

And got next output

BBBBBBBB
CCCCCCC

These two are for the d1=d2 expression.

Quote:
DDDDDD

This is your overloaded assignment.

Quote:

The question is why compiler builds call to both
B& operator=(const B &b)
C& operator=(const C &c)

on the line
d1=d2;
???

A much as I remember on the case of missing copy operator the compiler

Where do you remember that from?

Quote:
should build default one that performs bit-to-bit copy and not call base
copy operators. Am I wrong?

Yes, you are. Not bit-to-bit copy. Member-wise _assignment_. IOW,
the compiler creates code that is semantically equivalent to calling all
base classes' and members' assignment operators. That's what you get,
the default operator= called the B::operator= and C::operator= for the
respective base classes of D.

Quote:
Can anybody point me to the specific paragraph in the ANSI standard?

12.8, paragraph 13.

Victor


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Andrey Tarasevich
Guest





PostPosted: Fri Jul 23, 2004 2:24 am    Post subject: Re: Default copy operator on derivated classes Reply with quote



Alexander Tumarov wrote:
Quote:

A much as I remember on the case of missing copy operator the compiler
should build default one that performs bit-to-bit copy and not call base
copy operators. Am I wrong?

You are wrong. There's no such thing at bit-to-bit copying in C++. When
compiler defines implicit copy assignment operator for some class type,
it is required to perform memberwise copying of this class' subobjects
in accordance with their own copy semantics. For subobjects of class
type their copy assignment operators are called, for example. See 12.8/13.

--
Best regards,
Andrey Tarasevich


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Antoun Kanawati
Guest





PostPosted: Fri Jul 23, 2004 9:51 am    Post subject: Re: Default copy operator on derivated classes Reply with quote

Andrey Tarasevich wrote:

Quote:
Alexander Tumarov wrote:

A much as I remember on the case of missing copy operator the compiler
should build default one that performs bit-to-bit copy and not call base
copy operators. Am I wrong?

You are wrong. There's no such thing at bit-to-bit copying in C++. When
compiler defines implicit copy assignment operator for some class type,
it is required to perform memberwise copying of this class' subobjects
in accordance with their own copy semantics. For subobjects of class
type their copy assignment operators are called, for example. See 12.8/13.

Member-wise copy was not always the way of C++. If I recall correctly,
many things were different in the very old days.

C++ has been around for at least 15 years, and was not always as the
standard describes it.
--
Antoun Kanawati
[email]antounk.at (AT) comcast (DOT) dot.net[/email]
[remove .dot and .at before use]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Fri Jul 23, 2004 2:17 pm    Post subject: Re: Default copy operator on derivated classes Reply with quote

In article <c6vsm5$166a$1 (AT) news (DOT) f.de.plusline.net>, Alexander Tumarov
<pure-magic (AT) narod (DOT) ru> writes
Quote:
The question is why compiler builds call to both
B& operator=(const B &b)
C& operator=(const C &c)

on the line
d1=d2;
???

A much as I remember on the case of missing copy operator the compiler
should build default one that performs bit-to-bit copy and not call base
copy operators. Am I wrong?
Can anybody point me to the specific paragraph in the ANSI standard?

Yes, you are wrong. Bitwise copying was removed from the language many
years ago. We now use memberwise copying (see 12.8, and in particular
para 8)


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Fri Jul 23, 2004 3:40 pm    Post subject: Re: Default copy operator on derivated classes Reply with quote

In article <CJ%Lc.147233$JR4.30264@attbi_s54>, Antoun Kanawati
<antounk (AT) comcast (DOT) net> writes
Quote:
C++ has been around for at least 15 years, and was not always as the
standard describes it.

Well the first edition of 'The C++ Programming Language' has a prefaced
dated 1985 which suggests that C++ is almost 21.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Antoun Kanawati
Guest





PostPosted: Sat Jul 24, 2004 12:05 am    Post subject: Re: Default copy operator on derivated classes Reply with quote

Francis Glassborow wrote:

Quote:
In article <CJ%Lc.147233$JR4.30264@attbi_s54>, Antoun Kanawati
[email]antounk (AT) comcast (DOT) net[/email]> writes

C++ has been around for at least 15 years, and was not always as the
standard describes it.

Well the first edition of 'The C++ Programming Language' has a prefaced
dated 1985 which suggests that C++ is almost 21.

2004 - 1985 = 19, which is somewhat different from 21.

My first contact with C++ was either in 1986 or 1987, when I bought the
purple book on impulse. It wasn't clear to me back then whether the
language was used outside AT&T. It wasn't until late 1988 that I ran
into a real C++ compiler (good old CFront) after I switched colleges.
At that time, the language version was still 1.x (I forget what x was),
strange things happened to "this" in constructors, the word
"member-wise" was not known, etc... I don't think we had multiple
inheritance back then, but I could be wrong.

So, I rounded "late 1988" to 1989, and figure "at least" 15 years; a
number that does not exclude 19 or 21.

I admit that I do not the exact date of birth of C++. Luckily, there
are still some living people who know these things, and we don't have
to go digging for archeological evidence Smile Smile
--
Antoun Kanawati
[email]antounk.at (AT) comcast (DOT) dot.net[/email]
[remove .dot and .at before use]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Sun Jul 25, 2004 12:24 am    Post subject: Re: Default copy operator on derivated classes Reply with quote

In article <NOeMc.153337$JR4.139837@attbi_s54>, Antoun Kanawati
<antounk (AT) comcast (DOT) net> writes
Quote:
Francis Glassborow wrote:

In article <CJ%Lc.147233$JR4.30264@attbi_s54>, Antoun Kanawati
[email]antounk (AT) comcast (DOT) net[/email]> writes

C++ has been around for at least 15 years, and was not always as the
standard describes it.

Well the first edition of 'The C++ Programming Language' has a prefaced
dated 1985 which suggests that C++ is almost 21.

2004 - 1985 = 19, which is somewhat different from 21.

It takes time to write a book and get it published Smile I just checked in
'The Design and Evolution of C++' There it lists August 1983 as the date
for the first implementation of C++. So, yes, C++ has been in use for 21
years some time next month. The name is listed as December 1983.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.