views:

167

answers:

6

suppose a class has private data members but the setters and getters are in public scope. If you inherit from this class, you can still call those setters and getters -- enabling access to the private data members in the base class. How is this possible since it is mentioned that a derived class cannot inherit private data members

+3  A: 

Because the getters and setters are public -- they're callable by anyone, not just derived classes.

Billy ONeal
my doubt is how come you can manipulate the values of the private data members since clearly you cannot inherit them
and those getters and setters are members of the base class, so they have access to the private data.
amertune
@user235230: "clearly you cannot inherit them" This is WRONG.
John Dibling
Adam wants to keep the keys to his car private, so only he can use them. Even his kids don't have access.However, if he provides a public get method for his car keys, he's giving the whole town permission to drop by and take his car out for a spin. _Of course_ that includes his children.
David
+10  A: 

A derived class doesn't inherit access to private data members. However, it does inherit a full parent object, which contains any private members which that class declares.

Avi
can you still manipulate it using pointer arithmetic
@user265260: Probably, but why? That strikes me as a problem just waiting to happen. Members are private for a reason, and if you can play with their values you can mess up an object. Moreover, if the layout changes (and it can change between different compiles; an object with a `size_t` member is likely to have a different layout with 32-bit and 64-bit compiles), you're stomping on entirely different data.
David Thornley
@user265260: That might work on *your* platform, but it is never **required** to work. Ever.
Billy ONeal
just to poke and hack around, i was just wondering how this is implemented
Yes, you could manipulate the private data in a hackish, non-portable way using `this` and offsetting accordingly.
Emile Cormier
@user265260: For learning what happens "under the hood", the best way would be to fire up your debugger and look at the addresses of the object and its data members. You could also pull up the debugger's "memory" view and see where the data goes in memory. Keep in mind that the behavior you'll see is platform/compiler specific.
Emile Cormier
A: 

The derived class does not inherit the private members but can access them via the getters and setters that in the base class. Control is still in the base class.

Jonathan Swinney
The derived class *does* inherit the private members. It's just that the derived class cannot access those members directly (they're private).
Emile Cormier
John Dibling
Ok, granted. I should have been more precise. I meant that derived classes don't inherit private members from base classes in the sense that they aren't defined in the derived class as well. Naturally they have to be in an instance of a derived class in order for the base class to use them.
Jonathan Swinney
A: 

Getters and setters do Not give you complete control over private data members. The control still lies with the base class.

zipcodeman
A: 

Using the pattern

class MyClass {
  private: int a;
  public: void setA(int x) { a = x; }
  public: int getA() const { return a; }
};

seems object-orientated and has the sent of encapsulation.

However as you noticed, you can still directly access the private field and there is nothing gained over just making a public and accessing it directly.

Using getters and setters like this does not really make sense in C++.

Danvil
Actually they do. In C# (for example), if you decided to cache access to `a` here (assuming it actually was a lot of work to calculate `a`), you could just make a a property and you'd be done. However, in C++ you'd have to go and replace all code that references `MyClass.a` with `MyClass.GetA();`
Billy ONeal
This question is related to C++ not C#. And a lot of people think it is smart to provide generic getters and setters for integral types - but it is not.
Danvil
@Danvil: "Using getters and setters like this does not really make sense in C++.". Allowing unfettered access to data members except through a well defined interface (e.g. getters and setters) makes tracking down errors a nightmare. You need to look at all places where that member is set to try to determine why it has an unexpected value. Restricting access to class members through that well defined interface makes debugging much easier, as you have the stack trace providing some useful insight.
andand
@andand: Or you could just learn to use your debugger better.
John Dibling
A: 

It depends on the inheritance type. If you inherit privately, then the derived class does NOT have access to the Base's private members.

Access                      public     protected    private
-----------------------------------------------------------
members of the same class      yes           yes        yes
members of derived classes     yes           yes         no
not members                    yes            no         no
JRL