Constructor for an unnamed structure

I have a class like this:

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 a bag :: iterator?

+7
c ++
source share
2 answers

Make a nice name for him :-)

 typedef struct NoName1 {void* prev; NoName1(){}; struct NoName2{T item; unsigned int count; NoName2() {}} body; void* next;}* node; 

EDIT: LOL sorry, wrote it incorrectly, but the principle is the same :-)

+6
source share

It is not possible to write a constructor for bag::iterator , because an iterator is a typedef name that cannot be used as constructor names:

14882:2003 12.1/3

the typedef name that names the class should not be used as an identifier in the declarator to declare the constructor.

There is even an example in the standard, although in another 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 need to specify this structure name if you want a custom constructor. Programming style typedef struct { } name; usually not recommended with C ++ style manuals in favor of struct name { }; .

+5
source share

All Articles