Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assignment initialization of a constructor

I am trying out different versions of calling the constructor, and I came up with this

#include<iostream>
#include<string>
using namespace std;
class game{

    public:
        float version;
        string name;
        game()
        {
            name = "game";
            version = 1.0;
        }
        game(float v,string n)
        {
            version = v;
            name = n;
        }
        game(float v)
        {
            version = v;
            name="any";
        }
};
int main()
{
    game lol1(1.0,"league of legends"); //functional form
    game lol2 = 2.0;    //assignment form
    game lol3{3.0,"league2"}; //uniform initialization
    game *pt = &lol1;
    cout<<pt->name<<endl;
    return 0;
}

Every statement compiles, but if I write

 game lol2 = 2.0,"league of legends2"; //code 2

I get an error:

expected unqualified-id before string constant

But the following code works fine:

game lol2 = {2.0,"league of legends2"}; //code 3

I am not getting what exactly the issue is with the second code. Any ideas?

like image 945
Anil Avatar asked Sep 14 '16 16:09

Anil


People also ask

What is initialization in constructor?

You explicitly initialize a class object when you create that object. There are two ways to initialize a class object: Using a parenthesized expression list. The compiler calls the constructor of the class using this list as the constructor's argument list. Using a single initialization value and the = operator.

What is assignment and initialization?

Initialization gives a variable an initial value at the point when it is created. Assignment gives a variable a value at some point after the variable is created.

Should my constructors use initialization lists or assignment?

Conclusion: All other things being equal, your code will run faster if you use initialization lists rather than assignment.

Why do we initialize in constructor?

A constructor is typically used to initialize instance variables representing the main properties of the created object. If we don't supply a constructor explicitly, the compiler will create a default constructor which has no arguments and just allocates memory for the object.


2 Answers

What you call "assignment form" is a copy initialization.

It works as if a temporary object is constructed from a single argument specified as initializer, and is then passed to the class' copy constructor or move constructor.

So, the code

game lol2 = 2.0,"league of legends2"; //code 2

… is just syntactically invalid.


Tip: Instead of three constructors, where the second adds a first argument, and the third adds a second argument, you can just use default argument values:

class game{

public:
    float version;
    string name;
    game(float v = 1.0,string n = "game")
        : version( v ), name( n )
    {}
};

The : syntax is a constructor member initializer list.

It can sometimes be more efficient, sometimes necessary, and anyway is usually more concise and conventional.

like image 70
Cheers and hth. - Alf Avatar answered Oct 05 '22 14:10

Cheers and hth. - Alf


You are not using the correct grammar to initialize a object from multiple values. When you do

game lol2 = 2.0,"league of legends2";

The grammer expects a variable name after the comma like

type name1 = value1, name2 = value2;

So you get an error as you have

type name1 = value1, value2;
                    ^ missing variable declaration here 

When you have multiple variables that you need to construct with you can only use the folowing forms

type name = {value1, value2, ..., valuen};
type name{value1, value2, ..., valuen};
auto name = type{value1, value2, ..., valuen};
type name(value1, value2, ..., valuen);
auto name = type(value1, value2, ..., valuen);
like image 23
NathanOliver Avatar answered Oct 05 '22 15:10

NathanOliver