I am mostly a Java and AS3 programmer but right now I am working in C++ for something and I stumbled upon a problem.
I have a base class (let’s call it ClassA) that has a private variable var1. In this class I have a method getVar1() that returns a pointer to that variable.
Next I have another class that extends Base (let’s call it ClassB). How do I call the getVar1() from the super class?
In java it is as simple as this.var1 = super.getVar1().
I read that it is like var1 = ClassA::getVar1(), but I am not sure if this is OK for work with variables (aka the pointers can change).
Thanks.
it’s ok to call it ClassA::getVar1().
However, if you want the java-way, you can make the method “virtual”. This means whenever you write getVar1(), it doesn’t depend on the Type you wrote in front of it (so at compile time) but it depends on the type of the object when you call it (at runtime). For this reason, c++ keeps an internal v-table to find the appropriate method. It’s equivalent to the java way and called late-binding.