Constructor for a no-named struct
I have a class something like that:
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;
//...
};
So, how to write a constructor for the bag::iterator?
Make some nice name for it :-)
typedef struct NoName1 {void* prev; NoName1(){}; struct NoName2{T item; unsigned int count; NoName2() {}} body; void* next;}* node;
EDIT: LOL sorry, wrote it for the wrong one, but the principle is the same :-)
There is no way to write a constructor for bag::iterator
because iterator is a typedef name, which are prohibited from being used as constructor names:
14882:2003 12.1/3
a typedef-name that names a class shall not be used as the identifier in the declarator for a constructor declaration.
There is even an example in the standard, although in a different paragraph, 7.1.3/5
:
typedef struct {
S(); //error: requires a return type because S is
// an ordinary member function, not a constructor
} S;
You will have to give that struct a name, if you want a user-defined constructor. The typedef struct { } name;
style of programming is usually discouraged by C++ style guides anyway, in favor of struct name { };
.
精彩评论