I have a simple class
class Foo {
public:
float m;
Foo();
}
Foo::Foo(){
this->m = 1.0f;
}
Then I’m extending it with
class Bar: public Foo {
public:
float m;
Bar()
}
Bar::Bar(){
this->m = 10.0f;
}
I then instantiate Bar() but Bar.m is still 1.0f. Is there a reason for this?
In C++, you cannot override a field. Only methods can be overridden. Consequently, your declaration of the variable
min the classBaris a new field that hides the base classFoo‘s version ofm.If you want to access
Foo‘smfromBar, then you could use this syntax:Which explicitly tells the compiler to write to
Foo‘s version ofm. Alternatively, you can drop thethis->and just writeHope this helps!