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

copy constructor Q

If I do this:

struct aa
{
int r,s,t;
aa();
aa(const aa& t);
};

int tt()
{
aa zz = aa();
}

which constructor(s) are supposed to be invoked when creating zz? The
compiler I use simply invokes the aa(), but I am wondering based on the
comeau compiler output if this is a correct interpretation of the
standard, or if both constructors are supposed to be invoked?

Thanks

David

Mar 3 '06 #1
7 1839

David Lindauer wrote:
If I do this:

struct aa
{
int r,s,t;
aa();
aa(const aa& t);
};

int tt()
{
aa zz = aa();
}

which constructor(s) are supposed to be invoked when creating zz? The
compiler I use simply invokes the aa(), but I am wondering based on the
comeau compiler output if this is a correct interpretation of the
standard, or if both constructors are supposed to be invoked?

Thanks

David

aa::aa() and an implicit aa::= are invoked. If your code is 'aa
zz(aa())' then aa::aa() and aa::aa(const aa&t) are invoked.

Mar 3 '06 #2
Fei Liu wrote:
David Lindauer wrote:
If I do this:

struct aa
{
int r,s,t;
aa();
aa(const aa& t);
};

int tt()
{
aa zz = aa();
}

which constructor(s) are supposed to be invoked when creating zz? The
compiler I use simply invokes the aa(), but I am wondering based on the
comeau compiler output if this is a correct interpretation of the
standard, or if both constructors are supposed to be invoked?

Thanks

David
aa::aa() and an implicit aa::= are invoked. If your code is 'aa


Erm, no. This is an initialization; there is no assignment involved.
zz(aa())' then aa::aa() and aa::aa(const aa&t) are invoked.
Similarly.

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
Mar 3 '06 #3
Fei Liu wrote:
David Lindauer wrote:
If I do this:

struct aa
{
int r,s,t;
aa();
aa(const aa& t);
};

int tt()
{
aa zz = aa();
}

which constructor(s) are supposed to be invoked when creating zz? The
compiler I use simply invokes the aa(), but I am wondering based on the
comeau compiler output if this is a correct interpretation of the
standard, or if both constructors are supposed to be invoked?

Thanks

David
aa::aa() and an implicit aa::= are invoked.


No assignment operator is involved in this. Only default-construction and
copy-construction. And most likely, with the allowed optimization, no
copy-construction is happening.
If your code is 'aa
zz(aa())' then aa::aa() and aa::aa(const aa&t) are invoked.


V
--
Please remove capital As from my address when replying by mail
Mar 3 '06 #4

Artie Gold wrote:
Fei Liu wrote:
David Lindauer wrote:
If I do this:

struct aa
{
int r,s,t;
aa();
aa(const aa& t);
};

int tt()
{
aa zz = aa();
}

which constructor(s) are supposed to be invoked when creating zz? The
compiler I use simply invokes the aa(), but I am wondering based on the
comeau compiler output if this is a correct interpretation of the
standard, or if both constructors are supposed to be invoked?

Thanks

David


aa::aa() and an implicit aa::= are invoked. If your code is 'aa


Erm, no. This is an initialization; there is no assignment involved.
zz(aa())' then aa::aa() and aa::aa(const aa&t) are invoked.


Similarly.

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]


What if the code is
aa zz;
zz=aa();

So in 'aa zz=aa();', '=' is equivalent of calling a copy constructor?

I guess it's time to revisit the book.

Mar 3 '06 #5
Fei Liu wrote:
Artie Gold wrote:
Fei Liu wrote:
David Lindauer wrote:
[..]
aa zz = aa();
[..]
aa::aa() and an implicit aa::= are invoked. If your code is 'aa
Erm, no. This is an initialization; there is no assignment involved.

[..]
What if the code is
aa zz;
zz=aa();


But the code _isn't_. Yes, if you change the code, you default-initialise
the 'zz' and then assign it on the next statement.
So in 'aa zz=aa();', '=' is equivalent of calling a copy constructor?
The syntax is called "copy-initialisation". Look it up.
I guess it's time to revisit the book.


Good call!
Mar 3 '06 #6
David Lindauer wrote:
If I do this:

struct aa
{
int r,s,t;
aa();
aa(const aa& t);
};

int tt()
{
aa zz = aa();
}

which constructor(s) are supposed to be invoked when creating zz?
Two possibilities: Either the default-constructor is called to create a
temporary, then the copy constructor is called to create zz from that. The
other possibility is that the temporary gets optimized away (which is
allowed by the standard) and so zz is created using the default
constructor.
However, even if the copy constructor is not used, it still must be
available, because optimizations can't change the correctness of code.
The compiler I use simply invokes the aa(), but I am wondering based on
the comeau compiler output
What output?
if this is a correct interpretation of the standard, or if both
constructors are supposed to be invoked?


Mar 4 '06 #7


Rolf Magnus wrote:
The compiler I use simply invokes the aa(), but I am wondering based on
the comeau compiler output


What output?


when I ran what I typed through the comeau online compiler, with the addition
of declaring both constructors explicit, I was given an error that there was
no suitable copy constructor available. That is why I was asking the
question... and your answer helped me see why it would. I didn't realize the
standard gave explicit instructions as to certain allowable optimizations and
was glad I asked :).

David
Mar 4 '06 #8

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

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
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: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.