Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > does `C o1(C());' declare a function

Reply
Thread Tools

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

 
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      01-18-2007
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);
}

 
Reply With Quote
 
 
 
 
Dizzy
Guest
Posts: n/a
 
      01-18-2007
(E-Mail Removed) wrote:

> 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()));

--
Dizzy
http://dizzy.roedu.net

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      01-18-2007
(E-Mail Removed) wrote:
> 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 <http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.19>.

> 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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-18-2007
Dizzy wrote:
> (E-Mail Removed) wrote:
>
>> Why doesn't line# 21 create an object of type `C' class?

>
> Yes.
>


Love your answer.


 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-19-2007
(E-Mail Removed) wrote:

> 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.

 
Reply With Quote
 
Sylvester Hesp
Guest
Posts: n/a
 
      01-19-2007

"Dizzy" <(E-Mail Removed)> wrote in message
news:45af9f47$0$49196$(E-Mail Removed)...
> (E-Mail Removed) wrote:
>
>> 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));


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


 
Reply With Quote
 
hit_pc
Guest
Posts: n/a
 
      01-19-2007
On Fri, 19 Jan 2007 19:34:29 +0800, Sylvester Hesp <(E-Mail Removed)> wrote:

int func(int x)=int func(int),so
int func(int())=int func(int func1(void))
....



--
Hello,World!
----legolaskiss.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
private forward declare, public class. What does the standard say? Daniel T. C++ 1 01-20-2009 12:50 AM
Declare a Friend function which is a template member function of adifferent class getrakesh@gmail.com C++ 2 02-26-2008 11:32 AM
forward declare member function so that it can be friend function yancheng.cheok@gmail.com C++ 1 11-09-2006 11:30 AM
how to declare pseudo-matrix function argument that is not modified by function? classicist C Programming 3 10-05-2005 02:45 AM
"virtual outside class declaration" and "declaration does not declare anything" kelvSYC C++ 6 05-17-2005 08:58 AM



Advertisments