Class

Discussion in 'C++' started by Miami_Vice, Jan 21, 2007.

  1. Miami_Vice

    Miami_Vice Guest

    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
     
    Miami_Vice, Jan 21, 2007
    #1
    1. Advertising

  2. Miami_Vice

    Pete Becker Guest

    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)
     
    Pete Becker, Jan 21, 2007
    #2
    1. Advertising

  3. Miami_Vice

    Gavin Deane Guest

    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
     
    Gavin Deane, Jan 21, 2007
    #3
  4. Miami_Vice

    Jim Langston Guest

    "Miami_Vice" <> wrote in message
    news:...
    > 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.
     
    Jim Langston, Jan 22, 2007
    #4
  5. Miami_Vice

    Grizlyk Guest

    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);
     
    Grizlyk, Jan 22, 2007
    #5
  6. Miami_Vice

    Gavin Deane Guest

    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
     
    Gavin Deane, Jan 22, 2007
    #6
  7. Miami_Vice

    Grizlyk Guest

    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);
     
    Grizlyk, Jan 22, 2007
    #7
  8. Miami_Vice

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Jan 23, 2007
    #8
    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. cyril
    Replies:
    2
    Views:
    3,869
    cyril
    Aug 25, 2004
  2. Purush
    Replies:
    4
    Views:
    1,662
    Purush Rudrakshala
    Apr 13, 2005
  3. E11
    Replies:
    1
    Views:
    4,781
    Thomas Weidenfeller
    Oct 12, 2005
  4. christopher diggins
    Replies:
    16
    Views:
    756
    Pete Becker
    May 4, 2005
  5. Joseph Turian
    Replies:
    5
    Views:
    594
Loading...

Share This Page