...

View Full Version : from void* back to an object



mad_girl
09-17-2007, 03:24 PM
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???????

nikkiH
09-17-2007, 03:44 PM
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++?

mad_girl
09-17-2007, 03:46 PM
yes
but what type checking if i don know the types and trying to make generic class???

ralph l mayo
09-17-2007, 06:10 PM
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.



#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;
}

nikkiH
09-17-2007, 07:35 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum