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

Discussion in 'C++' started by lovecreatesbea...@gmail.com, Jan 18, 2007.

  1. Guest

    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);
    }
    , Jan 18, 2007
    #1
    1. Advertising

  2. Dizzy Guest

    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
    Dizzy, Jan 18, 2007
    #2
    1. Advertising

  3. mlimber Guest

    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
    mlimber, Jan 18, 2007
    #3
  4. Dizzy wrote:
    > wrote:
    >
    >> Why doesn't line# 21 create an object of type `C' class?

    >
    > Yes.
    >


    Love your answer. :)
    Victor Bazarov, Jan 18, 2007
    #4
  5. Grizlyk Guest

    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.
    Grizlyk, Jan 19, 2007
    #5
  6. "Dizzy" <> wrote in message
    news:45af9f47$0$49196$...
    > 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
    Sylvester Hesp, Jan 19, 2007
    #6
  7. hit_pc Guest

    On Fri, 19 Jan 2007 19:34:29 +0800, Sylvester Hesp <> wrote:

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



    --
    Hello,World!
    ----legolaskiss.
    hit_pc, Jan 19, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kelvSYC
    Replies:
    6
    Views:
    7,221
    Richard Herring
    May 17, 2005
  2. classicist
    Replies:
    3
    Views:
    328
  3. Replies:
    1
    Views:
    1,320
    Jim Langston
    Nov 9, 2006
  4. Replies:
    2
    Views:
    664
    Triple-DES
    Feb 26, 2008
  5. Daniel T.
    Replies:
    1
    Views:
    322
    Kai-Uwe Bux
    Jan 20, 2009
Loading...

Share This Page