1

I learnt the work of virtual functions: if the inherited classes inherit a function from the base class, and it is custom for each ones, I can call these functions with pointers that point to the base class, this way:

BaseClass* PointerName = &InheritedClassObject;

But what about variables? I found this question on the site that tells: I can't create virtual variables in C++. My experience proves it: for variables, Visual C++ says: 'virtual' is not allowed.

Then, what is the way to reach the value of a(n inherited) variable that belongs to an inherited class by using base class pointers?

Community
  • 1
  • 1
Zoltán Schmidt
  • 1,286
  • 2
  • 28
  • 48
  • How comes your base class knows that a variable is going to be added in *every* class derived from it? (And this variable will have the same type for each derived class etc.) – dyp Jul 20 '13 at 22:03
  • I want to call variables that are inherited from the base class, and consequently all the inherited class have them. – Zoltán Schmidt Jul 20 '13 at 22:05
  • 1
    Could you provide an example? It might be the case that there's a very simple solution to your problem. – dyp Jul 20 '13 at 22:07
  • @ZoltánSchmidt See my answer. I think there may have been some confusion with the wording of the question. if not, please tell me and I will delete my response. – Steve P. Jul 20 '13 at 22:11
  • @DyP My exact problem is that an expression like this: `PointerArray[Object.X][Object.Y] = &Object;` (where `Object` is an inherited class object and `X` and `Y` are inherited from base class) does nothing. The value of `PointerArray[Object.X][Object.Y]` is still the same, most certainly because neither of `X` or `Y` is called. @SteveP. Okay! – Zoltán Schmidt Jul 20 '13 at 22:13

3 Answers3

1

You don't. Virtual functions use them, do whatever needs to be done and return result if needed.

Tomek
  • 4,554
  • 1
  • 19
  • 19
1

Based off your comment, I think what you are trying to ask if how do child classes access their parent's variables. Consider this example:

class Parent 
{
public:
  Parent(): x(0) {}
  virtual ~Parent() {}

protected:
  int x;
};

class Child: public Parent
{
public:
  Child(): Parent(), num(0) {}

private:
  int num;
};

void Child::foo() 
{
  num = x; //Gets Parent's x, 
}

NB:
If you define an x in Child, that masks the x in Parent. So, if you want to get the x in Parent, you would need: Parent::x. To simply get x from a Child c, you use c.x if x is public or use a getter if x is protected or private:

int Child::getNum()
{
     return num;
}
Steve P.
  • 14,489
  • 8
  • 42
  • 72
  • True, I haven't programmed in C++ in quite some time. Thanks. – Steve P. Jul 20 '13 at 22:15
  • In my program, the parent is necessary only for declaring variables that every inherited class owns, so it's not a problem. However, your code is not exactly clear for me: `parent()` is obviously a function, but what is `x(0) {}`? A function with the name of a variable, and a zero input? Anyway, for my exact problem, see the comment below the main post. – Zoltán Schmidt Jul 20 '13 at 22:28
  • 1
    @ZoltánSchmidt it just initializes `x` to be 0. I made an edit. It should help. – Steve P. Jul 20 '13 at 22:29
  • 2
    @ZoltánSchmidt For the `: x(0)`, see [What is this weird colon-member syntax in the constructor?](http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor) – dyp Jul 20 '13 at 22:45
1

You can't use any function, data member of an inherited class if it's casted back to base class. However, you can alter those variables with virtual functions. Example:

#include <iostream>

class BaseClass {
public:
  BaseClass() {}
  virtual void do_smth() = 0;

private:
};

class InheritedClass: public BaseClass {
public:
  InheritedClass(): a(1) {}
  virtual void do_smth() { std::cout << ++a << std::endl; }
private:
  int a;
};

int main() {
  BaseClass* ptr = new InheritedClass();
  ptr->do_smth();
  return 0;
}

In this piece of code, virtual function did alteration of variable belongs to InheritedClass.

Cengiz Kandemir
  • 375
  • 1
  • 4
  • 16