LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ templated Node class: pointers to different instantated class types (https://www.linuxquestions.org/questions/programming-9/c-templated-node-class-pointers-to-different-instantated-class-types-578464/)

jhwilliams 08-20-2007 01:11 PM

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!

JoeyAdams 08-20-2007 02:51 PM

Code:

vector<void*> children;
This by itself should not cause an error. It might be what you are doing with it.

tuxdev 08-20-2007 02:55 PM

I think what you are looking for is boost::any, but something smells wrong about what you're trying to do.

graemef 08-20-2007 06:20 PM

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.


All times are GMT -5. The time now is 04:34 AM.