Paul Zhang
Paul Zhang

Reputation: 325

what does the give_me_a_name mean?

I'm new to c++ and I have write the code below in the clion:

Student student[] = {
            Student("Larry", 95.5, "This is Larry", 1);
            Student("Paul", 78.0, "This is Paul", 2);
            Student("Tom", 80.0, "This is Tom", 3);

    };

and it hints a clang-tidy: object destroyed immediately after the creation; did you mean to name the object? If I follow it, the code is changed to:

Student student[] = {
            Student("Larry", 95.5, "This is Larry", 1);
            Student give_me_a_name("Paul", 78.0, "This is Paul", 2);
            Student("Tom", 80.0, "This is Tom", 3);

    };

So what does the give_me_a_name mean? And why there could be such syntax?

Upvotes: 2

Views: 221

Answers (1)

eesiraed
eesiraed

Reputation: 4654

It's exactly what it sounds like. You used semicolons instead of commas, which made Clang think that you're constructing an object and immediately discarding it as if you had Student("Paul", 78.0, "This is Paul", 2); by itself. Therefore it suggests you to store the constructed object in an variable by giving it a name. Clang doesn't know what name you would want, so it puts a placeholder name there for you to replace.

Upvotes: 1

Related Questions