Working with void*

This is a discussion on Working with void* within the C++ Programming forums, part of the General Programming Boards category; Hey there; I've been working on a really simple class for a void pointer linked list. I'm looking for a ...

  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
    21,401
    Have you considered the use of templates?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 07: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, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21