Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Opaque struct inside a class in C++

This thread is locked; no one can reply to it. rss feed Print
Opaque struct inside a class in C++
EliasYFGM
Member #11,096
June 2009

I have a class named "State_Game", which has an opaque [struct Data] private by default:

// State_Game.h

class State_Game
{
   struct Data;

public:
   State_Game();
   ...

Then I put the definition in the main source file:

// State_Game.cpp

struct State_Game::Data
{
   int asdf;
} data;

void a_global_function()
{
   data.asdf = 5; // Works just fine
}

However, as you can see, I'm still able to use that variable outside of State_Game, despite the structure is "private" inside the class (or not?).

Defining the struct as static also works, by the way:

static struct State_Game::Data
{
   int asdf;
} data;

So why am I still allowed to do this? Isn't it supposed that "data" should be private inside State_Game?

I'm sorry if this sounds too obvious or something, really, but I'm just a little confused. It's been long since I programmed something in C++ (I use C for almost everything).

bamccaig
Member #7,536
July 2006
avatar

I'm not sure if C++ supports private types. I'm a little cockeyed reading your definition of the Data type as it is. In any case, data is a global variable of type State_Game::Data. You don't actually appear to have actually declared a member variable within the State_Game class of type State_Game::Data though. I'm not sure if nested types are new to C++ or not, but I think the definition syntax you're using to define ::data comes from C. Probably what you want is something like this (untested):

class State_Game
{
    struct Data
    {
        int asdf;
    } data; // Declares a member variable `data` of type `Data`.

public:
    State_Game();
    ...
};

That, or separate Data from the State_Game class since it's probably not important for it to be nested.

struct Data
{
    int asdf;
};

class State_Game
{
    Data data;

public:
    State_Game();
     ...
};

Note that if you don't specify what shape Data is yet then you probably can't declare a member variable of it within the class (unless it's a pointer), just like in C. You appeared to forward declare the type, but without a full declaration the compiler wouldn't know how to allocate space for it. I'm rusty too, but I think C++ compilers would still choke on that.

Kitty Cat
Member #2,815
October 2002
avatar

EliasYFGM said:

However, as you can see, I'm still able to use that variable outside of State_Game, despite the structure is "private" inside the class (or not?).

The structure definition is private, but the variable and struct members are public (the variable is global, and struct members have public visibility by default). The variable needs to be put into private scope if you don't want non-State_Game methods to touch it. Either that, or make it all fields in Data private and friend the State_Game class.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Go to: