|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Using a variable as a class object in Windriver/C++
Hello, I am currently working on streamlining our teams code and was wondering if it is possible to use a variable to reference a specific object.
For Example Say I named a solenoid object 1 and had a int variable named b that stored 1 could i say b.Set(true) and have the same result as 1.Set(True) If it cannot be done this way is there another way to get the same result. The key to what I want is to have a numeric value that relates to a specific object. Thanks for any help in advance. ![]() |
|
#2
|
|||||
|
|||||
|
Re: Using a variable as a class object in Windriver/C++
Try a map.
Code:
#include <map> ... map<int, Solenoid*> solenoids; solenoids[1] = new Solenoid(1); ... |
|
#3
|
|||
|
|||
|
Re: Using a variable as a class object in Windriver/C++
A map is the only way to "map" (hint hint nudge nudge) integer values to solenoid references.
|
|
#4
|
||||
|
||||
|
Re: Using a variable as a class object in Windriver/C++
Why doesn't an array work?
Code:
Solenoid* solenoids[3]; solenoid[0] = new Solenoid(5); //... |
|
#5
|
|||
|
|||
|
Re: Using a variable as a class object in Windriver/C++
It does, at the penalty of having a fixed number of spots with fixed names to map to unless additional code is written.
To be clear, I was referring to the data type map, not necessarily the c++ implementation. I can count at least 8 ways in C++ to implement the map with varying degrees of effectiveness, map and hash_map (assuming that library is included in Windriver by default) being the most flexible with mapping names. Point being, the map structure could accept any valid integer and associate the corresponding solenoid with it; with an array, you are forced to associate a solenoid with a specific value from 0 to size - 1. |
|
#6
|
|||
|
|||
|
Re: Using a variable as a class object in Windriver/C++
Use an array. The amount of solenoids you have is known, so the associative array (map) doesn't add any value.
|
|
#7
|
|||
|
|||
|
Re: Using a variable as a class object in Windriver/C++
Quote:
The best generic way to do this is to simply name variables or pointers for each mechanism and use those to refer to the solenoids or anything else. Such as: Code:
Solenoid *leftGripper = new Solenoid(2); Solenoid *impaler = new Solenoid(3); impaler->Set(true); Code:
class MyRobot : public IterativeRobot
{
...
Solenoid leftGripper;
Solenoid impaler;
...
MyRobot() :
leftGripper(2),
impaler(3)
{
}
...
void AutonomousInit(void)
{
impaler.Set(true);
}
...
};
Do you have some compelling reason not to refer to each object directly? |
|
#8
|
||||
|
||||
|
Re: Using a variable as a class object in Windriver/C++
Last year, we wanted to program the robot to set all the solenoids on to power the line tracking sensors. We just looped through each of the ports (it was in labview though).
|
|
#9
|
|||
|
|||
|
Re: Using a variable as a class object in Windriver/C++
Well, if you had an array of solenoid pointers for this, it'd just be something simple as having:
for(int solenoidPort = 0; 0 < 8; solenoidPort++){ solenoids[solenoidPort]->set(true); } |
|
#10
|
|||
|
|||
|
Re: Using a variable as a class object in Windriver/C++
Thanks for all the imput. I was wondering if any of you could elaborate on how to implement a solenoid array.
|
|
#11
|
|||||
|
|||||
|
Re: Using a variable as a class object in Windriver/C++
See WizenedEE's first reply. I think we could provide more useful feedback if you could post your team's code.
|
|
#12
|
||||||
|
||||||
|
Re: Using a variable as a class object in Windriver/C++
Quote:
However, trying to do something like that to for anything less trivial would greatly trade readability and maintainability for compactness, which for FIRST is not something I would try to do. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|