-1

i just found out we can defining and instantiate class at once like this

#include <iostream>

using namespace std;

class PLAYERS
{
public:
    int value3;
    PLAYERS(int value3)
    {
        this->value3 = value3;
    }
}play1(2), play2(3), play3(7);

int main()
{
    play1.value3 = 2;
    PLAYERS play4();
    cout << "Hello world!" << endl << play1.value3;
    return 0;
}

what is it called? when i delete the argument on instantiation of play1, play2, play3 like play4. it will compile error but the play4 is fine. how it can be?

Aorstab
  • 133
  • 1
  • 9
  • 2
    `PLAYERS play4();` is a function declaration anyway – StoryTeller - Unslander Monica Dec 07 '16 at 07:26
  • Maybe split this into two questions? The `play4()` one is a duplicate. – juanchopanza Dec 07 '16 at 07:27
  • @StoryTeller ah yes, stupid me, i intend to call default constructor but i'm forgot to call default constructor must be not use parenthesis or it will be function declaration and i laugh at myself because there is no default constructor in that class haha.. after i fix the code it compiles fine, just my mistake. thank you – Aorstab Dec 07 '16 at 08:01
  • @juanchopanza yes i will edit it, maybe i will delete the second question. if i may can you post the link of that same question? – Aorstab Dec 07 '16 at 08:01
  • downvoted. please dont modify the whole question,after solving it, just because you are lazy to create a new thread. this will be confusing to the future researcher. – Lorence Hernandez Dec 07 '16 at 08:05

1 Answers1

0

It's just called multiple declarations, no special name as far as I'm aware.

You're just making use of a shortened way of doing so, by having the declarations between the closing brace of the class definition and the semi-colon, which is perfectly valid but unusual.

acraig5075
  • 10,588
  • 3
  • 31
  • 50