8

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?

2
  • You can condense your constructors down to a single one using default parameters. Commented Sep 14, 2016 at 16:26
  • And for amusement, add a constructor that only takes a string .... Commented Sep 14, 2016 at 22:14

2 Answers 2

11

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Just for completeness: It should be game(float v = 1.0,string n = "game"), not game(float v = 1.0,string n = "game), because the code won't compile with the missing quote.
@Striezel: Thanks, fixed.
@Cheersandhth.-Alf ,Just little doubt, as u told "It works as if a temporary object is constructed from a single argument specified as initializer, so according the promblem ,A new object is created with version = 2.0 with code 2?
@SubSea: The temporary is in practice always optimized away. That's called "elision" of the temporary and the copy or move constructor call. But still, since the construction of a temporary plus implicit call of copy or move constructor call is the spec, a copy or move constructor must be accessible and must not be declared explicit.
4

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

5 Comments

Are you sure it's the comma operator in this context? Do you get a complaint about the type of argument? Test this by adding a parenthesis around (then you do get comma operator for sure).
@Cheersandhth.-Alf Oops. It is just bad grammar. fixing
@NathanOliver ,there is no error like missing variable declartion here ,but i got your point of constructin objects with multiple variables.
@SubSea Yeah that is not what you will actually get. I was just putting it in human readable language.
@NathanOliver .okay.i got you..:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.