C+中的被动对象+; 谷歌C++风格使用结构来被动对象而不是类。被动对象有哪些好的用例(附示例)? 对被动对象使用结构(相对于类)有什么好处?

C+中的被动对象+; 谷歌C++风格使用结构来被动对象而不是类。被动对象有哪些好的用例(附示例)? 对被动对象使用结构(相对于类)有什么好处?,c++,class,struct,convention,C++,Class,Struct,Convention,被动对象通常是一个自身不会与其他对象交互的对象。这方面的一个例子是点的数学表示: struct Point { float x, float y; } 点在技术上不会与其他对象交互,它是被动的 非被动对象将是使用其他对象执行任务的对象,尤其是使用函数的对象。编写一个处理网络I/O的类应该是一个类,因为它将使用许多其他对象来处理套接字、地址等。我有一个来自当前程序的快速示例: 就好处而言,拥有一个结构意味着您不需要为您的成员提供gettor/setors。除非明确定义,否则结构成员/方法

被动对象通常是一个自身不会与其他对象交互的对象。这方面的一个例子是点的数学表示:

struct Point
{
    float x, float y;
}
点在技术上不会与其他对象交互,它是被动的

非被动对象将是使用其他对象执行任务的对象,尤其是使用函数的对象。编写一个处理网络I/O的类应该是一个类,因为它将使用许多其他对象来处理套接字、地址等。我有一个来自当前程序的快速示例:

就好处而言,拥有一个结构意味着您不需要为您的成员提供gettor/setors。除非明确定义,否则结构成员/方法是隐式的
public
。这意味着更少的代码,以及较小的内存改进。如果对象没有函数,则不会创建函数表。没那么大,但还是有进步。您也没有函数调用的开销

阶级则相反。它们隐式地
私有
,只有在明确定义为公共的情况下才是公共的。私有数据意味着您需要与所述数据交互的函数。这通常意味着将其隐藏在接口后面。这是我目前正在制作的IRC客户端的界面:

enum IRC_RESPONSE
{
  RSP_IGNORE = 0,
  RSP_CONTINUE,
  RSP_RET_TRUE,
  RSP_RET_FALSE,
  RSP_BREAK
};

class IRC_Client
{
public:
  //Initialize all sockets and stuff given IP, port, etc
  bool Initialize(const char *address, const int port);

  //login to server, give login info
  bool Login(const char *password, const char *nickname, const char *user);

  //disconnect
  bool Disconnect();

  //main update loop
  void Run();

  //join/leave channel
  void Join(const std::string channel);
  void Part(const std::string channel);

  //main update

private:
  // MEMBERS ////////////////////////////////////////////////////////
  SOCKET ClientSocket_;         //socket of the client
  sockaddr_in *ServerAddress_;  //address of the server
  std::string Nickname_;        //nickname of user

  // METHODS ////////////////////////////////////////////////////////
  //parsing user input to decide what to do
  bool HandleUserInput(std::string message);

  //handling any message from server
  IRC_RESPONSE HandleServerMessage(std::string message);

  //send message
  void SendMessage(std::string message);
  void SendMessage(std::string prefix, std::string command, std::string         parameters, std::string trail);

  // UTILITIES //////////////////////////////////////////////////////
  //pull nickname from prefix
  std::string GetNickname(std::string prefix);

  //parse
  void ParseServerMessage(std::string message, std::string &prefix,     std::string &command, std::string &end, std::vector<std::string> &parameters);

  // USER INPUT /////////////////////////////////////////////////////
  static void *_inputThread(void*);
};
enum IRC\u响应
{
RSP_IGNORE=0,
继续,,
RSP_RET_TRUE,
RSP_RET_FALSE,
休息时间
};
类IRC_客户端
{
公众:
//初始化给定IP、端口等的所有套接字和内容
bool初始化(常量字符*地址,常量int端口);
//登录服务器,提供登录信息
bool登录(const char*密码、const char*昵称、const char*用户);
//断开
bool Disconnect();
//主更新循环
无效运行();
//加入/退出频道
void连接(const std::string通道);
无效部分(常量标准::字符串通道);
//主要更新
私人:
//成员////////////////////////////////////////////////////////
SOCKET ClientSocket \;//客户端的套接字
*服务器地址中的sockaddr\u;//服务器的地址
std::string昵称\;//用户的昵称
//方法////////////////////////////////////////////////////////
//解析用户输入以决定要执行的操作
bool HandleUserInput(标准::字符串消息);
//处理来自服务器的任何消息
IRC_响应HandleServerMessage(std::string消息);
//发送消息
void SendMessage(std::string message);
void SendMessage(std::string前缀、std::string命令、std::string参数、std::string trail);
//公用事业//////////////////////////////////////////////////////
//从前缀中提取昵称
std::string get昵称(std::string前缀);
//解析
void ParseServerMessage(std::string消息、std::string和前缀、std::string和命令、std::string和end、std::vector和参数);
//用户输入/////////////////////////////////////////////////////
静态void*_输入线程(void*);
};

