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

Circle class

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
8 3364
"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
"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
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
"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
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
"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

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: rdeaton | last post by:
I need to design and code a Java program that calculates and prints the (D) diameter, the (C) circumference, or the (A) area of a circle, given the radius. The program inputs two data items: the...
0
by: Richard | last post by:
I want to put a GDI type circle on top of my DataGrid (actually I want to put GDI colored border around the entire selected row, but if I can figure out how to put a circle on top of it I can do the...
3
by: Richard | last post by:
I have a requirement to put a GDI style circle or rectangle border around the selected row of a datagrid/ It will overlap into the row above and below the selected row. Doing this in a the OnPaint...
1
by: JWest46088 | last post by:
I am a little confused about what I need to do here. I created four separate classes (Circle, Rectangle, Square, and Triangle) that each calculate their own area and perimeter via user input. Now...
9
by: saraaana | last post by:
Given the center and a point on the circle, you can use this formula to find the radius of the circle. Write a program that prompts the user to enter the center and a point on the circle. The program...
7
by: Tem | last post by:
I need to draw a black lined circle and save it as a gif file. Can this be done with wpf or do I need to use GDI+ The examples I found seem to only apply to UI elements not a file. Thank you ...
4
by: Slickuser | last post by:
How do I automatic move the X & Y to create a ellipse function as below, DrawCircle? Or there any sample source code out there? I want it move down by user input. How can I achieve this?...
19
by: NycCraze88 | last post by:
Hey all, I'm in a Java data structures class in college and I have a problem with this one sorting assignment. I'm not very experienced in java and have tried tackling this assignment in...
1
by: hdivecha | last post by:
i have make a jtree program in java and if the node is circle then the circle can be shown in frame and i have to resize it with slider . plz help me i have done the tree program bt how to get...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.