call copy constructor in "=" operator

F

fourfires.d

Dear All,

I am new to c++ and when write below code that try to call copy
constructor
in "=" operator overloading, it can not compile. Can anyone point out
for
me the reason? thanks !!

class AA
{
public:

AA( AA & obj)
{
}

AA & operator=( AA & obj)
{
AA(obj);
return *this;
}

};

int main()
{

return 0;
}

pp.cc: In method `class AA & AA::eek:perator =(AA &)':
pp.cc:11: declaration of `obj' shadows a parameter
pp.cc:11: no matching function for call to `AA::AA ()'
pp.cc:6: candidates are: AA::AA(AA &)
 
A

Alf P. Steinbach

* (e-mail address removed):
I am new to c++ and when write below code that try to call copy
constructor in "=" operator overloading, it can not compile. Can
anyone point out for me the reason? thanks !!

class AA

Style: don't use all uppercase names except for macros.

{
public:

AA( AA & obj)
{
* (e-mail address removed):

Unless there is a very good reason to do something else, the argument to
the copy constructor should be a reference to const,

AA( AA const& other ) {}

AA & operator=( AA & obj)
{
AA(obj);

This would declare an object obj of type AA, _if_ type AA had a default
constructor and _if_ you didn't have a name conflict:

AA (obj);

means

AA obj;

and causes the compiler to issue
pp.cc: In method `class AA & AA::eek:perator =(AA &)':
pp.cc:11: declaration of `obj' shadows a parameter

for the name conflict, and
pp.cc:11: no matching function for call to `AA::AA ()'
pp.cc:6: candidates are: AA::AA(AA &)

for the lack of a default constructor.

Probably what you intended was

AA copy( obj );

return *this;

At this point it's too early to return; you still have to update the
object assigned to!

What you have is a copy of the assignment source, and the idiom for
using the copy constructor in this way relies on then swapping the
contents of that copy and the object assigned to,

swap( copy );

where swap is a member function that swaps the contents and is
guaranteed to not throw.

Then you can return, and as part of that the swap the destructor is
responsible for destroying e.g. dynamically allocated memory earlier
held by *this but now residing in copy.
 
N

n2xssvv g02gfr12930

Dear All,

I am new to c++ and when write below code that try to call copy
constructor
in "=" operator overloading, it can not compile. Can anyone point out
for
me the reason? thanks !!

class AA
{
public:

AA( AA & obj)
{
}

AA & operator=( AA & obj)
{
AA(obj);
return *this;
}

};

int main()
{

return 0;
}

pp.cc: In method `class AA & AA::eek:perator =(AA &)':
pp.cc:11: declaration of `obj' shadows a parameter
pp.cc:11: no matching function for call to `AA::AA ()'
pp.cc:6: candidates are: AA::AA(AA &)
That is not generally a good idea because a constructor
should not have to clean up any dynamically allocated
memory currently in use whereas the operator =() should
do. Hence your code could develop memory leaks and
GP faults!!

JB
 
J

Jakob Bieling

Dear All,

I am new to c++ and when write below code that try to call copy
constructor

You cannot call constructors. They are called for you when you
create an object .. and *only* when you create an object. When you do
assignment, you do not create anything. Maybe this helps in
understanding why you cannot call the copy constructor.

Obviously you are trying to reduce duplicate code. In this case, it
is usually a good idea to create little private helper functions that do
the job. My preference: destroy, create and copy. Your destructor calls
destroy, default constructor calls create, copy constructor calls copy
and the assignment operator first calls destroy and then copy etc.

hth
 
F

fourfires.d

Thanks All for the detailed explaination,
do you mean I can not call a constructor explicitly?

I've changed to use "this " to try to call constructor AA but failed
(but i think destructor can be called explicitly),

AA & operator=( AA & obj)
{
this->AA(obj);
return *this;
}


pp.cc: In method `class AA & AA::eek:perator =(AA &)':
pp.cc:11: calling type `AA' like a method

Thanks!
 
I

Ivan Vecerina

: Thanks All for the detailed explaination,
: do you mean I can not call a constructor explicitly?
:
: I've changed to use "this " to try to call constructor AA but failed
: (but i think destructor can be called explicitly),
:
: AA & operator=( AA & obj)
: {
: this->AA(obj);
: return *this;
: }

It would be possible to re-construct the object (by using
placement-new), but constructing an object twice will lead
to undefined behavior.
I have also seen programmers try first force the destruction
of the object ( this->~AA() ) before copy-constructing it,
but then in many cases it is impossible to provide basic
exception safety, and the legality of it remains uncertain.

If your class is simple enough for any of the bad tricks above
to work, you are better off in any case to implement the
copy-constructor itself in terms of the assignment operator:
AA( AA const& obj )
{
*this = obj;
}

for more complex objects, a common idiom is to use the
swap-with-temp-copy idiom, which is the solution Alf
pointed you to:
AA& operator=( AA const& obj )
{
AA(obj).swap(*this);
return *this;
}
Implementing an efficient swap operation is a good idea
anyway for most of the complex objects that are assignable.


hth-Ivan
 
D

dc

This is quite strange

Suppose AA has a constructor with arg int.
int inst;
.........
AA (inst);//Treats inst as a variable of AA.
AA(10);//OK
 
R

Rolf Magnus

Thanks All for the detailed explaination,
do you mean I can not call a constructor explicitly?

A constructor is - as the name suggests - part of construction of an object.
An object's constructor gets only called once, and that's when the object
is created.
 
A

Alf P. Steinbach

* (e-mail address removed):
Thanks All for the detailed explaination,
do you mean I can not call a constructor explicitly?

See the reply from Ivan Vecerina.

What I think you mean by the question, namely, invoking a constructor on
an existing object, is something you absolutely don't want to do!

Also, be aware that (1) that's _not_ the same as an explicit constructor
call in the sense the term is used in the standard, and (2) that use of
the word "call" -- or even "invoke" -- in the context of constructor
whatchammacallitbutsomeutterancethatcausesthemtoexecute, can have about
the same effect on some C++ programmers as the publishing of drawings of
Mohammed recently had on some fanatics: they'll go amok, berserk, etc.
 
S

Sharad Kala

| This is quite strange

What is quite strange ? Please specify the context.

| Suppose AA has a constructor with arg int.
| int inst;
| ........
| AA (inst);//Treats inst as a variable of AA.
| AA(10);//OK

I can't see the point you are trying to make.

Sharad
 
O

Old Wolf

dc said:
This is quite strange

Suppose AA has a constructor with arg int.
int inst;
........
AA (inst);//Treats inst as a variable of AA.
AA(10);//OK

The rule is: If it could compile OK as a declaration, then it is;
otherwise it isn't. :) Here's another example:

typedef int x;
class A { };

void foo()
{
A b(x); // declares function b taking int as parameter
// and returning an A (!)

A (x); // declares local variable named 'x'
// (hides the typedef)
A a(x); // creates object 'a' , passing local variable
// 'x' to its copy constructor

}
 
A

Alf P. Steinbach

* Old Wolf:
The rule is: If it could compile OK as a declaration, then it is;
otherwise it isn't. :) Here's another example:

typedef int x;
class A { };

void foo()
{
A b(x); // declares function b taking int as parameter
// and returning an A (!)

A (x); // declares local variable named 'x'
// (hides the typedef)
A a(x); // creates object 'a' , passing local variable
// 'x' to its copy constructor

}

He he... :) :) :)

The description "failed experiment" is very apt.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top