what will be called ?

K

Krzysztof Poc

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
 
D

dost

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
 
D

dost

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;
 
V

Victor Bazarov

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
 
N

Noah Roberts

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

red floyd

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

dost

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
 

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,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top