7 c++
我有一个这样的课:
template <class T>
class bag
{
public:
private:
typedef struct{void* prev; struct{T item; unsigned int count;} body; void* next;}* node;
typedef struct{
node operator->() { return current; }
operator(){;} // <- i can not do that, right?
private:
node current;
} iterator;
//...
};
Run Code Online (Sandbox Code Playgroud)
那么,如何为bag :: iterator编写构造函数呢?
为它做一些好名字:-)
typedef struct NoName1 {void* prev; NoName1(){}; struct NoName2{T item; unsigned int count; NoName2() {}} body; void* next;}* node;
Run Code Online (Sandbox Code Playgroud)
编辑:大声抱歉,写错了一个,但原则是一样的:-)
无法编写构造函数,bag::iterator因为iterator是一个typedef名称,禁止将其用作构造函数名称:
14882:2003 12.1/3
命名类的typedef-name不能用作构造函数声明的声明符中的标识符.
标准中甚至有一个例子,尽管在不同的段落中7.1.3/5:
typedef struct {
S(); //error: requires a return type because S is
// an ordinary member function, not a constructor
} S;
Run Code Online (Sandbox Code Playgroud)
如果需要用户定义的构造函数,则必须为该结构命名.该typedef struct { } name;节目的风格通常是用C++气馁风格指南,无论如何,赞成的struct name { };.
| 归档时间: |
|
| 查看次数: |
3986 次 |
| 最近记录: |