what does...

This is a discussion on what does... within the C++ Programming forums, part of the General Programming Boards category; What exactly does xyz (as an example) in var(xyz) do? i have been unable to find anything that might tell ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    3

    Arrow what does...

    What exactly does xyz (as an example) in var(xyz) do? i have been unable to find anything that might tell me and was curious as to what it did. Any help would be appriciated.

    Many thanks,
    Fussion

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>What exactly does xyz (as an example) in var(xyz) do?
    In what context?
    *Cela*

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    As in

    int xyz = 200;
    int var(xyz);

    ?

    If so, it does the exact same thing as
    int var = xyz;
    But only for built-in types
    User for user defined types (classes, structs) assigning a variable like this calls the copy constructor, as opposed to the default constructor, and assignment operator.

    As an example, try running this:
    Code:
    class contact
    {
    public:
       contact() {std::cout << "default constructor"; }
       contact(const contact &rhs) 
       : m_number(rhs.m_number), m_name(rhs.m_name) { std::cout << "copy constructor"; }
       contact(const std::string &name, int number) 
       : m_number(number), m_name(name) { std::cout << "overloaded construtor"; }
    
    private:
       int m_number;
       std::string m_name;
    };
    For built in types, it doesn't matter which way you assign. For user defined types, prefer the first ( var(anothervar) ) as you only invoke a single function call.
    Last edited by Eibro; 01-19-2003 at 05:14 PM.

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Eibro
    But only for built-in types
    User for user defined types (classes, structs) assigning a variable like this calls the copy constructor, as opposed to the default constructor, and assignment operator.

    ...

    For built in types, it doesn't matter which way you assign. For user defined types, prefer the first ( var(anothervar) ) as you only invoke a single function call.
    That's not true at all!

    The = in a declaration with initialization is purely cosmetic. The copy constructor is called, not the default and assignment.

    Both explicitly specifying the copy constructor and using = do exactly the same thing.

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Hmm I wonder; is this a compiler optimization or does it state somewhere in the standard that the result is a call to the copy constructor?

    Anyway, good to know. Thanks for clearing that up.

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    According to the standard the copy constructor is called.

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    type variable = something;

    is initialisation.

    variable = something;

    is assignment.

    Copy constructor called in initialisation and assignment operator in assignment. Learn to spot the difference between the two.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Rightly Stated

    Originally posted by Stoned_Coder
    type variable = something;

    is initialisation.

    variable = something;

    is assignment.

    Copy constructor called in initialisation and assignment operator in assignment. Learn to spot the difference between the two.
    A copy constructor is called
    1: Pass by value
    2: Return by value
    3: Creation of one object using another object // Initialization

    Well having said that, I just can't understand why a default constructor is called in this case

    Imagine that X is a class

    X(); // Creates an object and this object dies at the end of this statement

    X ob = X(); // calls the default constructor only

    It does not call the copy constructor nor the assignment operator.
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  9. #9
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: Rightly Stated

    Originally posted by shiv_tech_quest
    Well having said that, I just can't understand why a default constructor is called in this case

    Imagine that X is a class

    X(); // Creates an object and this object dies at the end of this statement

    X ob = X(); // calls the default constructor only

    It does not call the copy constructor nor the assignment operator.
    Yes it does. It calls the default constructor for the X() part and then the copy constructor for ob's initialization.

  10. #10
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Its working correctly

    Hi Poly,

    I had tried this few years ago, when learning copy / default constructor, can't recollect the compiler. It didn't behave this way. So was always wondering why it didn't work the way I had predicted (default first followed by copy), but just tried the same now on MSVC++6 and worked correctly.... hmmmmmmm what more can i say
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21