Go to Post Someday, when I grow up, I want to join the GDC so I can make people go crazy, and then release hints that get people even crazier. - unionylibertad [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
 
Thread Tools Rating: Thread Rating: 6 votes, 4.50 average. Display Modes
  #1   Spotlight this post!  
Unread 01-14-2009, 03:43 PM
byteit101's Avatar
byteit101 byteit101 is offline
WPILib maintainer (WPI)
AKA: Patrick Plenefisch
no team (The Cat Attack (Formerly))
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Worcester
Posts: 699
byteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of light
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...
You can probably see what i am doing, (accessing different function based on sensor type), and Nothing has worked

Last edited by byteit101 : 01-14-2009 at 03:45 PM. Reason: code bock
Reply With Quote
  #2   Spotlight this post!  
Unread 01-14-2009, 03:51 PM
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,033
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
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.
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff

Last edited by virtuald : 01-14-2009 at 03:51 PM. Reason: Ooops
Reply With Quote
  #3   Spotlight this post!  
Unread 01-14-2009, 03:58 PM
jee7s jee7s is offline
Texan FIRSTer, ex-frc2789, ex-frc41
AKA: Jeffrey Erickson
FRC #6357
 
Join Date: Nov 2007
Rookie Year: 1997
Location: Dripping Springs, TX
Posts: 298
jee7s has a reputation beyond reputejee7s has a reputation beyond reputejee7s has a reputation beyond reputejee7s has a reputation beyond reputejee7s has a reputation beyond reputejee7s has a reputation beyond reputejee7s has a reputation beyond reputejee7s has a reputation beyond reputejee7s has a reputation beyond reputejee7s has a reputation beyond reputejee7s has a reputation beyond repute
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
If you want to use the void* you need to recast it for the compiler to know what you are doing. So, you need a recast for each different sensor. This leads to all sorts of extra code that you avoid by going the polymorphic route.

-Jeff Erickson, FRC 41
Reply With Quote
  #4   Spotlight this post!  
Unread 01-14-2009, 03:59 PM
byteit101's Avatar
byteit101 byteit101 is offline
WPILib maintainer (WPI)
AKA: Patrick Plenefisch
no team (The Cat Attack (Formerly))
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Worcester
Posts: 699
byteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of light
Re: void* pointer to object in C++

Thanks! I'll Try that!
Reply With Quote
  #5   Spotlight this post!  
Unread 01-14-2009, 04:28 PM
slavik262's Avatar
slavik262 slavik262 is offline
We do what we must because we can.
AKA: Matt Kline
FRC #0537 (Charger Robotics)
Team Role: Alumni
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Sussex, WI
Posts: 310
slavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to behold
Send a message via AIM to slavik262
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.
__________________
Reply With Quote
  #6   Spotlight this post!  
Unread 01-14-2009, 04:41 PM
byteit101's Avatar
byteit101 byteit101 is offline
WPILib maintainer (WPI)
AKA: Patrick Plenefisch
no team (The Cat Attack (Formerly))
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Worcester
Posts: 699
byteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of light
Re: void* pointer to object in C++

like (Gyro)MyPointer->GetValue();?
or ((Gyro)MyPointer)->GetValue();?
or ((Gyro)(*MyPointer))->GetValue();?
Reply With Quote
  #7   Spotlight this post!  
Unread 01-14-2009, 05:15 PM
heydowns's Avatar
heydowns heydowns is offline
Registered User
AKA: Jeff Downs
FRC #1511 (Rolling Thunder)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Ra-Cha-Cha
Posts: 142
heydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond repute
Re: void* pointer to object in C++

Quote:
Originally Posted by byteit101 View Post
like (Gyro)MyPointer->GetValue();?
or ((Gyro)MyPointer)->GetValue();?
or ((Gyro)(*MyPointer))->GetValue();?
None of the above.
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.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Aiming that remote: Laser pointer allowed? efoote868 Rules/Strategy 9 03-02-2008 09:01 PM
Pointer to pwm bronxbomber92 Programming 5 02-07-2008 03:57 PM
Suspicious pointer conversion ebowla Programming 2 01-26-2008 12:13 PM
laser pointer 1574aviad Technical Discussion 9 02-20-2005 08:25 PM
problem with void Process_Data_From_Local_IO CmptrGk Programming 2 01-29-2005 05:14 PM


All times are GMT -5. The time now is 10:19 AM.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi