what will be called ?

Discussion in 'C++' started by Krzysztof Poc, May 10, 2011.

  1. Hi

    What constructors/operators be called when executing the line "Dog
    reks = Dog ();" in the following program ?

    class Dog {};
    Dog rex = Dog ();

    thanks for help
     
    Krzysztof Poc, May 10, 2011
    #1
    1. Advertising

  2. Krzysztof Poc

    dost Guest

    On May 10, 3:24 pm, Krzysztof Poc <> wrote:
    > Hi
    >
    > What constructors/operators be called when executing the line "Dog
    > reks = Dog ();" in the following program ?
    >
    > class Dog {};
    > Dog rex = Dog ();
    >
    > thanks for help


    Dog rex = Dog ();
    Dog class has default constructor which will be called in this
    statement.You can check it by adding some code in your empty class for
    constructor.For example
    class Dog
    {
    Dog(){//some statement for output} //1
    Dog(int a){//some statement for output} //2
    };

    void main()
    {
    Dog rex = Dog (); //this will call constructor 1
    Dog rex1 = Dog (5); //this will call constructor 2



    }

    more accurately you can say its just a calling parametrized
    constructor.which can be done in another way too
     
    dost, May 10, 2011
    #2
    1. Advertising

  3. Krzysztof Poc

    dost Guest

    On May 10, 3:24 pm, Krzysztof Poc <> wrote:
    > Hi
    >
    > What constructors/operators be called when executing the line "Dog
    > reks = Dog ();" in the following program ?
    >
    > class Dog {};
    > Dog rex = Dog ();
    >
    > thanks for help


    This will call default constructor of your class.
    the two statement are same.
    Dog rex = Dog (); = Dog rex;
     
    dost, May 10, 2011
    #3
  4. On 5/10/2011 6:51 AM, dost wrote:
    > On May 10, 3:24 pm, Krzysztof Poc<> wrote:
    >> Hi
    >>
    >> What constructors/operators be called when executing the line "Dog
    >> reks = Dog ();" in the following program ?
    >>
    >> class Dog {};
    >> Dog rex = Dog ();
    >>
    >> thanks for help

    >
    > Dog rex = Dog ();
    > Dog class has default constructor which will be called in this
    > statement.You can check it by adding some code in your empty class for
    > constructor.For example
    > class Dog
    > {
    > Dog(){//some statement for output} //1


    This is a syntax error. The closing curly brace is going to be thrown
    out during removal of all comments (before preprocessing). If you
    needed to add a comment between curly braces, use /**/:

    Dog(){/*some statement for output*/} //1

    > Dog(int a){//some statement for output} //2


    Same thing.

    > };
    >
    > void main()


    There is no 'void main' in C++. 'main' returns 'int'.

    > {
    > Dog rex = Dog (); //this will call constructor 1
    > Dog rex1 = Dog (5); //this will call constructor 2
    >
    >
    >
    > }
    >
    > more accurately you can say its just a calling parametrized
    > constructor.which can be done in another way too


    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 10, 2011
    #4
  5. Krzysztof Poc

    Noah Roberts Guest

    On 5/10/2011 3:54 AM, dost wrote:
    > On May 10, 3:24 pm, Krzysztof Poc<> wrote:
    >> Hi
    >>
    >> What constructors/operators be called when executing the line "Dog
    >> reks = Dog ();" in the following program ?
    >>
    >> class Dog {};
    >> Dog rex = Dog ();
    >>
    >> thanks for help

    >
    > This will call default constructor of your class.
    > the two statement are same.
    > Dog rex = Dog (); = Dog rex;


    The second version doesn't call any constructor since Dog is a POD. It
    allocates the space but does no initialization. The first version on
    the other hand does indeed call the default constructor, which will
    default initialize ... nothing because Dog doesn't have any members. If
    Dog did have members the difference would be more significant.


    --
    http://crazycpp.wordpress.com
     
    Noah Roberts, May 10, 2011
    #5
  6. Krzysztof Poc

    red floyd Guest

    On May 10, 3:24 am, Krzysztof Poc <> wrote:
    > Hi
    >
    > What constructors/operators be called when executing the line "Dog
    > reks = Dog ();" in the following program ?
    >
    > class Dog {};
    > Dog rex = Dog ();


    Call me crazy, but this really sounds like a homework question.
     
    red floyd, May 10, 2011
    #6
  7. Krzysztof Poc

    dost Guest

    On May 10, 4:38 pm, Pete Becker <> wrote:
    > On 2011-05-10 06:54:21 -0400, dost said:
    >
    > > On May 10, 3:24 pm, Krzysztof Poc <> wrote:
    > >> Hi

    >
    > >> What constructors/operators be called when executing the line "Dog
    > >> reks = Dog ();" in the following program ?

    >
    > >> class Dog {};
    > >> Dog rex = Dog ();

    >
    > >> thanks for help

    >
    > > This will call default constructor of your class.
    > > the two statement  are same.
    > > Dog rex = Dog (); = Dog rex;

    >
    > No, they're different.


    well thank you you are right the are different.

    The first calls the default constructor to
    > create a temporary object, then calls the copy constructor to copy that
    > temporary into rex. The second calls the default constructor to create
    > rex. In the first statment, the compiler is allowed to skip the copy
    > constructor and construct rex directly


    please correct me if i am wrong,the reason for skipping copy
    constructor is that because both the object of the statement Dog rex =
    Dog (); go out of scope at the same time.

    , but it is not required to do
    > that. And if the copy constructor is not accessible (e.g.., if it's
    > private), the first is invalid but the second is okay.
    >
    > --
    >   Pete
    > Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    > Standard C++ Library Extensions: a Tutorial and Reference
    > (www.petebecker.com/tr1book)
     
    dost, May 11, 2011
    #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. Apricot
    Replies:
    4
    Views:
    556
    velthuijsen
    Apr 16, 2004
  2. Weng Tianxiang
    Replies:
    6
    Views:
    599
    glen herrmannsfeldt
    Sep 12, 2007
  3. S_K
    Replies:
    6
    Views:
    1,234
    Robert Dunlop
    Nov 8, 2007
  4. Jimmy Hartzell
    Replies:
    0
    Views:
    435
    Jimmy Hartzell
    May 19, 2008
  5. Jimmy Hartzell
    Replies:
    2
    Views:
    1,180
    Jimmy Hartzell
    May 20, 2008
Loading...

Share This Page