简体   繁体   中英

declaring struct inside of a class

I am reading through a framework for a game, and it has a Door struct inside the Room class as follows:

#include <vector>

class Room{
public:
    struct Door{
        unsigned name;
        unsigned orientation;
        Room* room1;
        Room* room2;
    };
    std::vector<Door*> adjacent;
};

What is the purpose of defining a struct inside of a class? And, does it make a difference what access modifier that the struct is defined with?

What is the purpose of defining a struct inside of a class?

It's just a nested type.

And, does it make a difference what access modifier that the struct is defined with?

If Door was declared private then trying to declare a variable of type Room::Door outside this class will give an error.

The intent is to create a type that not just the instances of that type are owned by the surrounding class, but the the type itself is as also owned by the surrounding class.

For an obvious example, most container types will define the iterator type for that container as a nested class (or struct). Even though vector<T>::iterator and deque<T>::iterator represent similar concepts, each is still owned by, and specific to, its associated container type.

As far as access specifiers go, they follow the usual rules -- if you make a struct/class definition private , it will only be visible to other code in the same class. If it's protected , it'll be visible to that class and its descendants. If it's public, it'll be visible to everybody.

To give a more concrete example, for positions in a business, you might have:

class secretary  {};

class executive {
   class secretary {};
};

In this case, a ::secretary is a "normal" secretary and an executive::secretary is an executive secretary -- although obviously similar, an executive secretary will normally a have job description that's at least somewhat different from a non-executive secretary's. An executive vice president might have one executive secretary and two "normal" secretaries, but a lower level manager is probably only authorized to have a normal secretary, not an executive secretary.

In real programming, you often have private nested classes -- unlike an executive secretary that's just different from a normal secretary, some of these are things that the rest of the world doesn't even know they exist at all (at least unless they look at the private parts of the header of course).

What is the purpose of defining a struct inside of a class? And, does it make a difference what access modifier that the struct is defined with?

No two people will give you the same answer as to the purpose, however we can tell you what the effects are:

  • In the scope of Room you can refer to the class as just Door.

  • Outside the scope of Room you must refer to the class as Room::Door

  • Yes, the visibility of the type is the same as any class member, and effected by private, protected and public.

Short answer: struct and class mean exactly the same thing in C++, except for the default access. Door is just a nested class. In other words:

struct Door{
    ...

Is the same as:

class Door{
    public:
    ...

Nesting classes is useful for organizational and documentation reasons. It's like namespaces, but predates namespace in C++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM