473,856 Members | 1,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using assignment operator

Hello Experts!

Assume I have a class called SphereClass as the base class and a class
called BallClass that is derived from the SphereClass. The copy constructor
initialize the left hand object in this case object b2 below. It also
initialize subobject from class SphereClass.

The BallClass copy constructor looks like this.
BallClass::Ball Class(const BallClass& bc) : SphereClass(bc)

int main()
{
BallClass b1("ball");

BallClass b2(b1);
return 0;
}

Now to my question when you use assignment for example
b2 = b1
how do you initialize the subobject part from class SphereClass. When I used
the copy constructor I initialized the subobject part by using this
statement SphereClass(bc)

Many thanks!

//Tony
Aug 9 '05 #1
14 2365
Tony Johansson wrote:
Assume I have a class called SphereClass as the base class and a class
called BallClass that is derived from the SphereClass. The copy constructor
initialize the left hand object in this case object b2 below. It also
initialize subobject from class SphereClass.

The BallClass copy constructor looks like this.
BallClass::Ball Class(const BallClass& bc) : SphereClass(bc)

int main()
{
BallClass b1("ball");

BallClass b2(b1);
return 0;
}

Now to my question when you use assignment for example
b2 = b1
how do you initialize the subobject part from class SphereClass.
When you use assignment, nothing is _initialised_. Everything is
_assigned_. What example of the copy assignment operator does your
favourite C++ book give? What do you not understand in that example?

BTW, what do you not like about the compiler-generated copy assignment
operator? Does it not work for you for some reason?
When I used
the copy constructor I initialized the subobject part by using this
statement SphereClass(bc)


We noticed. Initialiser lists are a feature of _constructors_o nly_.

V
Aug 9 '05 #2
Tony, Does your base class SphereClass have a copy constructor? If so,
then your memberwise initialization statement SphereClass(bc) is okay.
Otherwise if your base class does not have a copy constructor, default
memberwise initialization is recursively applied to the base class.

Aug 9 '05 #3
Victor, Compiler-generated copy assignment operators are not always the
best choice. Tony has not provided us with a full description of his
class. If his base class contains any members which are pointers to
dynamically allocated memory, I would not use the compiler-generated
copy assignment operators. Instead, I would write my own as Tony
suggests.

Aug 9 '05 #4
Frank Chang wrote:
Victor, Compiler-generated copy assignment operators are not always the
best choice. Tony has not provided us with a full description of his
class. If his base class contains any members which are pointers to
dynamically allocated memory, I would not use the compiler-generated
copy assignment operators. Instead, I would write my own as Tony
suggests.


Frank, my question wasn't to suggest that compiler-generated assignment
operators are the best choice always. My question was to make Tony think
about it and give _his_ answer. Thanks for chiming in, though.

V
Aug 9 '05 #5
Tony Johansson wrote:

Hello Experts!
Now to my question when you use assignment for example
b2 = b1
how do you initialize the subobject part from class SphereClass.


By calling the assignment operator for the base class.
In your case, it could be done eg by:

BallClass& BallClass::oper ator=( const BallClass& rhs )
{
SphereClass::op erator=( rhs );

// do whatever is necc. additionally for a BallClass object

return *this;
}

As others have pointed out: You need to work on your terminologie.
Especially the distinction between assignment and initialization.
And what you called 'the statement' is known as initializer list

--
Karl Heinz Buchegger
kb******@gascad .at
Aug 9 '05 #6
Frank Chang wrote:

Victor, Compiler-generated copy assignment operators are not always the
best choice.
If they do the right thing, then yes they are.
Tony has not provided us with a full description of his
class. If his base class contains any members which are pointers to
dynamically allocated memory, I would not use the compiler-generated
copy assignment operators.


:-)
Especially in this case I would let the compiler do it.
(Note: You wrote: if the *base* class ...
In the derived class I don't care what the base class does. The compiler
generated op= of the *derived* class, does the right thing: it calls the
op= of the base class and that's all there needs to be such that the *base*
class op= gets a chance to get it right.

--
Karl Heinz Buchegger
kb******@gascad .at
Aug 9 '05 #7
Karl Heinz Buchegger writes:

"In the derived class I don't care what the base class does. The
compiler
generated op= of the *derived* class, does the right thing: it calls
the
op= of the base class and that's all there needs to be such that the
*base* class op= gets a chance to get it right. "

Suppose we have the scenario b2 = b1 where b1 and b2 are derived
objects of type BallObject. Suppose the base class of BallObject,
SphereClass has a member variable which is a pointer to an object of
class Foo. If we use the compiler generated assignment operator for
the base class SphereClass, then in most compilers it will make a
bitwise copy of the pointer to an object of Foo. Now, suppose the
virtual destructors for b1 and b2 get called eventually, then you will
be double deleting the Foo pointer member of b1 and b2. Please name a
widely used compiler which avoids this problem.

Aug 9 '05 #8
Frank Chang wrote:
Karl Heinz Buchegger writes:

"In the derived class I don't care what the base class does. The
compiler
generated op= of the *derived* class, does the right thing: it calls
the
op= of the base class and that's all there needs to be such that the
*base* class op= gets a chance to get it right. "

Suppose we have the scenario b2 = b1 where b1 and b2 are derived
objects of type BallObject. Suppose the base class of BallObject,
SphereClass has a member variable which is a pointer to an object of
class Foo. If we use the compiler generated assignment operator for
the base class SphereClass,
I think you don't understand what Karl Heinz is saying: BallObject should
not care whether SphereClass has its assignment operator compiler-created
or user-defined. Now, if SphereClass has some dynamic memory management
to accomplish, BallObject doesn't have to, or, more often, shouldn't, care
about that when using SphereClass' operator=.
then in most compilers it will make a
bitwise copy of the pointer to an object of Foo. Now, suppose the
virtual destructors for b1 and b2 get called eventually, then you will
be double deleting the Foo pointer member of b1 and b2. Please name a
widely used compiler which avoids this problem.


Please reread Karl's post.

V
Aug 9 '05 #9
I read Karl's post before I posted and I understand what he is saying.
However, in proper C++ design, all C++ classes, including base
classes, must adhere to a contract and implement the canonical
operators such as copy constructor, assignment operator, destructor
correctly. Otherwise , all subsequently derived classes from these base
classes, no matter how cleverly designed or implemented, will exhibit
erratic run time behavior. The ARM by Bjarne Strousoup discusses this
issue in detail.

Aug 9 '05 #10

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

Similar topics

8
17888
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. Why can't the = (assignment) operator be overloaded as a friend function ? I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it said the following :
5
4840
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant to be? Am I not suppose not to have any constant data member if I am going to have the assignment operator working for the class? Or am I missing something here and there is something I need to learn about? Clear, easy to understand...
10
2669
by: Christopher Benson-Manica | last post by:
Why can't I use a class destructor in a using declaration: using MyClass::~MyClass; ? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
9
3073
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, Gary Moore LP, LP Faded DC, Jeff Beck Strat, Morgan OM Acoustic,
7
4679
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++ copyconstructor1.cpp #copyconstructor1.cpp: In function `int main()': #copyconstructor1.cpp:86: no match for `sample& = sample' operator #copyconstructor1.cpp:53: candidates are: sample sample::operator=(sample&) ...
3
3413
by: Ondrej Spanel | last post by:
I defined operator = using template member function for a template class. However compiler failed to recognize it is defined and created its own version (which was member-wise copy, and a result was disastrous and hard to debug bug, as pointer was copied). Adding explicit operator = helped. See code snip below. I am not sure if this compiler behaviour is standard conformant or not. Any thoughts?
10
2610
by: Christian Christmann | last post by:
Hi, how do I define an assignment operator which is supposed to copy all member attributes of one object to another where both objects are given as pointers? Example: CLASS_A *source = new CLASS_A; ....
11
2415
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;
13
1961
by: Daniel W | last post by:
Hi! I tried to post this to comp.lang.c.moderated but it didn't seem to go through. I've got a question about volatiles in assignment expressions. I found the following code snippet in an embedded development forum: ----
9
3832
by: George2 | last post by:
Hello everyone, I am wondering the default implementation of assignment operator (e.g. when we do not implement assignment operator in user defined class, what will be returned? temporary object? reference or const reference? deep copy or shallow copy is used in default assignment operator?)? I have the C++ Programming Book at hand, but can not find it from Index page.
0
9763
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11061
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10787
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9534
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7098
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5762
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5960
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.