Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    from void* back to an object

    now here's the problem
    i'm trying to make a linked list store different types of data object (which i don know what they r now)
    so i used void* like this
    //for example a node of int
    node<userDefindData>* n;
    void* ptr=n;
    and now i have the address of my node in my ptr ,but how can i treat this pointer as an object
    and call its functions???????

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    You don't. You'd have to do some sort of typecasting and checking of object type, depending on what language you're using. What is that, C++?

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes
    but what type checking if i don know the types and trying to make generic class???

  • #4
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    If you have a pile of void* objects and you have no idea at compile time what they are it's simply not possible to do anything with them directly in a manner that won't break in certain cases. You can sort them out by passing them to methods with multiple overrides, in which case the most appropriate method will be called for the pointer type, but again if you have no idea whatsoever what's in the pile you can't write a method for every possibility. Likely you do have a pretty good idea about what possible types will end up in the collection. Build a base class for all these types that contains a common interface (or at least one virtual method that returns an object type, if necessary [this gets kind of ugly]). Store pointers to the base class instead of to void.

    Code:
    #include <vector>
    #include <iostream>
    
    namespace MyStuff
    {
    
    class Base
    {
    public:
    	virtual ~Base() { };
    	virtual void do_junk() = 0; 
    };
    
    class DerivationOne : public Base
    {
    public:
    	~DerivationOne() { };
    	void do_junk()
    	{
    		std::cout << "Doing junk in derivation one\n";
    	}
    };
    
    class DerivationTwo : public Base
    {
    public:
    	virtual ~DerivationTwo() { }
    	virtual void do_junk()
    	{
    		std::cout << "Doing junk in derivation two\n";
    	}
    };
    
    class DerivationThree : public DerivationTwo
    {
    public:
    	~DerivationThree() { };
    	void do_junk()
    	{
    		std::cout << "Doing junk in derivation three\n";
    	}
    };
    
    } // MyStuff::
    
    
    // Store pointers to base
    typedef std::vector<MyStuff::Base*> MixedContainer;
    int main(int, char**)
    {
    	MixedContainer test;
    	// Push derived classes
    	test.push_back(new MyStuff::DerivationOne());
    	test.push_back(new MyStuff::DerivationTwo());
    	test.push_back(new MyStuff::DerivationThree());
    
    	// Use common methods, the most derived version will be called
    	for (MixedContainer::iterator it = test.begin(); it != test.end(); ++it)
    	{
    		(*it)->do_junk();
    		delete *it;
    	}
    
    	return 0;
    }

  • #5
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Quote Originally Posted by mad_girl View Post
    yes
    but what type checking if i don know the types and trying to make generic class???
    C++.NET 2.0 and above should support real generics (C# does, I don't code much in C++). Don't use pointer to void, as ralph pointed out. If you don't want to (or can't) use .NET generics, store a pointer to the base class.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •