Jump to content
  • Advertisement
Sign in to follow this  
DpakoH

[C++] pointer's issue

This topic is 3803 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi all, so i have a struct like this :
struct node
{
  int key_value;
  node *left;
  node *right;
};

to represent a node in a binary tree and fucntion :
search(int key,node* root);

to search for certain node with a certain key value in a simple tree. now what i want to do is to have a function :
search_template(int key,template_pointer* root)

which has for 2nd parameter a pointer , that is not defined as a type. so i could be able to call the function like this
search(10,node* root);
//or
search(10,node_2* root);
//or
search(10,node_3* root);
// where node, node_2, node_3 are different types of structures but what i 
// know is that they have the same properties (like the basic node struct) 

so my question is what kind of pointer can i used for this thing, i knew i read about something like this some time ago but i cant find it now so i need some help. any help will be greatly appreciated !! best, y.

Share this post


Link to post
Share on other sites
Advertisement
If you're using C++ try:


template <typename Key, typename Node>
Node *search(const Key& key, const Node *root);

Share this post


Link to post
Share on other sites
The problem above has been corrected.

Please, DpakoH, even if you lack confidence in our forum system, can you try to trust it a bit? Like, wait for a few seconds before you post the same question again [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by DpakoH
*** Source Snippet Removed ***
// where node, node_2, node_3 are different types of structures but what i
// know is that they have the same properties (like the basic node struct)


Make a base class, base_node, that each of the other classes (node, node_1, node_2) inherit from that contains the basic properties that all nodes have and that make it searchable. Then you can pass a base class pointer for each node type to a single search function.

Share this post


Link to post
Share on other sites
hi again,

first, sorry for the multiple posts , but i having some problems with my browser (Opera). it wasnt loading anything from gamedev.net so i pressed the post button several times, obviously it is my error (i thought the server is down or smth like this). however, i apologize again !!!


now back to my issue. i read your replies but they didnt really help me out. BUT this is because i wasnt clear enough in my initial post.
so lets explain again . i want to have this function :

search(int key, node* root);


but it is kind of part of library with some functions, so i dont have in this library the struct(node). so it has to work with whatever parameter it is used instead of "node* root". basically, to work with every pointer given as a parameter(of course this pointer should link to some data structure that resembles my node structure , but this is not a probem).

so i searched a little bit more and found that pointers can be define as follows:

void* pointer;



which is kind of undefined(as type) pointer . and now i am wondering if i can write my function like this :

search(int key, void* root);


if anyone is more familiar with this usage of pointers , please help me out . give me a piece of advice on this :)

i am going to try it out , but still any help will be greatly appreciated.


y.

PS : i am still checking rozz666's reply, what he said could be very useful, but i am not sure. still reading about it

Share this post


Link to post
Share on other sites
No. If your function declaration looks like:
search(int key, void* root);
, and "root" could be anything, how in the world would that be implemented? What would the body of the function look like?
root->????;
SomeClass* root_cast = reinterpret_cast<SomeClass*>(root); // What is SomeClass?

The function executing the search needs to know what it is working on by giving it a defined type as a parameter, whether this type be templated, a base class, whatever.

Why not try this?
class base_node
{
// Stuff needed by all nodes to they can be searched.
int key_value;
node *left;
node *right;
};

class node_1 : public base_node
{
// 'node_1' specific stuff
}

class node_2 : public base_node
{
// 'node_2' specific stuff
}

search(int key, base_node* root);

node_1 FirstNodeType;
node_2 SecondNodeType;

search(key, &FirstNodeType);
search(key, &SecondNodeType);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!