-1

I'm wondering if there is some way to declare a variable that is member data of a base class so that it is not inherited by a class derived from it. I have some member data in my base class that should not be part of objects of the derived class, so I'd like to separate what should be inherited from what should not. Is there some way to do this?

Victor Brunell
  • 5,668
  • 10
  • 30
  • 46
  • 2
    private fields/methods/properties are not readily available to derived classes. would that help? also see here http://stackoverflow.com/questions/2950820/are-private-members-inherited-in-c – Ric Nov 21 '13 at 19:01
  • @Ric But they still live within the object. you can access it through encapsulation if provided or through reflection – Sriram Sakthivel Nov 21 '13 at 19:03
  • Refer to the comment left on the highest answer: If you try to pull the private field from the derived class using derivedType.GetField("f", BindingFlags.Instance | BindingFlags.NonPublic), you will get nothing back, even though this is technically supposed to return all inherited members. – Ric Nov 21 '13 at 19:05

2 Answers2

0

If you're trying to 'hide' data from derived/inherited classes, use private access modifier. Yes, even though they are inherited, you cannot access them unless they are marked protected or public.

Ric
  • 12,855
  • 3
  • 30
  • 36
  • I was looking more for a way to prevent derived classes from inheriting the data at all. I want to do this to reduce their size, if possible. – Victor Brunell Nov 21 '13 at 20:16
  • Not sure that's possible. Better off trying something else if that's the case. – Ric Nov 21 '13 at 20:32
0

Ric, yes. I think it's just not a feature available in C++. What I'd really like to do is create a base class with member data with some kind of prefix that prevents the data from being inherited by child classes. Something like: noinherit void func1(); or noinherit double x; Where noinherit is just some keyword I made up to define data that should not be inherited by child classes. In a way, I want to be able to determine the genes inherited by the children from the parent, instead of the children just getting the full set of the parent's genes, and simply having a certain phenotype based on which genes are private, and which are public or protected, to use a genetics analogy.

Victor Brunell
  • 5,668
  • 10
  • 30
  • 46