|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools |
Rating:
|
Display Modes |
|
#1
|
||||
|
||||
|
void* pointer to object in C++
I have been at this for some time now, and this won't compile:
Code:
(function(int data))
void * MyPointer
if (data=0)
{
Gyro gy(1);
MyPointer = &gy;
}
if (data=1)
{
Ultrasonic gy(1);
MyPointer = &gy;
}
if (etc...
etc...
(function2(int data2))
if (data2=0)
return MyPointer->GetValue(); //this produces error: 'void*' is not a pointer-to-object type (:eek:)
etc...
![]() Last edited by byteit101 : 14-01-2009 at 15:45. Reason: code bock |
|
#2
|
||||
|
||||
|
Re: void* pointer to object in C++
Don't use a void pointer, use a pointer to the base class. So for those I think its SensorBase.
Last edited by virtuald : 14-01-2009 at 15:51. Reason: Ooops |
|
#3
|
|||
|
|||
|
Re: void* pointer to object in C++
To be in line with the strictest and most proper C++ design principles (a very good idea), you don't want to use void* in the scenario you describe. Rather, you want to take advantage of polymorphism and use an abstract base class pointer instead.
For example, if all your sensors derive from a single abstract base class (like the WPILib classes do), then you can use a pointer to the base class to access the inherited class functions. I assume that GetValue is inherited from SensorBase and is virtual. ( I don't have the library in front of me at the moment.) Therefore, rather than using a void* you can use a SensorBase* like this: Code:
Gyro gy(1); Ultrasound us(2); SensorBase* mySensor; mySensor = &gy; mySensor->GetValue(); //mySensor calls Gyro::GetValue() here mySensor = &us; mySensor->GetValue();//mySensor calls Ultrasound::GetValue() here -Jeff Erickson, FRC 41 |
|
#4
|
||||
|
||||
|
Re: void* pointer to object in C++
Thanks! I'll Try that!
|
|
#5
|
||||
|
||||
|
Re: void* pointer to object in C++
And for future reference, void pointers need to be casted to the correct pointer type in order to be used. This should fix your error.
|
|
#6
|
||||
|
||||
|
Re: void* pointer to object in C++
like (Gyro)MyPointer->GetValue();?
or ((Gyro)MyPointer)->GetValue();? or ((Gyro)(*MyPointer))->GetValue();? |
|
#7
|
||||
|
||||
|
Re: void* pointer to object in C++
Quote:
Your pointer is the wrong type. So it's the pointer you have to cast to the correct type of pointer: ((Gyro *)MyPointer)->GetValue() But you don't want to do this Do as suggested in the prior posts as it is much cleaner and less error-prone. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Aiming that remote: Laser pointer allowed? | efoote868 | Rules/Strategy | 9 | 02-03-2008 21:01 |
| Pointer to pwm | bronxbomber92 | Programming | 5 | 07-02-2008 15:57 |
| Suspicious pointer conversion | ebowla | Programming | 2 | 26-01-2008 12:13 |
| laser pointer | 1574aviad | Technical Discussion | 9 | 20-02-2005 20:25 |
| problem with void Process_Data_From_Local_IO | CmptrGk | Programming | 2 | 29-01-2005 17:14 |