Understanding Initialization Syntax

Discussion in 'C++' started by joer3, Jul 21, 2008.

  1. joer3

    joer3 Guest

    When you initialize a variable like this:

    X var;
    var = X();

    The equals sign is called the assignment operator and you can overload
    it and indeed should in many cases.

    However, when you write code like this:

    X var = X();

    What is the equals sign considered? Is it still an operator? Can you
    overload it? I can't imagine any reason why you would want to overload
    it, just curious.

    Also, it is my current understanding this is what happens under the
    hood in these cases. Could someone confirm I understand correctly?

    First Case:
    1) Allocate memory for an "X" variable.
    2) Call default constructor for "X" and put that in "var" (no copy
    right?).
    3) Call default constructor of "X" again and copy it to "var".
    4) Delete temp data from second call to default constructor (not sure
    when that happens).

    Second Case:
    1) Allocate memory for an "X" variable.
    2) Call default constructor for "X" and put it in var.

    Thanks,

    Joe
    joer3, Jul 21, 2008
    #1
    1. Advertising

  2. joer3

    Noah Roberts Guest

    joer3 wrote:
    > When you initialize a variable like this:
    >
    > X var;
    > var = X();
    >
    > The equals sign is called the assignment operator and you can overload
    > it and indeed should in many cases.
    >
    > However, when you write code like this:
    >
    > X var = X();
    >
    > What is the equals sign considered? Is it still an operator? Can you
    > overload it? I can't imagine any reason why you would want to overload
    > it, just curious.


    Copy constructor, it's easily overloaded, and you should be able to
    think of many cases when you'd want to.
    Noah Roberts, Jul 21, 2008
    #2
    1. Advertising

  3. joer3

    joer3 Guest

    On Jul 21, 6:54 pm, Noah Roberts <> wrote:
    > joer3 wrote:
    > > When you initialize a variable like this:

    >
    > > X var;
    > > var = X();

    >
    > > The equals sign is called the assignment operator and you can overload
    > > it and indeed should in many cases.

    >
    > > However, when you write code like this:

    >
    > > X var = X();

    >
    > > What is the equals sign considered? Is it still an operator? Can you
    > > overload it? I can't imagine any reason why you would want to overload
    > > it, just curious.

    >
    > Copy constructor, it's easily overloaded, and you should be able to
    > think of many cases when you'd want to.


    Sorry for being unclear but I get what a copy constructor is and what
    it's used for. When I did a little test to better understand this, I
    didn't get consistent results with your statement. This program:

    #include <iostream>

    using namespace std;

    class My_Class
    {
    public:
    My_Class ()
    {
    cout << "I'm Default\n";
    }

    My_Class (const My_Class&)
    {
    cout << "I'm Copy\n";
    }

    My_Class& operator=(const My_Class)
    {
    cout << "I'm Assignment\n";
    return *this;
    }
    };

    int main()
    {
    cout << "My_Class a = My_Class():\n";
    My_Class a = My_Class();

    cout << "\nMy_Class b = a:\n";
    My_Class b = a;

    cout << "\na=b\n";
    a = b;

    return 0;
    }

    I get:

    My_Class a = My_Class():
    I'm Default

    My_Class b = a:
    I'm Copy

    a=b
    I'm Copy
    I'm Assignment
    Press any key to continue . . .

    I am guessing My_Class a = My_Class() doesn't call copy constructor
    because the compiler is trying to optimize right (FAQ [10.9])? Maybe
    if you could overload the "=" you could force it to call the copy
    constructor or do something else (again why would you want to).

    That brings me back to my original question, if the "=" isn't an
    operator what is it and can you overload it?
    joer3, Jul 22, 2008
    #3
  4. joer3

    Guest

    On Jul 22, 8:31 am, joer3 <> wrote:
    > a=b
    > I'm Copy
    > I'm Assignment
    > Press any key to continue . . .


    Note: you see both "I'm..." messages here because you forgot to make
    the assignment operator take the right-hand-side value by reference.

    > I am guessing My_Class a = My_Class() doesn't call copy constructor
    > because the compiler is trying to optimize right (FAQ [10.9])?


    Didn't check FAQ reference, but yeah.

    > Maybe
    > if you could overload the "=" you could force it to call the copy
    > constructor or do something else (again why would you want to).


    Nope... this is one very special case where the language is allowed to
    bypass your assignment operator to call the constructor: you can't
    prevent this optimisation.

    > That brings me back to my original question, if the "=" isn't an
    > operator what is it and can you overload it?


    As mentioned above, "=" can simply be an (arguably) more readable
    notation for construction, but for efficiency and so that it can be
    supported when no default constructor is available, the optimisation
    mentioned above is explicitly allowed to the compiler.

    Working backwards, I'll address some of your original questions:

    > > > However, when you write code like this:
    > > > X var = X();
    > > > What is the equals sign considered? Is it still an operator? Can you
    > > > overload it? I can't imagine any reason why you would want to overload
    > > > it, just curious.


    The equals sign is just a part of the construction notation, and
    doesn't constitute a call to the operator= member function.

    Note too: X var = X() doesn't require creation of a separate X
    instance that's then copied over or assigned into var: the (stack)
    memory for var is simply and directly initialised as per the default
    constructor.

    Cheers,

    Tony
    , Jul 22, 2008
    #4
  5. joer3

    James Kanze Guest

    On Jul 22, 12:44 am, joer3 <> wrote:
    > When you initialize a variable like this:


    > X var;
    > var = X();


    > The equals sign is called the assignment operator and you can
    > overload it and indeed should in many cases.


    > However, when you write code like this:


    > X var = X();


    > What is the equals sign considered?


    Punctuation. In this case, it's not an operator, and can't be
    overloaded.

    > Is it still an operator? Can you overload it? I can't imagine
    > any reason why you would want to overload it, just curious.


    No and no.

    > Also, it is my current understanding this is what happens
    > under the hood in these cases. Could someone confirm I
    > understand correctly?


    > First Case:
    > 1) Allocate memory for an "X" variable.
    > 2) Call default constructor for "X" and put that in "var" (no copy
    > right?).
    > 3) Call default constructor of "X" again and copy it to "var".


    And copy assign it to var. There's an important difference:
    copy assignment works on a fully constructed variable.

    > 4) Delete temp data from second call to default constructor
    > (not sure when that happens).


    The last two points would best be described as:

    -- construct a temporary X using the default constructor,
    -- copy assign it to var, using the assignment operator,
    -- destruct the temporary.

    > Second Case:
    > 1) Allocate memory for an "X" variable.
    > 2) Call default constructor for "X" and put it in var.


    Formally:
    -- construct a temporary X using the default constructor,
    -- copy it into var, using the copy constructor,
    -- destruct the temporary
    The standard explicitly authorizes the compiler to optimize out
    the copy, however (and all that I know of do).

    Your description would correspond to:
    X var ;

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jul 22, 2008
    #5
    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. JKop
    Replies:
    10
    Views:
    944
  2. Matthias Kaeppler
    Replies:
    2
    Views:
    441
    Victor Bazarov
    Jul 18, 2005
  3. Replies:
    6
    Views:
    459
    Ron Natalie
    Dec 11, 2005
  4. sks
    Replies:
    4
    Views:
    403
    Alf P. Steinbach
    Jul 6, 2006
  5. Shriramana Sharma
    Replies:
    6
    Views:
    256
    Shriramana Sharma
    Apr 5, 2013
Loading...

Share This Page