View Single Post
  #6   Spotlight this post!  
Unread 18-12-2008, 19:13
BradAMiller BradAMiller is offline
Registered User
AKA: Brad
#0190 ( Gompei and the Herd)
Team Role: Mentor
 
Join Date: Mar 2004
Location: Worcester, MA
Posts: 592
BradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant future
Re: Pointers and Tank Drive?

Quote:
Originally Posted by Straberrie View Post
void OperatorControl(void)
{
while (1)
{
Bot->TankDrive(&stickLeft, &stickRight);
}
}
};


I tried that and here are the errors that r showing up! The only line highlighted as having an error is "Bot->TankDrive(&stickLeft, &stickRight);"
This is confusing.

There are two ways of declaring objects: either as an instance of the object or a pointer to the object. In the case of the instance the variable represents the object, and it is created at the time of the declaration. In the case of a pointer you are only creating the space to store the address of the object, but the object remains uncreated. With pointers you have to create the object using the new operator. Look at these two snippets of code to see the difference.
Code:
Joystick stick1(1);   // this is an instance of a Joystick object called stick1
stick1.GetX();         // instances are dereferenced using the dot (.) operator
bot->ArcadeDrive(stick1);  // and can be passed to methods as a reference

Joystick * stick2;    // this is a pointer to an uncreated Joystick object
stick2 = new Joystick(1);  // this creates the instance of the Joystick object
stick2->GetX();       // pointers are dereferenced with the arrow (->)
bot->ArcadeDrive(stick2);  // and can be passed as pointers (notice, no &)
A friend just suggested that this example itself might be a little confusing, and this is going to get kind of geeky. The ArcadeDrive method in the library is taking advantage of a feature of C++ called function overloading. This allows us to have two methods with the same name that differ by the argument list. In the first ArcadeDrive(stick1), the variable stick1 is passed as a reference to a Joystick object. In the second ArcadeDrive(stick2), it is being passed as a pointer to a Joystick object. There are actually two methods in the RobotDrive object, both called ArcadeDrive that each take a different type of argument. The cool thing is that the compiler figures out which one to call. The library is built this way to let it adopt with the style of programmers that prefer to use pointers while at the same time accommodating those who prefer to use references.

Does this help?
__________________
Brad Miller
Robotics Resource Center
Worcester Polytechnic Institute

Last edited by BradAMiller : 18-12-2008 at 19:28. Reason: improved explanation