这门课一点也不被动!我在中使用结构,如
sockaddr\u。我还使用了一些类,比如
std::string
。创建这个is类是很好的,因为我隐藏了实现,以便用户保持简单的生活。

被动对象通常是一个本身不会与其他对象交互的对象。这方面的一个例子是点的数学表示:

struct Point
{
    float x, float y;
}
点在技术上不会与其他对象交互,它是被动的

非被动对象将是使用其他对象执行任务的对象,尤其是使用函数的对象。编写一个处理网络I/O的类应该是一个类,因为它将使用许多其他对象来处理套接字、地址等。我有一个来自当前程序的快速示例:

就好处而言,拥有一个结构意味着您不需要为您的成员提供gettor/setors。除非明确定义,否则结构成员/方法是隐式的
public
。这意味着更少的代码,以及较小的内存改进。如果对象没有函数,则不会创建函数表。没那么大,但还是有进步。您也没有函数调用的开销

阶级则相反。它们隐式地
私有
,只有在明确定义为公共的情况下才是公共的。私有数据意味着您需要与所述数据交互的函数。这通常意味着将其隐藏在接口后面。这是我目前正在制作的IRC客户端的界面:

enum IRC_RESPONSE
{
  RSP_IGNORE = 0,
  RSP_CONTINUE,
  RSP_RET_TRUE,
  RSP_RET_FALSE,
  RSP_BREAK
};

class IRC_Client
{
public:
  //Initialize all sockets and stuff given IP, port, etc
  bool Initialize(const char *address, const int port);

  //login to server, give login info
  bool Login(const char *password, const char *nickname, const char *user);

  //disconnect
  bool Disconnect();

  //main update loop
  void Run();

  //join/leave channel
  void Join(const std::string channel);
  void Part(const std::string channel);

  //main update

private:
  // MEMBERS ////////////////////////////////////////////////////////
  SOCKET ClientSocket_;         //socket of the client
  sockaddr_in *ServerAddress_;  //address of the server
  std::string Nickname_;        //nickname of user

  // METHODS ////////////////////////////////////////////////////////
  //parsing user input to decide what to do
  bool HandleUserInput(std::string message);

  //handling any message from server
  IRC_RESPONSE HandleServerMessage(std::string message);

  //send message
  void SendMessage(std::string message);
  void SendMessage(std::string prefix, std::string command, std::string         parameters, std::string trail);

  // UTILITIES //////////////////////////////////////////////////////
  //pull nickname from prefix
  std::string GetNickname(std::string prefix);

  //parse
  void ParseServerMessage(std::string message, std::string &prefix,     std::string &command, std::string &end, std::vector<std::string> &parameters);

  // USER INPUT /////////////////////////////////////////////////////
  static void *_inputThread(void*);
};
enum IRC\u响应
{
RSP_IGNORE=0,
继续,,
RSP_RET_TRUE,
RSP_RET_FALSE,
休息时间
};
类IRC_客户端
{
公众:
//初始化给定IP、端口等的所有套接字和内容
bool初始化(常量字符*地址,常量int端口);
//登录服务器,提供登录信息
bool登录(const char*密码、const char*昵称、const char*用户);
//断开
bool Disconnect();
//主更新循环
无效运行();
//加入/退出频道
void连接(const std::string通道);
无效部分(常量标准::字符串通道);
//主要更新
私人:
//成员////////////////////////////////////////////////////////
SOCKET ClientSocket \;//客户端的套接字
*服务器地址中的sockaddr\u;//服务器的地址
std::string昵称\;//用户的昵称
//方法////////////////////////////////////////////////////////
//解析用户输入以决定要执行的操作
bool HandleUserInput(标准::字符串消息);
//处理来自服务器的任何消息
IRC_响应HandleServerMessage(std::string消息);
//发送消息
void SendMessage(std::string message);
void SendMessage(std::string前缀、std::string命令、std::string参数、std::string trail);
//公用事业/////////////////////////////////////////////////////