0

If I am declaring several variables in the base class, they will be defined in every derived class, right? I just want to have the variables in the base class once for all derived class. Example:

class Base{
public:
 virtual ~Base();

protected:
 Base();
 int fileIndex;
};

class DerivedA : public Base{
public:
 DerivedA();
 virtual ~DerivedA();
 void DoSth(); //using fileIndex
};

class DerivedB : public Base{
public:
 DerivedB();
 virtual ~DerivedB();
 void DoSthDifferent(); //using the same fileIndex
};

How can I do that?

D. Mogwitz
  • 35
  • 4
  • 1
    You have already done that! Possible duplicate: [Private and Protected Members](http://stackoverflow.com/q/224966/514235) – iammilind Aug 08 '16 at 09:56
  • 2
    When you say you want all derived classes to use the same fileIndex what do you actually mean. All the answers so far understand it to mean that all Bases, whether they use inheritance or not should use the same underlying fileIndex value. But it seems like your example is overcomplicated for that interpretation. – 1stCLord Aug 08 '16 at 10:03
  • Yes, do you mean you only want to _define_ these members once in the base but store separate copies in each instance (including deriveds - bog-standard inheritance), or do you instead want all instances to share the same members (`static`)? I recommend following this flag description: _"Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question."_ – underscore_d Aug 08 '16 at 10:21
  • 2
    I want that all instances use the same member (static). There is only one fileIndex. I want to ensure that all derived objects use the value from the base class, once defined. – D. Mogwitz Aug 08 '16 at 10:37

2 Answers2

2

Just make fileIndex a static variable:

static int fileIndex;

Then all instances from all derived classes will share the same value

Ohad Eytan
  • 8,114
  • 1
  • 22
  • 31
1

I'm assuming this isn't a file index in some global log that you need to keep tidy. A static will do for that.

Use composition instead of (misusing) inheritance.

class FileAccessor
{
  int fileIndex;
  // Your stuff
};

Class A
{
  std::shared_ptr<FileAccessor> mFileAccessor;
public:
  A(std::shared_ptr<FileAccessor> fAccesor ) : mFileAccessor(fAccesor ) {}

  void DoSth () { /* use mFileAccessor*/ }
};

Class B
{
  std::shared_ptr<FileAccessor> mFileAccessor;
public:
  B(std::shared_ptr<FileAccessor> fAccesor ) : mFileAccessor(fAccesor ) {}

  void DoSth () { /* use mFileAccessor*/ }
};

int main()
{
  auto fAccesor = std::make_shared<FileAccessor>();
  A a{fAccesor};
  B b{fAccesor};
}

That way, if you need to synchronize access, the logic stays in FileReader.

If you need to have objects share state, than make it explicit by factoring the state into another object.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458