LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

Rep: Reputation: 211Reputation: 211Reputation: 211
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:

Code:
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

Code:
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

Code:
vector<void*> children;
returns this error:

Quote:
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?

Thanks!

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

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

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, 06:20 PM   #4
graemef
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.
 
  


Reply



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 02:30 PM
C++ - problem with pointers to class member functions Nylex Programming 3 01-15-2006 10:14 AM
c++ list class, type of node? exodist Programming 2 05-20-2004 06:27 PM
node class help... leroy27336 Programming 1 02-25-2004 01:35 PM
arrays of pointers to class objects tdurden Programming 5 12-16-2002 10:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:07 PM.

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