Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Class (http://www.velocityreviews.com/forums/t459960-class.html)

Miami_Vice 01-21-2007 07:16 PM

Class
 
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


Pete Becker 01-21-2007 07:38 PM

Re: Class
 
Miami_Vice wrote:
> 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)

Gavin Deane 01-21-2007 11:50 PM

Re: Class
 

Miami_Vice wrote:
> 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


Jim Langston 01-22-2007 01:12 AM

Re: Class
 

"Miami_Vice" <manmedia@gmail.com> wrote in message
news:1169406973.166117.93610@l53g2000cwa.googlegro ups.com...
> 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.



Grizlyk 01-22-2007 05:45 AM

Re: Class
 
Miami_Vice wrote:

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


Gavin Deane 01-22-2007 12:18 PM

Re: Class
 

Grizlyk wrote:
> 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


Grizlyk 01-22-2007 03:45 PM

Re: Class
 
Gavin Deane wrote:

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


Rolf Magnus 01-23-2007 12:11 AM

Re: Class
 
Grizlyk wrote:

> Gavin Deane wrote:
>
>> 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?


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



All times are GMT. The time now is 09:47 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.