Class

M

Miami_Vice

The following statements have identical functionalities (if anyone is
interested)

//
Classname object;
object=default constructor;
//

//Classname object default constructor;

for instance:

Vector a (2, 3);

is as same as

Vector a;

a=Vector(2, 3);

NB: Not trying to be a nudge, just for the sake of explanation
 
P

Pete Becker

Miami_Vice said:
The following statements have identical functionalities (if anyone is
interested)

//
Classname object;
object=default constructor;
//

//Classname object default constructor;

for instance:

Vector a (2, 3);

is as same as

Vector a;

a=Vector(2, 3);

They're not the same. The first uses only the constructor that takes two
arguments. The second uses the default constructor, the constructor that
takes two arguments, and the assignment operator. Try it with a class
that declares an assignment operator but doesn't define it.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
G

Gavin Deane

Miami_Vice said:
The following statements have identical functionalities (if anyone is
interested)

//
Classname object;
object=default constructor;
//

//Classname object default constructor;

for instance:

Vector a (2, 3);

is as same as

Vector a;

a=Vector(2, 3);

NB: Not trying to be a nudge, just for the sake of explanation

What do you think you are explaining and to whom do you think you are
explaining it? Because you are, of course, quite wrong.

Gavin Deane
 
J

Jim Langston

Miami_Vice said:
The following statements have identical functionalities (if anyone is
interested)

It depends on what you mean by "functionalities".
//
Classname object;
object=default constructor;
//

//Classname object default constructor;

for instance:

Vector a (2, 3);

This creates an instance of class Vector using a constructor taking 2
values.
is as same as

Vector a;

This creates an instance of class Vector using a default construtor.
a=Vector(2, 3);

Now a temporary is created using a constructor taking 2 values and a's
operator= is called.
NB: Not trying to be a nudge, just for the sake of explanation

Try using both types on this class:

class Vector
{
public:
Vector( int a, int b ) {};
};

Vector a;
should not even compile, you should get an error saying there is no default
constructor.

They are not the same.
 
G

Grizlyk

Miami_Vice said:
for instance:

Vector a (2, 3);

is as same as

Vector a;

a=Vector(2, 3);

Unlike to
Vector a (2);
expression
Vector a =2;
on some compilers can be same as
Vector a( Vector(2) ); //copy ctor is used, but later can be
eliminated

I am not shure, is it std C++ behaviour or concrete compilers
restrictions. And I am not shure, if copy ctor is private but
assignment is not, that assignment operator can not be used on some
concrete compilers:
Vector a =2;
could be same as
Vector a;
a=Vector(2);
 
G

Gavin Deane

Grizlyk said:
I am not shure, is it std C++ behaviour or concrete compilers
restrictions. And I am not shure, if copy ctor is private but
assignment is not, that assignment operator can not be used on some
concrete compilers:
Vector a =2;
could be same as
Vector a;
a=Vector(2);

No. For any type T, the statement

T name = expression;

never involves the assignment operator. Whether it compiles or not
depends on the existence of the necessary constructor(s) but not the
assignment operator. If the constructor is not there, the compiler
won't go looking for an option that uses the assignment operator
instead. Or if it does, it's non-standard behaviour.

Gavin Deane
 
G

Grizlyk

Gavin said:
If the constructor is not there, the compiler
won't go looking for an option that uses the assignment operator
instead. Or if it does, it's non-standard behaviour.

Absolutely. But treats
Vector a =2;
as
Vector a ( Vector(2) );
is standard behaviour? Not sure, because many said
Vector a =2;
is the same as
Vector a (2);
 
R

Rolf Magnus

Grizlyk said:
Absolutely. But treats
Vector a =2;
as
Vector a ( Vector(2) );
is standard behaviour?

Yes, formally. However, the compiler is allowed to optimize the copy away.
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top