Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-20-2007, 02:11 PM   #1
Senior Member
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
C++ templated Node class: pointers to different instantated class types

I have made a templated class, Node<K,D>.

Say I have some code that goes:

Node<int,int> ham(1,1);
Node<char,char> potato('n','e');
Node<double,double> gun(3.0,4.44);
and then I want to make potato and gun children of the ham node.

If i declare the node pointers as

vector<Node<K,D>*> children;
then I can only point to nodes of type Node<K,D>, so ham could not point to potato and gun. Perhaps the solution is to use void pointers - but

vector<void*> children;
returns this error:

error: `void*' is not a pointer-to-object type
How do you suggest I expand the functionality of my Node pointers so that they can point to any Node, regardless of the type of keys or data it possesses?


Last edited by jhwilliams; 08-20-2007 at 02:12 PM.
Old 08-20-2007, 03:51 PM   #2
Registered: Jun 2006
Distribution: Kubuntu Hardy
Posts: 94

Rep: Reputation: 15
vector<void*> children;
This by itself should not cause an error. It might be what you are doing with it.
Old 08-20-2007, 03:55 PM   #3
Senior Member
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
I think what you are looking for is boost::any, but something smells wrong about what you're trying to do.
Old 08-20-2007, 07:20 PM   #4
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I can't say that I recall having done anything like that before, but using void * should work although it would mean that you will need to cast back to the appropriate class when you remove it from the vector. I'd suggest that you have a parent class called (for example) Object which each of the other classes inherit from and then you just need to have a vector of Objects.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Templated Class and Borland C++ Compiler enemorales Programming 2 09-25-2010 03:30 PM
C++ - problem with pointers to class member functions Nylex Programming 3 01-15-2006 11:14 AM
c++ list class, type of node? exodist Programming 2 05-20-2004 07:27 PM
node class help... leroy27336 Programming 1 02-25-2004 02:35 PM
arrays of pointers to class objects tdurden Programming 5 12-16-2002 11:47 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:29 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration