Re: protected constructor, std::list and friends
On Nov 7, 2:59 am, "Thomas Grund" <thomas.gr...@mail.com> wrote:
Where is the destructor from the original code? The body of the destructor
was left blank, but it was given to show that there is a destructor in the
real code. I need the case with destructor!!!
Sorry! I Did not notice that I had "eaten" both the destructor and the
constructor. Now I am curious to what can be done. Perhaps it is a
requirement to have a public destructor if you want a class to be used
in a container. Perhaps not.
Tha fact is that looking at the errors messages given by gcc I
realized that the following should work:
#include <list>
using namespace std;
class Node
{
public:
protected:
Node(){};
~Node(){};
void addChild()
{
Children.push_back(Node());
}
private:
list<Node> Children;
friend class Database;
friend class __gnu_cxx::new_allocator< Node >;
};
class Database
{
public:
void addItem()
{
n.addChild();
}
Node n;
};
int main()
{
Database d;
}
Notice that friend class __gnu_cxx::new_allocator< Node >; is not the
most portable piece of code I have ever seen, but does the job. I have
tried also friend std::allocator< Node >; but does not work. If you
are using another compiler, you should try to dig into the error
messages (or perhaps post them to me), so you can figure out which
class must actually be declared friend.
Hope I have helped.
Elias Salom=E3o Helou Neto.