![]() |
void* pointer to object in C++
I have been at this for some time now, and this won't compile:
Code:
(function(int data)) |
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.
|
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);-Jeff Erickson, FRC 41 |
Re: void* pointer to object in C++
Thanks! I'll Try that!
|
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.
|
Re: void* pointer to object in C++
like (Gyro)MyPointer->GetValue();?
or ((Gyro)MyPointer)->GetValue();? or ((Gyro)(*MyPointer))->GetValue();? |
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. |
| All times are GMT -5. The time now is 10:19 AM. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi