Sign in to follow this  
slayemin

[Resolved] Question on pointers

Recommended Posts

slayemin    6100
I'll criticize myself before anyone else does: Yes, I *should* be using the STL. I created my own linked list class which uses templates and I tried to maximize its polymorphism. The linked list works great when I'm storing objects, and I combined linked lists with stacks and queues because they're so similar. However, I ran into a problem when I'm trying to store a list of pointers. Here's roughly the problem I'm having. It's probably a design/syntax misunderstanding.
//class member declaration for pop function
bool pop(T *&out);// Removes the head node from the list, stores it in 'out' param
bool push(T *Data);
....

//variable declaration
List<Connection*> History;
....
//I already have a list of objects called 'connection'. I just want to store a stack of pointers to those objects. Here's how I'm adding to history:
Connection *Temp;

//Temp is a pointer to an object in the stack. The stack is unchanged.
ok = CurrentNode->NodeList.peeknext(Temp,true);

while(ok)
{
	//did we find the right node to follow?
	if(Temp->Child->Value == val)
	{
	//traverse down the tree
	CurrentNode = Temp->Child;
	History.push(&Temp);  //     <------------HERE-----(it's wrong somehow)
	break;
	}
	else
	{
	//go to the next node in the list
	ok = CurrentNode->NodeList.peeknext(Temp);
	}
}

.....
/*And here's where I use the pop function:*/

//ConPointer is supposed to be the pointer which will store the value retrieved
//from the pop() function. 
Connection **ConPointer; // <---I just wanted a pointer to a pointer...
bool WasCompMove;
	
if(win)
{
	//the last move was made by the computer
	WasCompMove = true;

	while(History.pop(ConPointer))
	{
		if(WasCompMove)
			(*ConPointer)->Weight += .25;
		else
			(*ConPointer)->Weight -= .25;

		WasCompMove ? WasCompMove = false : WasCompMove = true;
	}

}
Now, syntactically, my code is correct. However, the problem is that when I push the address of Temp into the history stack, I'm not actually pushing the pointer to the "connection" object. I'm pushing the address of Temp, which remains the same every time. This isn't what I want... I really want to do this: History.push(Temp); But that gives me this syntax error: "cannot convert parameter 1 from 'Connection *' to 'Connection **'" Arg! What's the right way to do what I'm trying to do? A) Is my knowledge of pointers & reference syntax lacking? Is it something really small? B) Should I try to write overloaded push and pop functions which deal exclusively with pointers? C) Should I give up and just use STL vectors? (or whatever appropriate data structure) [Edited by - slayemin on December 19, 2008 9:25:39 AM]

Share this post


Link to post
Share on other sites
ToohrVyk    1596
I don't get it. You define push(T*), then you instantiate T as Connection*, which means the definition becomes push(Connection**). Therefore, you should pass a Connection**. Where is the problem?

If you want push(T) to be 'special' when T is a pointer, use template specialization. But I don't see the point: you would be doing the same operations anyway.

Share this post


Link to post
Share on other sites
slayemin    6100
So, if I want to store a list of pointers to pointers rather then a list of pointers to objects, you're saying that I have to overload my push and pop member functions?

I also don't see the point since the operations would be exactly the same anyways... that's why I'm trying to avoid needless code rewrite/bloat.

Share this post


Link to post
Share on other sites
ToohrVyk    1596
Quote:
Original post by slayemin
So, if I want to store a list of pointers to pointers rather then a list of pointers to objects, you're saying that I have to overload my push and pop member functions?
I'm not saying that. As it stands, if you wish to push "Foobar" into your list, you have to use "push(&Foobar)", because push() expects a pointer to the value to be stored. If you're unhappy with this, change your interface, but keeping it as-is will still work.

Share this post


Link to post
Share on other sites
slayemin    6100
Okay, I took a break and came back and figured it out in 15 seconds. I am such a dumbass sometimes... I was over thinking this. My linked list is already storing a list of pointers which happen to be pointing to objects in memory. Rather then creating a list of pointers to the pointers which point to the objects, I can just create a list of the pointers which point to the objects. I shouldn't ever have to create a list of pointers to pointers with my implementation... good to know...

Thanks for the help ToohrVyk!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this