I just saw
your other thread - there are a lot of subtle differences between C++ and Java, and the ampersand is one of them. In your Java class...
Code:
// LimitSwitch.java
public class LimitSwitch {
protected DigitalInput di;
public LimitSwitch(int portNumber) {
di = new DigitalInput(portNumber);
}
public boolean isOpen(){
if (di.get() == true) {
return true;
} else return false;
}
public boolean isClosed(){
return (!isOpen());
}
}
...
di is
not a
DigitalInput, it's actually a reference to a
DigitalInput.
Unlike Java, in C++ object variables hold value types. In C++ there are
references, which are sort of the equivalent to what you are used to in Java. The ampersand you're seeing indicates this reference type. For example, the
const DigitalInput & in the
DigitalInput constructor prototype means you should pass a reference to a
DigitalInput object; not an actual
DigitalInput object instance variable. This is conceptually similar to C-style pointers (which are also available in C++).
Here's how you might translate your Java class using a pointer to match the Java semantics:
Code:
// C++
#include "WPILib.h"
class LimitSwitch
{
DigitalInput *di;
public:
LimitSwitch(UINT32 p) // constructor
{
di = new DigitalInput(p);
}
~LimitSwitch() // destructor
{
delete di;
}
bool GetState()
{
return di->Get();
}
};
There are a lot of other differences between C++ and Java; I'd recommend looking at the following resources:
Again, hope this helps
