I get no compile errors but the _Move function does not 'do something' when the condigition is there unless i remove it while i do call the _ConfigureAxis before i call the _Move function. Does anyone have an idea on what is going on here? I'm kind of lost here, the same happens for some other values as well.
You are right, but in my derived class i also write the Configured boolean. Shown below.
The point was that you were not providing enough information to diagnose the problem (and an example was provided that illustrated this.) You saying that particular example isn't germane still doesn't provide enough information/context/code to diagnose the problem.
There is nothing wrong with the code you supplied, so the problem is elsewhere.
Thank you for your reply everyone, i have found something that might mess up everything. I just don't understand the reasoning behind it so maybe someone can explain this to me?
#include <iostream>
class test {
public:
virtualinlinevoid _ConfigureAxis() { _MotorData.Configured = true; };
virtualinlinevoid _Move() {
if (_MotorData.Configured)
std::cout << "Move - configured\n";
else std::cout << "Move - not configured\n";
};
protected:
struct MotorData { bool Configured = false; };
MotorData _MotorData;
};
class testy : public test{
public:
inlinevoid _ConfigureAxis() { _MotorData.Configured = true; }; // Here i need to set test::_MotorData.Configured = true in order for it to work.
struct MotorData : public test::MotorData {
int testdata = 0;
};
MotorData _MotorData;
};
int main() {
test t;
t._ConfigureAxis();
t._Move();
testy s;
s._ConfigureAxis();
s._Move();
}
Can anyone explain why this doesnt work? I though it would append the new variable and then make a new variable. However i need to put test::_MotorData.Configured = true in order for this to work. Does this mean that everything in the derived class has its own variables if declared again in the derived clasS?
Your class testy. It contains an object of type test::MotorData , named _Motordata, and an object of type testy::Motordata, named _Motordata.
Two different types, same name. Two different instances of those different types, same name. If you're in class testy, and you want to use the parent _Motordata, you have to specify that you're using the parent one.
This is just a bad idea all round. Do not create objects with the same name.
include <iostream>
usingnamespace std;
class test {
public:
virtualinlinevoid _ConfigureAxis() { _MotorData.Configured = true; };
virtualinlinevoid _Move() {
if (_MotorData.Configured)
std::cout << "Move - configured\n";
else std::cout << "Move - not configured\n";
};
protected:
struct MotorData { bool Configured = false; };
MotorData _MotorData;
};
class testy : public test{
public:
inlinevoid _ConfigureAxis() { _MotorData.Configured = true; }; // Here i need to set test::_MotorData.Configured = true in order for it to work.
struct MotorData2 : public test::MotorData {
int testdata = 0;
};
MotorData2 _MotorData2;
};
int main() {
test t;
t._ConfigureAxis();
t._Move();
testy s;
s._ConfigureAxis();
s._Move();
}