简体   繁体   中英

Using Custom Struct inside of Class Definition

This is a relatively simple question, but the specifics are confusing me.

I have a basic struct as follows:

struct player {
    string name;
    int rating;
 };

and a class 'team' which would make use of this struct. With this in mind, where is the correct placement of the 'player' struct- inside the public/private fields of the team or in a separate file? Also, would there be anything different that I'd need to do in order to create an instance of it? Thanks.

You have to ask yourself a question. Is the struct player meaningless unless in conjunction with class team ? If so, it should probably be defined inside team ; otherwise, it should be defined independently.

That is: if someone can conceivably want to use a player object that has not been directly retrieved from a team , you should make player an independent struct. Since we don't know the general structure or idea behind your program, we cannot give you a direct answer.

It depends on the how you use team/player struct. If you player is used by other part of the application, you should put player independent, that's the most cases do.

struct Player {
  int rating;
  std::string name;
};

class Team
{

private:
  std::vector<Player> players_;  // here you have a bunch of players
};

if your Player is totally internally used by Team and no one is using it, you can put it inside Team struct/class to narrow down its name scope.

 class Team
 {
 private:
   struct Player{
      int rating;
      std::string name;
   };

 private:
  std::vector<Player> players_;  // here you have a bunch of players
 };

simply define the struct before the class. that should do the trick...

I think a team is a collection of players , so it is convinient to use some container for managing the team.
A lot of efficient containers can be found in STL , it is up to you to choose one which suits you needs best.
Also to mention, in order to group staff related to the same task you can use namespace .

The key is whether it ia possible to use Player without using Team . If so, it should be a separate class, and not nested. In well designed conventional (OO) software, most nested classes are used exclusively inside the outer class, and are private. Or they are proxies, used for return values from functions of the outer class, but never declared by clients of the outer classes.

It may also be deemed advisable to make other "views" of the class member classes; eg iterators into a container. Whether iterators should be members, or free classes, is more of a style issue than anything else (but you should be consistent); it mainly affects the name ( ContainerIterator vs. Container::Iterator ).

Finally, templates introduce another dimension: given a template on a type T , it's easy, within this template, to reference members, including member types, of T ; it's impossible to find "unrelated" types. In other words, if you instantiate the template with T == Container , Container::Iterator is readily available as typename T:Iterator , but you have no way of finding ContainerIterator .

Given the names of your classes, I suspect very strongly that they should be independent, with Team having a 1-n containment relationship to Player . (I also suspect that both are entity objects, and should be made uncopyable and unassignable. But that's another issue.)

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