473,396 Members | 1,907 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,396 software developers and data experts.

Is it a good idea to implement constructors with assignment operator?

Hello. Sometimes I see that some class constructors were implemented
with assignment operator and I wanna know whether it is safe to do
that or not?

Aug 3 '07 #1
5 1411
Weihui Shen wrote:
Hello. Sometimes I see that some class constructors were implemented
with assignment operator and I wanna know whether it is safe to do
that or not?
An example of code you're referring to would really help.

My crystal ball tells me no.
Aug 3 '07 #2

Weihui Shen <sw****@gmail.comwrote in message...
Hello. Sometimes I see that some class constructors were implemented
with assignment operator and I wanna know whether it is safe to do
that or not?
Are you asking about 'initialization lists'?

class Sex{
int x, y;
public:
Sex() : x( 0 ), y( 0 ){}
Sex( int u, int v ) : x( u ), y( v ){}
};
Not only safe, it's the best way to do it (unless you have good cause not
to).

Or, did you mean this:

class Sex{
int x, y;
public:
Sex(){ x= 0 ; y= 0; }
Sex( int u, int v ){ x= u; y= v; }
};
If you need to. It's safe.

Or, did you mean this:

class Sex2{ public:
int x, y;
Sex2& operator=( Sex2 const &sx){
x = sx.x;
y = sx.y;
return *this;
}
};
It's safe.

Otherwise, show what you mean.

FAQ http://www.parashift.com/c++-faq-lite

--
Bob R
POVrookie
Aug 3 '07 #3
On 8 3 , 2 40 , "BobR" <removeBadB...@worldnet.att.netwrote:
Weihui Shen <swl...@gmail.comwrote in message...
Hello. Sometimes I see that some class constructors were implemented
with assignment operator and I wanna know whether it is safe to do
that or not?

Are you asking about 'initialization lists'?

class Sex{
int x, y;
public:
Sex() : x( 0 ), y( 0 ){}
Sex( int u, int v ) : x( u ), y( v ){}
};
Not only safe, it's the best way to do it (unless you have good cause not
to).

Or, did you mean this:

class Sex{
int x, y;
public:
Sex(){ x= 0 ; y= 0; }
Sex( int u, int v ){ x= u; y= v; }
};
If you need to. It's safe.

Or, did you mean this:

class Sex2{ public:
int x, y;
Sex2& operator=( Sex2 const &sx){
x = sx.x;
y = sx.y;
return *this;
}
};
It's safe.

Otherwise, show what you mean.

FAQ http://www.parashift.com/c++-faq-lite

--
Bob R
POVrookie
Sorry for my obscure question.
Please consider the following code:

class A {
public:
A(int a) {
*this = a; // call A::operator=(int), it's safe?
}
A& operator =(int b) {
a = b;
return *this;
}
private:
int a;
};

I mean that calling A's assignment operator in A's constructor.

Aug 3 '07 #4
class A {
public:
A(int a) {
*this = a; // call A::operator=(int), it's safe?
May be I should write this ctor like this:

A(int x) {
*this = x;
}

Aug 3 '07 #5
On Aug 3, 9:16 am, Weihui Shen <swl...@gmail.comwrote:
On 8 3 , 2 40 , "BobR" <removeBadB...@worldnet.att.netwrote:
Weihui Shen <swl...@gmail.comwrote in message...
Hello. Sometimes I see that some class constructors were implemented
with assignment operator and I wanna know whether it is safe to do
that or not?
[...]
Please consider the following code:
class A {
public:
A(int a) {
*this = a; // call A::operator=(int), it's safe?
}
A& operator =(int b) {
a = b;
return *this;
}
private:
int a;
};
I mean that calling A's assignment operator in A's constructor.
As a general rule, no. In general, an assignment operator can
only be called on a fully constructed object.

The usual idiom, in fact, is the reverse: use the copy
constructor to implement assignment. This is often associated
with a shallow swap, to give something like:

A&
A::operator=(
A const& other )
{
A tmp( other ) ;
tmp.swap( *this ) ; // but ONLY if swap is shallow!!!
return *this ;
}

The idea, of course, is that any operations that might fail will
have taken place before any modification to the object.

--
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
Aug 3 '07 #6

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

Similar topics

1
by: Russ Ford | last post by:
Hi all, I'm trying to get inheritance and constructors clear in my head (and in my code). I have the following inheritance situation (all derivations public): A is the base class B is...
12
by: Bryan Bullard | last post by:
i have a class that contains a private pointer to a system resource. i'm quite sure that i will need to have a user defined copy-constructor to do a deep copy. can someone put me in the right...
9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
2
by: Birt | last post by:
My understanding about defining your own copy and assignment constructors is whenever there is a member of pointer type. Is this true or is there any exception to this"rule"? How about when you...
2
by: John Ratliff | last post by:
What are the ramifications of creating a private copy constructor? Say I didn't want any copies to be created, say if I had a singleton for example. Should I make the copy constructor public...
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
8
by: rKrishna | last post by:
I was trying to understand the real need for copy constructors. From literature, the main reason for redfinition of copy constructor in a program is to allow deep copying; meaning ability to make...
21
by: Michael Hull | last post by:
Hi, I remember from somewhere reading that inlining constructors is a 'BadThing', but now can't seem to find the original source. I can't however thing of a reason why it would be for simple...
23
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
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,...

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.