protected constructor, std::list and friends

T

Thomas Grund

Hi,

In the following code I would like that the class Node is only be used by
the class Database. So the idea was to make the interface protected and
use the friend definition. The code does not compile because the list
needs an public destructor. Making list<Node> a friend of Node doesn't
help. If the constructor and destructor in Node is moved to the public
section then everything works but this is not what I want.

Thanks a lot,

Thomas


#include <list>

using namespace std;

class Node
{
public:
protected:
Node(){};
~Node(){};
void addChild()
{
Children.push_back(Node());
}

private:
list<Node> Children;

friend class Database;
};

class Database
{
public:
void addItem()
{
n.addChild();
}
Node n;
};

int main()
{
Database d;
}
 
?

=?iso-8859-1?q?Elias_Salom=E3o_Helou_Neto?=

Hi,

In the following code I would like that the class Node is only be used by
the class Database. So the idea was to make the interface protected and
use the friend definition. The code does not compile because the list
needs an public destructor. Making list<Node> a friend of Node doesn't
help.

It does if done the right way. Try friend std::list< Node >. The
following should compile:

#include <list>

using namespace std;

class Node
{
public:
protected:
void addChild()
{
Children.push_back(Node());
}

private:
list<Node> Children;

friend class Database;
friend class std::list< Node >;

};

class Database
{
public:
void addItem()
{
n.addChild();
}
Node n;

};

int main()
{
Database d;
}
 
T

Thomas Grund

Am 07.11.2007, 01:54 Uhr, schrieb Elias Salomão Helou Neto
It does if done the right way. Try friend std::list< Node >. The
following should compile:

#include <list>

using namespace std;

class Node
{
public:
protected:
void addChild()
{
Children.push_back(Node());
...

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!!!

Thank you

Thomas
 
?

=?iso-8859-1?q?Elias_Salom=E3o_Helou_Neto?=

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ão Helou Neto.
 
T

Thomas Grund

....
friend class Database;
friend class __gnu_cxx::new_allocator< Node >;
};
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ão Helou Neto.


This helped! - at least to get an idea. I definitly need a
platform-independend solution but it might be possible to write a special
allocator class and use this instead of the platform-dependend one. I'm
trying this next week and will write again when I have some results ...

Thanks!

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top