Class values not being rememberd?

Hi All,

Sorry for the vague description but i wouldn't know how else to describe it. I'll try explaining my problem right now.

I have a class defined, somewhat like this:

class test {
public:
virtual inline void _ConfigureAxis() {
_MotorData.Configured = true;
};
virtual inline void _Move() {
if (_MotorData.Configured) {
// Do something
};
};

protected:
struct MotorData {
bool Configured = false;
};

MotorData _MotorData;
};

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.
Last edited on
if (_MotorData.Configured) means the same as if (_MotorData.Configured == true).

Since _MotorData.Configured starts out as false it will not "do something" if you call _Move() before calling _ConfigureAxis().
Note that names starting with an underscore followed by an uppercase letter are reserved to the implementation.
http://en.cppreference.com/w/cpp/language/identifiers#In_declarations

Also note that there is no need to put a semicolon after if statements and function definitions.
Your code suggest that test should be inherited from. One would assume it is in the types that inherit from test that the problem occurs in.

For instance, the code works as expected for t in the following code, but not for s:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

class test {
public:
  virtual inline void _ConfigureAxis() { _MotorData.Configured = true;};
  virtual inline void _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: 
  void _ConfigureAxis() {}
};


int main() {
  test t;
  t._ConfigureAxis();
  t._Move();

  testy s;
  s._ConfigureAxis();
  s._Move();
}
Hello cire,

You are right, but in my derived class i also write the Configured boolean. Shown below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

class test {
public:
  virtual inline void _ConfigureAxis() { _MotorData.Configured = true;};
  virtual inline void _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: 
  void _ConfigureAxis() { _MotorData.Configured = true; }
};


int main() {
  test t;
  t._ConfigureAxis();
  t._Move();

  testy s;
  s._ConfigureAxis();
  s._Move();
}



Peter87:

I will have a look at it, thanks!

Kind regards,

Bob

Edit: this is done for an arduino zero btw. Maybe this is usefull information.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

class test {
public:
  virtual inline void _ConfigureAxis() { _MotorData.Configured = true;};
  virtual inline void _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: 
  void _ConfigureAxis() { _MotorData.Configured = true; }
};


int main() {
  test t;
  t._ConfigureAxis();
  t._Move();

  testy s;
  s._ConfigureAxis();
  s._Move();
}


This code works as expected. Output is
1
2
Move - configured
Move - configured
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

class test {
public:
  virtual inline void _ConfigureAxis() { _MotorData.Configured = true; };
  virtual inline void _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: 
  inline void _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?

Kind regards,


Bob
Oh, look at this mess.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
include <iostream>
using namespace std;

class test {
public:
  virtual inline void _ConfigureAxis() { _MotorData.Configured = true; };
  virtual inline void _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: 
  inline void _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();
}

Last edited on
Topic archived. No new replies allowed.