读书人

以下代码为什么这样写解决办法

发布时间: 2012-03-22 17:43:57 作者: rapoo

以下代码为什么这样写

C/C++ code
    tcp.endpoint end(tcp::v4,port);    tcp::endpoint endPoint(tcp::v4(),port);

第一句是错的,第二句是对的
endpoint应该是tcp类的静态变量,但是看了源码是这样写的
C/C++ code
class tcp{public:  /// The type of a TCP endpoint.  typedef basic_endpoint<tcp> endpoint;....}


[解决办法]
加了typedef 就不是变量了,而是类型
而是用endpoint来定义变量的
[解决办法]
tcp.endpoint 点号表示调用成员(数据成员或者方法成员)
tcp::enpoint 双冒号是域名空间提示符,表示endpoint是tcp域名空间中声明的一种类型、宏、静态函数、静态变量、枚举类型等等

每个class struct括号定义了一个类的同时也生成了一个域名空间

namespace ABCD
{
int A = 99;
void Fun1(){}

typdef int INT;
typedef basic_endpoint<tcp> endpoint;

};
[解决办法]
稍微补充一点,这属于类内部类型
诸如:vector::iterator都属于这种情况,
你这个类型只存在于这个类的作用于中,所以定义变量需要加上类名来限制
所以就成了这种形式tcp::endpoint endPoint(tcp::v4(),port);

[解决办法]
就是一个类型
比如嵌套类
[解决办法]
定义了一个新的类型

tcp.endpoint 就相当于 tcp.int 当然就不行了

读书人网 >C++

热点推荐