473,473 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

protected constructor, std::list and friends

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<Nodea 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<NodeChildren;

friend class Database;
};

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

int main()
{
Database d;
}
Nov 6 '07 #1
4 2079
On 6 nov, 11:28, "Thomas Grund" <thomas.gr...@mail.comwrote:
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<Nodea 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<NodeChildren;

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

};

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

};

int main()
{
Database d;
}
Nov 7 '07 #2
Am 07.11.2007, 01:54 Uhr, schrieb Elias Salomão Helou Neto
<es*****@gmail.com>:
On 6 nov, 11:28, "Thomas Grund" <thomas.gr...@mail.comwrote:
>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<Nodea 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());
...
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
Nov 7 '07 #3
On Nov 7, 2:59 am, "Thomas Grund" <thomas.gr...@mail.comwrote:
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<NodeChildren;

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.

Nov 9 '07 #4
09.11.2007, 02:16 Uhr, Elias Salomão Helou Neto wrote:
....
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
Nov 9 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Pemberton | last post by:
I'm sure there's a good explanation for this effect, but I get rather a strange output from this little test: #include <iostream> #include <list> int main() { std::list<int> int_list;
8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
5
by: Eric Lilja | last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope it's readable at least): #include <algorithm> #include <iostream> #include <vector> class Row { public:
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
7
by: alex221 | last post by:
In need to implement a tree structure in which every node has arbitrary number of children the following code has come into mind: using std::list; template < class Contents class Tree_node{ ...
8
by: Spoon | last post by:
Hello, Could someone explain why the following code is illegal? (I'm trying to use a list of (C-style) arrays.) #include <list> typedef std::list < int foo_t; int main() { int v = { 12, 34...
7
by: TBass | last post by:
So I have a class: class Client { unsigned int ClientID; .... }; class MyListenSocket
0
by: Javier | last post by:
Hi all, I have this code: class A { std::list<Bm_observadores; void function() {
12
by: isliguezze | last post by:
template <class T> class List { public: List(); List(const List&); List(int, const T&); void push_back(const T &); void push_front(const T &); void pop_back();
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.