Circle class

S

Stub

In statement:
Circle c1=c2;
Is the assignment operator= of Circle called?

In statement
Circle c1(c2);
Is the copy constructor called?
 
M

Mike Wahler

Stub said:
In statement:
Circle c1=c2;
Is the assignment operator= of Circle called?
In statement
Circle c1(c2);
Is the copy constructor called?

Why not try them and find out?

-Mike
 
I

Ivan Vecerina

Stub said:
In statement:
Circle c1=c2;
Is the assignment operator= of Circle called?
Eventually.
The generated code will typically be identical
to that of the code below (a single copy-ctr call).
But the assignment operator must be accessible
when using this syntax, as the compiler may
interpret it as an initialization followed by
an assignment.
In statement
Circle c1(c2);
Is the copy constructor called?
Yes.

hth,
Ivan
 
S

Stub

Ivan Vecerina said:
Eventually.
The generated code will typically be identical
to that of the code below (a single copy-ctr call).
But the assignment operator must be accessible
when using this syntax, as the compiler may
interpret it as an initialization followed by
an assignment.
When you say "the compiler may interpret it as an initialization followed by
an assignment.", do you mean that if this could be an initalization only to
some compilers and initialization plus an assignment to others. The
compiler I checked proved to me only initialization is triggerred, no matter
if an assignment operator provided or not.
 
I

Ivan Vecerina

| | > | > > In statement:
| > > Circle c1=c2;
| > > Is the assignment operator= of Circle called?
| > Eventually.
| > The generated code will typically be identical
| > to that of the code below (a single copy-ctr call).
| > But the assignment operator must be accessible
| > when using this syntax, as the compiler may
| > interpret it as an initialization followed by
| > an assignment.
| >
| When you say "the compiler may interpret it as an initialization followed
by
| an assignment.", do you mean that if this could be an initalization only
to
| some compilers and initialization plus an assignment to others. The
| compiler I checked proved to me only initialization is triggerred, no
matter
| if an assignment operator provided or not.

Sorry, I got somewhat confused in my first reply, but
here's a more detailed and formal answer:

Here's an extract of what the standard says (8.5):
T x = a; is called copy-initialization
T x(a); is called direct-initialization
If direct-initialization is used,
or if a has type T (+/- const/volatile) or a base class of T,
then x is directly created using a copy-constructor call.

If copy-initialization is used and the types are somewhat different,
then a temporary T instance is first created, and a copy-constructor
is then called to perform a direct-initialization of x.
However, the intermediate temporary can be optimized-out by the
compiler (and most compilers I tried do so).

=> no, an assignment operator is never called. However, if the type
of a is different from the type of x, there is one major difference
between the two syntaxes: the copy constructor of T will have to be
*accessible* (e.g. public if called outside of the class members)
if the copy-initialization syntax is used.
That was it...

As a summary:
- if in your code example c2 was also of type Circle,
then the two variable initializations are fully equivalent,
and translated into a single constructor call.
- if c2 was not of the same type, then the Circle c1=c2 syntax
can be less efficient (may involve an intermediate copy), and
the copy-constructor needs to be accessible.


With my apologies for the confusion,
Ivan
 
A

Andrey Tarasevich

Stub said:
In statement:
Circle c1=c2;
Is the assignment operator= of Circle called?

No. The conversion constructor of 'Circle' is called. Which one -
depends on 'c2's type, on the set of accessible conversion constructors
of 'Circle' and on the set of accessible conversion operators provided
by 'c2'.
In statement
Circle c1(c2);
Is the copy constructor called?

Same answer as above (although this situation is slightly different).
 
G

Gavin Deane

Ivan Vecerina said:
Eventually.
The generated code will typically be identical
to that of the code below (a single copy-ctr call).
But the assignment operator must be accessible
when using this syntax, as the compiler may
interpret it as an initialization followed by
an assignment.

Are you sure about that Ivan? I thought Circle c1=c2; was *always*
copy construction (provided that c2 is the same type as c1) and the
use of = was just a syntax convenience.

GJD
 
I

Ivan Vecerina

| > > In statement:
| > > Circle c1=c2;
| > > Is the assignment operator= of Circle called?
| > Eventually.
| > The generated code will typically be identical
| > to that of the code below (a single copy-ctr call).
| > But the assignment operator must be accessible
| > when using this syntax, as the compiler may
| > interpret it as an initialization followed by
| > an assignment.
|
| Are you sure about that Ivan?

No ... please see the correction in my 2nd reply to Stub.

I confused what really can happen:
creation of a temporary, from which the var is then constructed
[can happen if the type of c2 is not Circle]
with:
default-initialization of the var, then assignment [WRONG].

My apologies for not having been really awake yet...

| I thought Circle c1=c2; was *always*
| copy construction (provided that c2 is the same type as c1) and the
| use of = was just a syntax convenience.
There is a subtle difference, in that the copy-constructor must
be accessible when using the = syntax, and not with the () one.

Here's a minimal complete example:


class Resource {
public:
// ....
void acquire();
void release(); // must balance each call to acquire!
};

// RAII class to manage resource locking
class LockResource
{
public:
LockResource(Resource& resource)
: res_(resource) { res_.acquire(); }
~LockResource() { res_.release(); }

private:
Resource& res_;
// must disable copy-constructor:
LockResource(LockResource const& resource);
};

void useResource1(Resource& res)
{
LockResource lock(res); // ok
// do stuff...
}

void useResource2(Resource& res)
{
LockResource lock = res; // ERROR: copy-ctr not accessible
// do stuff...
}



Regards,
Ivan
 
G

Gavin Deane

Ivan Vecerina said:
| > > In statement:
| > > Circle c1=c2;
| > > Is the assignment operator= of Circle called?
| > Eventually.
| > The generated code will typically be identical
| > to that of the code below (a single copy-ctr call).
| > But the assignment operator must be accessible
| > when using this syntax, as the compiler may
| > interpret it as an initialization followed by
| > an assignment.
|
| Are you sure about that Ivan?

No ... please see the correction in my 2nd reply to Stub.

Saw it, but not until after I'd posted :)
I confused what really can happen:
creation of a temporary, from which the var is then constructed
[can happen if the type of c2 is not Circle]
with:
default-initialization of the var, then assignment [WRONG].

My apologies for not having been really awake yet...

| I thought Circle c1=c2; was *always*
| copy construction (provided that c2 is the same type as c1) and the
| use of = was just a syntax convenience.
There is a subtle difference, in that the copy-constructor must
be accessible when using the = syntax, and not with the () one.

Now you say that, I do remember. But it never hurts to be reminded.
Particularly since the compiler I use most often (MSVC++6) doesn't
seem to have a problem with your code.

<your example - which Comeau online correctly rejected - snipped>

GJD
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top