4

I want each class to have its own static code, which can be requested from each object. I am thinking of this, but it doesn't seem to work:

#include <iostream>

class Parent {
protected:
    static int code;
public:
    int getCode();
};

int Parent::code = 10;
int Parent::getCode() {
  return code;
}

class Child : public Parent {
protected:
    static int code;
};

int Child::code = 20;

int main() {
  Child c;
  Parent p;

  std::cout << c.getCode() << "\n";
  std::cout << p.getCode() << "\n";

  return 0;
}

It outputs:

10

10

yet I expect

20

10

Community
  • 1
  • 1
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195

4 Answers4

1
class Parent {
public:
    virtual int getCode();

    // Looks like a variable, but actually calls the virtual getCode method.
    // declspec(property) is available on several, but not all, compilers.
    __declspec(property(get = getCode)) int code;
};


class Child : public Parent {
public:
    virtual int getCode();
};

int Parent::getCode() { return 10; }
int Child::getCode()  { return 20; }

int main() {
  Child c;
  Parent p;

  std::cout << c.code << "\n"; // Result is 20
  std::cout << p.code << "\n"; // Result is 10

  return 0;
}
abelenky
  • 63,815
  • 23
  • 109
  • 159
1

You have to make the 'getCode()' function as virtual and have to implement every time as following codes:

class Parent {
protected:
    static int code;
public:
    virtual int getCode() { return code; }
};

int Parent::code = 10;

class Child : public Parent {
protected:
    static int code;
public:
    virtual int getCode() { return code; }
};

int Child::code = 20;

int main() 
{
    Child c;
    Parent p;

    std::cout << c.getCode() << "\n";
    std::cout << p.getCode() << "\n";
    return 0;
}
Farhad Reza
  • 424
  • 4
  • 16
  • The static variables are not needed when the values are constant (as described in OPs comments). – abelenky Jan 20 '15 at 16:04
  • @abelenky, you are correct but I've tried to give a solution with less change of the original source code. – Farhad Reza Jan 20 '15 at 16:09
  • You don't need to have virtual methods if you don't access the child object with a pointer to a base class like in his example, overriding the method is sufficient. – Johnmph Jan 20 '15 at 16:26
0

Why use the member variable at all?

class Parent {
public:
    static int getCode();
};

int Parent::getCode() {
  return 10;
}

class Child : public Parent {
public:
    static int getCode();
};

int Child::getCode() {
  return 20;
}

Instead of one static member per class, you have one member function per class. Plain and simple.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
0

Your problem:

In the parent class, you don't declare your getCode function as virtual. So, whenever you call it with a class that inherits from your parent class, it will just return the int code from the parent class.

To fix this:

  • First, declare the getCode function as virtual in your parent class.
  • Second, write another getCode function in your inherited class and return the int code from the inherited class
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
busebd12
  • 997
  • 2
  • 12
  • 24