Jérémy Pouyet
Jérémy Pouyet

Reputation: 2019

c++ code explanation after :: Operator

Can someone tells me what is the ::CLOSE in the following code :

Fight c;
c.type = Fight::CLOSE; //CLOSE can also become ::RANGE

I have to create the Fight class, but I have no idea of what is the ::CLOSE part. I just know that c.type is a bool or an int.

Edit : An enum, not an int or a bool

Upvotes: 1

Views: 99

Answers (5)

krish
krish

Reputation: 104

It could be one of the following

  1. enum nested in a class .
  2. static data member under public.
  3. int/bool public data member.

Upvotes: 0

Laurent LA RIZZA
Laurent LA RIZZA

Reputation: 2975

It's a name that is declared in the scope of the Fight class. It probably should be declared as an enum.

struct Fight {
    enum Status {
        CLOSE,
        RANGE
    }

    Status type;
    //...
}

Enums export their names (CLOSE, RANGE) to the enclosing scope, i.e. the scope of the class in this case. When converted to int, CLOSE will yield 0 and RANGE will yield 1. (With this order of declaration)

Upvotes: 0

Stefano Sanfilippo
Stefano Sanfilippo

Reputation: 33086

Fight must be a class or a struct (probably a struct), so CLOSE is either a public constant or a value from an enum declared inside that class. That is:

class Fight {
public:
    const bool CLOSE = false;
    //...
};

or

class Fight {
public:
    enum Status {
        CLOSE
    }
    //...
};

Since you said that "c.type is a bool or an int", I think the first one is more likely to be the definition of your Fight class.

Upvotes: 3

choeger
choeger

Reputation: 3567

It is a static member of the class Fight.

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385325

It's more likely to be an enum, but okay.

Inside Fight will be:

enum Something
{
   CLOSE, RANGE
};

Then Fight::CLOSE and Fight::RANGE are integral constants with distinct values.

Read about enums ("enumerated types") in your C++ book.

The :: syntax is (in this context) the way you access static members of the class. For example, you invoke static member functions like Fight::someFunc().

Upvotes: 2

Related Questions