does `C o1(C());' declare a function

L

lovecreatesbea...

Why doesn't line# 21 create an object of type `C' class? I think the
default construction function invocation `C()' creates a temporary
nameless object, and `o1' will be built with the copy construction
function of the same class from this temporary object. With g++ (GCC)
3.4.2 (mingw-special), line 21 doesn't create `o1' as type of `C'
class. No default and copy constructions are called for this line. I
don't understand this.

For line 22, only C::C(int) is called, no copy construction is called.
Could you please explain this also?

#include <iostream>

class C
{
public:
C(){
std::cout << "C()" << std::endl;
}

C(int ){
std::cout << "C(int)" << std::endl;
}

C(const C &){
std::cout << "C(const C &)" << std::endl;
}
};

int main()
{
C o1(C()); /*line 21*/
C o2(C(0)); /*line 22*/
C o3(o2);
}
 
D

Dizzy

Why doesn't line# 21 create an object of type `C' class?
Yes.

I think the
default construction function invocation `C()' creates a temporary
nameless object, and `o1' will be built with the copy construction
function of the same class from this temporary object.
No.

With g++ (GCC)
3.4.2 (mingw-special), line 21 doesn't create `o1' as type of `C'
class. No default and copy constructions are called for this line. I
don't understand this.

Simple, somewhere in the ISO C++ standard they say if some declaration ca be
interpreted as a function declaration then it is a function declaration.

This combined with the (not very well known) fact that functions, besides
declaring them as you usually do: int func(float obj); you can also do it
with the following syntax (they are equivalent) int func(float(obj));
Because of this and the rule above the explanation is simple. In order to
work arround it just wrap with another set of paranthesis the object
constructor parameter like: C o1((C()));
 
M

mlimber

Why doesn't line# 21 create an object of type `C' class? I think the
default construction function invocation `C()' creates a temporary
nameless object, and `o1' will be built with the copy construction
function of the same class from this temporary object. With g++ (GCC)
3.4.2 (mingw-special), line 21 doesn't create `o1' as type of `C'
class. No default and copy constructions are called for this line. I
don't understand this.

See said:
For line 22, only C::C(int) is called, no copy construction is called.
Could you please explain this also?

The compiler is free to optimize that away in an initialization.
#include <iostream>

class C
{
public:
C(){
std::cout << "C()" << std::endl;
}

C(int ){
std::cout << "C(int)" << std::endl;
}

C(const C &){
std::cout << "C(const C &)" << std::endl;
}
};

int main()
{
C o1(C()); /*line 21*/
C o2(C(0)); /*line 22*/
C o3(o2);
}

Cheers! --M
 
G

Grizlyk

Why doesn't line# 21 create an object of type `C' class? I think the
default construction function invocation `C()' creates a temporary
nameless object, and `o1' will be built with the copy construction
function of the same class from this temporary object. With g++ (GCC)
3.4.2 (mingw-special), line 21 doesn't create `o1' as type of `C'
class. No default and copy constructions are called for this line. I
don't understand this.

info gcc,Invoking GCC, C++ Dialect Options

`-fno-elide-constructors'
The C++ standard allows an implementation to omit creating a
temporary which is only used to initialize another object of the
same type. Specifying this option disables that optimization, and
forces G++ to call the copy constructor in all cases.
 
S

Sylvester Hesp

Dizzy said:
Simple, somewhere in the ISO C++ standard they say if some declaration ca
be
interpreted as a function declaration then it is a function declaration.

This combined with the (not very well known) fact that functions, besides
declaring them as you usually do: int func(float obj); you can also do it
with the following syntax (they are equivalent) int func(float(obj));

That is true, but that is not why it's a function declaration _in this
particular case_. It would be the case if
C o1(C(o2));
was used, but the function parameter name (o2 in the above example) was
omitted, making it a function declaration that returns C, and accepts a
[function that returns C with no parameters] as a parameter.

- Sylvester
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top