Thread: Working with void*

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Working with void*

    Hey there; I've been working on a really simple class for a void pointer linked list. I'm looking for a way to deal with having to convert to void* and then back to whatever type I'm using. For example:

    My class:
    Code:
    struct GSLLNODE {
    	void* dat;
    	GSLLNODE* next;
    };
    
    class GSLL {
    	private:
    	GSLLNODE *root;
    
    	public:
    	GSLL() {root = new GSLLNODE; root->next = 0;}
    
    	void Next(void* dat);
    	void* GetNext();
    };
    
    void GSLL::Next(void* dat) {
    	GSLLNODE *n = new GSLLNODE;
    	GSLLNODE *cur = root;
    
    	n->dat = dat;
    	n->next = 0;
    
    	while(cur->next) 
    		cur = cur->next;
    
    	cur->next = n;
    }
    
    void* GSLL::GetNext() {
    	if(!root->next)
    		return 0;
    	root = root->next;
    	return root->dat;
    }
    Code:
    int main() {
    	GSLL gg;
    	int f = 0;
    
    	gg.Next((void*)++f);
    	gg.Next((void*)++f);
    	gg.Next((void*)++f);
    	gg.Next((void*)++f);
    	gg.Next((void*)++f);
    	gg.Next((void*)++f);
    
    	f = (int)gg.GetNext();
    	while(f)	{
    		std::cout << f;
    		f = (int)gg.GetNext();
    	}
    	std::cin >> f;
    	return 0;
    }
    I suppose I'm looking for some sort of auto-interface to cast the void pointer automatically? It'd help cut the amount of neccesary coding drastically.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you considered the use of templates?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Typically when using void pointers, it is left to the programmer to know what type they are. And as a side note, in your GSLL::Next function, you're passing in an integer as a void pointer -- I doubt you will be able to use the same code for a linked list of floats/doubles.

    C++ offers a few things you can use to "auto-interface to cast the void pointer automatically". As laserlight said, a Template would probably be easiest here. Another way would be, instead of using void pointers, have it as a list of a certain interface. Anything you want to be in the list would have to implement that interface in order for the list to use it. This wouldn't be practical for lists of primitive types, but if you're going to be having lists of objects.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM