Skip to main content
gamedev.net
🔒 Locked

Pointer conversion

Started by Kamos 4 replies 900+ views
Original Post
Kamos
Kamos
Hello, I've been looking around here for solutions to my problem but I can't find any. So I'll make a new thread: I need a pointer, but it's not sure where it's going to point to. It may be going to point to an object of class SomeClass1 or to an object of class SomeClass2, etc. Here is some code of what i'm trying to do:

class SomeClass
{
  public:
  int a;
}

void main()
{
  void * FlexiblePointer;   // not pointing to an object of a specific class yet.
  FlexiblePointer = new SomeClass;
    // Now this FlexiblePointer is pointing to a SomeClass object.
    // Unfortunetely the type of the FlexiblePointer is still (void *)
    // So I need to cast it (SomeClass *), so I can access its data.
  FlexiblePointer = (SomeClass *)(FlexiblePointer);
    // Now that it's cast to (SomeClass *), I should be able to access its data:
  (*FlexiblePointer).a = 15;
}



The last line: (*FlexiblePointer).a = 15; gives me a compiler error: E2109 Not an allowed type I remember from delphi that you can cast an object (basically a pointer) to anything you'd like and if the conversion doesn't go properly you'll be accessing junk. Apparently it doesn't go this way in C++, could someone help me with this? Any help is greatly appreciated.
Evil Steve
Evil Steve
SomeClass* pClass = (SomeClass *)FlexiblePointer;
pClass->a = 15;
should work.

Although in C++ you should try to avoid this, and use some safer method like an abstract base class.
Enigma
Enigma
C++ is a statically typed language. When you declare FlexiblePointer to be a void * it will always be a void *.

If you have multiple classes which all have the same property it's likely they are related in some manner and as such an inheritance hierarchy is probably sensible. In this situation you make your FlexiblePointer a pointer to the base class and allocate objects of derived classes. The FlexiblePointer is still statically typed - you can only access the base class interface - but C++'s runtime polymorphism allows the derived classes to provide different behaviour depending on the dynamic type of the object pointed at by the FlexiblePointer (the type of the object you actually allocated), i.e.:

class Base{	public:		virtual ~Base(){}		virtual void set_a(int value) = 0;};class Derived1	:	public Base{	public:		virtual void set_a(int value)		{			a_ = value;		}	private:		int a_;};class Derived2	:	public Base{	public:		virtual void set_a(int value)		{			a_ = value / 5.0f;		}	private:		float a_;};int main(int argc, char * *){	Base * flexible_pointer;	if (argc == 1)	{		flexible_pointer = new Derived1();	}	else	{		flexible_pointer = new Derived2();	}	// will call Derived1::set_a or Derived2::set_a depending on the	// which type of object was actually created	flexible_pointer->set_a(7);	delete flexible_pointer;}

void *s are evil by the way.

Σnigma
Kamos
Kamos
Ah yes, ofcourse.
Thank you both!
Grain
Grain
Quote:
Original post by Evil Steve
SomeClass* pClass = (SomeClass *)FlexiblePointer;
pClass->a = 15;


or simply: ((SomeClass *)FlexiblePointer)->a = 15;
Ro_Akira
Ro_Akira
I presume Evil Steve was aiming for clarity. Which he succeeded in doing I might add.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.