473,320 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Reference assignment through base class reference

Dear experts,
#include <stdio.h>

class Base
{
};
class Der1:public Base
{
};
class Der2:public Base
{
};

int main()
{
Der1 d1;Der2 d2;
Base& b1 = d1;
Base& b2 = d2;
b1 = b2;
}

Above code gets compiled. But I wonder what does the assignment b1 =
b2; do?If these are pointers then its understandable. But how does
above assignment behave in case of references. Does it exhibit UB?

Regards,
Siddharth

Jun 4 '07 #1
12 1891
siddhu wrote:
#include <stdio.h>
^^^^^^^^^^^^^^^^
What's that for?
>
class Base
{
};
class Der1:public Base
{
};
class Der2:public Base
{
};

int main()
{
Der1 d1;Der2 d2;
Base& b1 = d1;
Base& b2 = d2;
b1 = b2;
}

Above code gets compiled. But I wonder what does the assignment b1 =
b2; do?If these are pointers then its understandable. But how does
above assignment behave in case of references. Does it exhibit UB?
No, no UB. 'Base' subobject of 'd1' gets the same 'Base' value as
the 'Base' subobject of 'd2'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 4 '07 #2
siddhu wrote:
Dear experts,
#include <stdio.h>

class Base
{
};
class Der1:public Base
{
};
class Der2:public Base
{
};

int main()
{
Der1 d1;Der2 d2;
Base& b1 = d1;
Base& b2 = d2;
b1 = b2;
}

Above code gets compiled. But I wonder what does the assignment b1 =
b2; do?If these are pointers then its understandable. But how does
above assignment behave in case of references. Does it exhibit UB?
No UB here. Your class might not expect it though. It is commonly
referred to as "slicing". It's also usually not something that is
expected to happen in the implementation and so things can co awry.

It can be prevented by making the base class constructor "protected".
Jun 4 '07 #3
Gianni Mariani wrote:
siddhu wrote:
>Dear experts,
#include <stdio.h>

class Base
{
};
class Der1:public Base
{
};
class Der2:public Base
{
};

int main()
{
Der1 d1;Der2 d2;
Base& b1 = d1;
Base& b2 = d2;
b1 = b2;
}

Above code gets compiled. But I wonder what does the assignment b1 =
b2; do?If these are pointers then its understandable. But how does
above assignment behave in case of references. Does it exhibit UB?

No UB here. Your class might not expect it though. It is commonly
referred to as "slicing". It's also usually not something that is
expected to happen in the implementation and so things can co awry.

It can be prevented by making the base class constructor "protected".
Constructor? Not the copy assignment operator? How does slicing get
into copy assigning objects through references, by the way?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 4 '07 #4
Victor Bazarov wrote:
...
Constructor? Not the copy assignment operator? How does slicing get
into copy assigning objects through references, by the way?
My excuse is I'm sick and I'm sticking to it ...
Jun 4 '07 #5

Gianni Mariani wrote in message...
Victor Bazarov wrote:
..
Constructor? Not the copy assignment operator? How does slicing get
into copy assigning objects through references, by the way?

My excuse is I'm sick and I'm sticking to it ...
Take two aspirin and call Dr. Dobbs in the morning! <G>

--
Bob <GR
POVrookie
Jun 5 '07 #6
On Jun 4, 10:49 pm, siddhu <siddharth....@gmail.comwrote:
#include <stdio.h>
class Base
{};
class Der1:public Base
{
};
class Der2:public Base
{
};
int main()
{
Der1 d1;Der2 d2;
Base& b1 = d1;
Base& b2 = d2;
b1 = b2;
}
Above code gets compiled. But I wonder what does the
assignment b1 = b2; do?
Nothing useful. It assigns the Base part of d2 to the Base part
of d1.
If these are pointers then its understandable. But how does
above assignment behave in case of references. Does it exhibit UB?
Not directly. Of course, class invariants are likely broken,
but that's not a language problem---the language specifies a
behavior here.

In general, assignment doesn't work well with polymorphic
objects. Typically, if a class is designed to be used as a
base, it will have a private assignment operator, so that such
code will be illegal.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 5 '07 #7
James Kanze wrote:
[..]
In general, assignment doesn't work well with polymorphic
objects. Typically, if a class is designed to be used as a
base, it will have a private assignment operator, so that such
code will be illegal.
*Private* assignment operator? Not *protected*? Just trying to
clarify...

Thanks!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 5 '07 #8
On Jun 5, 3:06 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
James Kanze wrote:
[..]
In general, assignment doesn't work well with polymorphic
objects. Typically, if a class is designed to be used as a
base, it will have a private assignment operator, so that such
code will be illegal.
*Private* assignment operator? Not *protected*? Just trying to
clarify...
Private. Why would you make it protected, if the goal is to ban
it? Alternatively, you could inherit from something like
boost::uncopiable.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 5 '07 #9
James Kanze wrote:
On Jun 5, 3:06 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>James Kanze wrote:
>>[..]
In general, assignment doesn't work well with polymorphic
objects. Typically, if a class is designed to be used as a
base, it will have a private assignment operator, so that such
code will be illegal.
>*Private* assignment operator? Not *protected*? Just trying to
clarify...

