-3

I have a peculiar requirement and not been able to find a solution.

class Base
{
public:
    void func()
    {
       //access the member say 'var' of derived class
    }
}
  1. It is mandatory in our case that all derived class from base will have member 'var'.
  2. Name of the derived class can be anything.
Coding Mash
  • 3,338
  • 5
  • 24
  • 45
Dexter
  • 1,299
  • 3
  • 20
  • 38
  • 7
    Why not have the `var` in the `Base` as it is common to all derived? – hmjd Sep 25 '12 at 09:28
  • There is a always a better solution to your source of "peculiar" requirement. Instead of breaking the fundamental tenets of object oriented programming look deeper at why you have such a requirement and ways of solving it with a more elegant solution. – Ram Sep 25 '12 at 10:29

2 Answers2

9

Since all vars in the derived classes will need to be of the same type to even make any sense, why not simply but it in the base class?

class Base
{
public:
    void func()
    {
      // use var
    }
protected: // allow derived classes to access 'var'
           // and give it a value on construction
    // where T == whatever type you want
    Base(T v) : var(v){}
    T var; 
};
Xeo
  • 129,499
  • 52
  • 291
  • 397
  • I considered this, but we don't want to implement it this way – Dexter Sep 25 '12 at 09:33
  • This is exactly how this should be implemented, at least based on what the OP has stated. – thecoshman Sep 25 '12 at 09:42
  • @HellBoy: Please continue this phrase: We don't want to implement it this way, because ... – Sebastian Mach Sep 25 '12 at 09:42
  • Then maybe he should write the implementation? Can you ask him for reasons why he doesn't want that? I can't believe that someone would just say "we won't do it that way, and I'm not telling why!" – Zdeslav Vojkovic Sep 25 '12 at 09:45
  • 5
    @HellBoy: Please slap your manager. I can't see any reason to *not* do this. – Xeo Sep 25 '12 at 09:46
  • @ZdeslavVojkovic clearly you have not dealt with managers, they are 'experts' don't you know – thecoshman Sep 25 '12 at 09:47
  • @thecoshman :) true, but this is extremely strange. Usually they at least pretend to have reasons – Zdeslav Vojkovic Sep 25 '12 at 09:48
  • I think it is about "dependency inversion". – Sebastian Mach Sep 25 '12 at 09:49
  • 2
    @phresnel: So instead of the derived class depending on the base class, the base class should depend on the derived class? [What.](http://tvtropes.org/pmwiki/pmwiki.php/Main/FlatWhat) Doesn't this, like, subvert every teaching of OO *ever*? – Xeo Sep 25 '12 at 09:51
6

You'll need a virtual function that returns that member:

class Base
{
public:
    void func()
    {
       getVar();
    }
    virtual int getVar() = 0;
};
class Derived : public Base
{
    int var;
    virtual int getVar() { return var; }
};

A better design would be having var in the base class, since it's a common member. Otherwise, C++ doesn't have any reflection mechanism, so dynamically inspecting classes the way you'd want is out of the question.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625