By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Circle class

P: n/a
In statement:
Circle c1=c2;
Is the assignment operator= of Circle called?

In statement
Circle c1(c2);
Is the copy constructor called?
Jul 19 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"Stub" <st**@asof.com> wrote in message
news:ra***********************@bgtnsc04-news.ops.worldnet.att.net...
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
Jul 19 '05 #2

P: n/a
"Stub" <st**@asof.com> wrote in message
news:ra***********************@bgtnsc04-news.ops.worldnet.att.net...
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
--
http://ivan.vecerina.com
Jul 19 '05 #3

P: n/a
Ivan Vecerina <pl*****************@ivan.vecerina.com> wrote in message
news:bo**********@newshispeed.ch...
"Stub" <st**@asof.com> wrote in message
news:ra***********************@bgtnsc04-news.ops.worldnet.att.net...
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.

Jul 19 '05 #4

P: n/a
"Stub" <st**@asof.com> wrote in message
news:N9***********************@bgtnsc04-news.ops.worldnet.att.net...
| Ivan Vecerina <pl*****************@ivan.vecerina.com> wrote in message
| news:bo**********@newshispeed.ch...
| > "Stub" <st**@asof.com> wrote in message
| > news:ra***********************@bgtnsc04-news.ops.worldnet.att.net...
| > > 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
--
http://ivan.vecerina.com
Jul 19 '05 #5

P: n/a
Stub wrote:
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).

--
Best regards,
Andrey Tarasevich

Jul 19 '05 #6

P: n/a
"Ivan Vecerina" <pl*****************@ivan.vecerina.com> wrote in message news:<bo**********@newshispeed.ch>...
"Stub" <st**@asof.com> wrote in message
news:ra***********************@bgtnsc04-news.ops.worldnet.att.net...
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? 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
Jul 19 '05 #7

P: n/a

"Gavin Deane" <de*********@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
| "Ivan Vecerina" <pl*****************@ivan.vecerina.com> wrote in message
news:<bo**********@newshispeed.ch>...
| > > 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
--
http://ivan.vecerina.com
Jul 19 '05 #8

P: n/a
"Ivan Vecerina" <pl*****************@ivan.vecerina.com> wrote in message news:<bo**********@newshispeed.ch>...
"Gavin Deane" <de*********@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
| "Ivan Vecerina" <pl*****************@ivan.vecerina.com> wrote in message
news:<bo**********@newshispeed.ch>...
| > > 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
Jul 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.