Private. Why would you make it protected, if the goal is to ban
it? Alternatively, you could inherit from something like
boost::uncopiable.
If a class is designed to be used as a base, and its assignment op
is private, how the hell are you going to copy the derived class
objects? The default copy assignment op cannot be generated, so
a user-defined has to be provided, which will have to forgo copying
of the base part, which means only the derived portion can be made
"the same as the other one", and the base class subobject will have
to stay the way it was created... That's just wrong, it meanst that
if I have

Derived d1(some_params), d2(some_other_params); // different
assert(d1 != d2);
d1 = d2;
assert(d1 == d2); // how is that going to be true if '=='
// compares the base part as well?

So, either _in_general_ comparing objects of derived class should
NOT involve comparing base class subobjects (which is just wrong
*in general*), or there should be a way to assign bass class
subobjects in a derived class copy assignment operator, which
means the copy assignment operator should be probably *protected*
_in_general_ if the class is intended to serve as a base.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 5 '07 #10
On Jun 5, 7:27 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
James Kanze wrote:
On Jun 5, 3:06 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
James Kanze wrote:
[..]
In general, assignment doesn't work well with polymorphic
objects. Typically, if a class is designed to be used as a
base, it will have a private assignment operator, so that such
code will be illegal.
*Private* assignment operator? Not *protected*? Just trying to
clarify...
Private. Why would you make it protected, if the goal is to ban
it? Alternatively, you could inherit from something like
boost::uncopiable.
If a class is designed to be used as a base, and its assignment op
is private, how the hell are you going to copy the derived class
objects?
You're not going to assign them. (A protected copy constructor
is sometimes appropriate. Although in most cases, if a class is
designed to be used as a base class, it has no data to be
copied.)
The default copy assignment op cannot be generated, so
a user-defined has to be provided, which will have to forgo copying
of the base part, which means only the derived portion can be made
"the same as the other one", and the base class subobject will have
to stay the way it was created...
No. The derived class doesn't have to do anything. Assignment
is just not part of the contract, and thus not supported.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 6 '07 #11
James Kanze wrote:
On Jun 5, 7:27 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>James Kanze wrote:
>>On Jun 5, 3:06 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
James Kanze wrote:
[..]
In general, assignment doesn't work well with polymorphic
objects. Typically, if a class is designed to be used as a
base, it will have a private assignment operator, so that such
code will be illegal.
>>>*Private* assignment operator? Not *protected*? Just trying to
clarify...
>>Private. Why would you make it protected, if the goal is to ban
it? Alternatively, you could inherit from something like
boost::uncopiable.
>If a class is designed to be used as a base, and its assignment op
is private, how the hell are you going to copy the derived class
objects?

You're not going to assign them. (A protected copy constructor
is sometimes appropriate. Although in most cases, if a class is
designed to be used as a base class, it has no data to be
copied.)
"In most cases" is no substitute for "in general". In general even
a class designed to be a base class _may_ contain data [to be copied].
So, in my book, *in general* the copy assignment operator cannot be
private, it has to be protected.
>The default copy assignment op cannot be generated, so
a user-defined has to be provided, which will have to forgo copying
of the base part, which means only the derived portion can be made
"the same as the other one", and the base class subobject will have
to stay the way it was created...

No. The derived class doesn't have to do anything. Assignment
is just not part of the contract, and thus not supported.
WHAT contract are you talking about? "Assignable" is a requirement
for an object to be stored in a standard container, for example.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 6 '07 #12
James Kanze wrote:
....
>
No. The derived class doesn't have to do anything. Assignment
is just not part of the contract, and thus not supported.
Based on the OP code, I think it was clear that he wanted his classes to
support assignment. The OP just wasn't sure what slicing would do. The
proposal to "protect" the base class assignment operator was simply a
suggestion as a way to disallow assignment other than from a derived class.
Jun 6 '07 #13

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

Similar topics

9
by: Rick N. Backer | last post by:
I have an abstract base class that has char* members. Is an assignment operator necessary for this abstract base class? Why or why not? Thanks in advance. Ken Wilson Amer. Dlx. Tele,...
17
by: N4M | last post by:
Dear, Suppose I have a Protocol class, in which I need also an assignment operator =(). class B { ..... virtual B& operator=(const B& rb) =0; }; Now in some derived class D: public B, how...
7
by: Mr. Ed | last post by:
I have a base class which has about 150 derived classes. Most of the derived classes are very similar, and many don't change the base class at all. All the derived classes have a unique factory...
10
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in...
11
by: anongroupaccount | last post by:
What measures should be taken to avoid this sort of thing? class Base { }; class Derived1 : public Base { private: int i, j, k;
8
by: junw2000 | last post by:
Hi, My textbook says that: "Once defined, a reference cannot be reassigned because it is an alias to its target. What happens when you try to reassign a reference turns out to be the assignment...
7
by: vj | last post by:
Hi Friends, I was going through a C++ reference book when this rule caught my eye: -->Assignment operator '=' is not inherited by the sub class. I cannot figure out why this rule has being...
2
by: Henrik Goldman | last post by:
Hi, Lets say you have class A which holds all data types as private members. Class B then inherits from A and does *only* include a set of public functions which uses A's existing functions for...
1
by: PeterAPIIT | last post by:
What is template template arguments and template typename arguments ? The reason i write in policy based design is because this is the requirement of the assignment. My code so far: ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.