1

I have a TrafficLight C++ class and I have some doubts about using enum for color management, especially regarding the correct syntax to use. below I write simplified code pieces, since my intent is only to understand how it works:

class TrafficLight
{
private:
    // ...
    enum class color{
        GREEN,
        RED,
        YELLOW
    };
    // ...
public:
    TrafficLight(/* Want to pass the color here in the constructor */)
    {
        if(/* Color passed as argument is RED */)
            // do something...
        {}
        else
            // do something else...
        {}
    }
}; 

OtherClass: this class creates a TrafficLight object with a specified color:

class OtherClass
{
public:
    //...
    void createTrafficLight()
    {
        TrafficLight traffic_light(/* Color */);
    }
    //...
};

TrafficLIght and OtherClass are not in the same file.

I'm not sure what the syntax is for passing the color of the traffic light as an argument

JamesD
  • 39
  • 4
  • 1
    Hi James, as your enum is private you won't be able to access it externally when calling the constructor of your TrafficLight class. You'll need to either make it public or use an accessor. – George Newton Dec 20 '19 at 14:22
  • There a couple concepts here to be familiar with. 1) C++ access modifiers: https://www.geeksforgeeks.org/access-modifiers-in-c/. 2) C++ includes: https://stackoverflow.com/a/50710593/620197 – Mike D Dec 20 '19 at 14:27
  • @GeorgeNewton Please write your answers in the appropriate place, not the comments section. Thanks! – Lightness Races in Orbit Dec 20 '19 at 15:51
  • @LightnessRaceswithMonica tbh it didn't seem like a very comprehensive answer.. so figured a comment was sufficient, but you are right. And I just noticed you list it as one of your pet hates.. won't happen again :P – George Newton Dec 20 '19 at 17:39
  • @GeorgeNewton :P If you don't have time to write a longer answer, that's fine, don't, but that doesn't magically make the un-peer-reviewed comments section the right place :) – Lightness Races in Orbit Dec 21 '19 at 00:45

2 Answers2

3

color can be used just like any other type and its enumerators can be accessed as if they were static members of a class:

    TrafficLight(color col)
    {
        if(col == color::RED)
            // do something...

    //...
    void createTrafficLight()
    {
        TrafficLight traffic_light(TrafficLight::color::RED);
    }

You will need to make enum class color public though, if code outside the class is supposed to be able to access it and its enumerators.

walnut
  • 21,629
  • 4
  • 23
  • 59
2

You need first of all make you enum public, then include .h of trafficLight class in file where OtherClass is defined.

The code can be the following:

class TrafficLight
{
private:

public:

    // ...
    enum class color{
        GREEN,
        RED,
        YELLOW
    };
    // ...

    TrafficLight(color col)
    {
        if(col == color::RED)
            // do something...
        {}
        else
            // do something else...
        {}
    }
}; 

And the OtherClass need to include the .h where is defined the TrafficLight class

class OtherClass
{
public:
    //...
    void createTrafficLight()
    {
        TrafficLight traffic_light(TrafficLight::color::RED);
    }
    //...
};
Zig Razor
  • 3,381
  • 2
  • 15
  • 35