public/private inheritance

  • Thread starter Baruch Burstein
  • Start date
B

Baruch Burstein

Is there any way I can inherit from a base class as public, but make
only one member of the base class private in my derived class? Or do I
have to inherit the whole thing as private and write wrapper functions
for the whole interface?
 
V

Victor Bazarov

Is there any way I can inherit from a base class as public, but make
only one member of the base class private in my derived class? Or do I
have to inherit the whole thing as private and write wrapper functions
for the whole interface?

What problem are you trying to solve by "privatizing" that function?
You can define your own function with the same name, make it private.
Not sure it's going to help, though. Depends on what your use case is.

V
 
B

Baruch Burstein

What problem are you trying to solve by "privatizing" that function?
You can define your own function with the same name, make it private.
Not sure it's going to help, though.  Depends on what your use case is.

V

Actually, I am trying to privatize a public variable. The base class
has no need to know when the variable changes, and therefore I made it
public directly; why use get/set methods that do nothing (right?). The
derived class needs to adjust some of it's own settings according to
that one, so it needs to know when it is changed (get/set methods). Am
I totally misusing inheritance?
 
P

Paul Bibbings

Baruch Burstein said:
Actually, I am trying to privatize a public variable. The base class
has no need to know when the variable changes, and therefore I made it
public directly; why use get/set methods that do nothing (right?). The
derived class needs to adjust some of it's own settings according to
that one, so it needs to know when it is changed (get/set methods). Am
I totally misusing inheritance?

I have to say I am not quite understanding your motivations for doing it
this way. Having a base class data member be public just for the sake
of not having to provide get/set methods seems odd, particularly where
there are other ways of achieving what (I believe) your are trying to
do.

In order to take up on Leigh's solution, let's suppose you are wanting
to do something like the following (and you'll correct me if I am wrong
in my assumption):

class Base {
public:
Base(int i) : i_(i) { }
int i_;
void foo() { }
void bar() { }
};

class Derived : private Base {
public:
Derived(int i = 0) : Base(i) { }
using Base::foo;
using Base::bar;
void baz() { }
};

int main()
{
Derived d;
d.foo();
d.bar();
d.baz();
// int j = d.i_; // error: inaccessible
}

But then what would be wrong, given that you are wanting to avoid
accessors in your base class, with something like:

class Base {
protected:
int i_;
public:
Base(int i) : i_(i) { }
void foo() { }
void bar() { }
};

class Derived : public Base {
public:
Derived(int i = 0) : Base(i) { }
void baz() { }
};

int main()
{
// same as before
}

As to your ultimate question, "Am I totally misusing inheritance," I
would suggest probably yes. Your argument against avoiding what I might
call good design is trivial and merely introduces complications for no
real advantage as I can see it. Personally, I would go for something
like:

class Base {
private:
int i_;
protected:
void seti(int i) { i_ = i; }
int geti() const { return i_; }
public:
Base(int i) : i_(i) { }
void foo() { }
void bar() { }
};

class Derived : public Base {
Derived(int i = 0) : Base(i) { }
void baz() { seti(geti() * 2); }
};

int main()
{
Derived d;
d.foo();
d.bar();
d.baz();
// int j = d.i_; // error: inaccessible
// j = d.geti(); // error: inaccessible
}

All this is still dependent upon your actual use case and follows your
requirement that Base does not require the getter/setters for its
own purposes (adding that they should not be public either). They are
trivial enough to benefit from inlining in any case.

Regards

Paul Bibbings
 
N

Naomi Sidor

I like the protected inheritance from base, leaving private variables to base class only.
 
J

Jorge

You can also inherit as private/protected and choose some methods/
members to be public in the derived class by typing "using
baseClass::methodName" in the derived class.
 
P

Paul N

Actually, I am trying to privatize a public variable. The base class
has no need to know when the variable changes, and therefore I made it
public directly; why use get/set methods that do nothing (right?). The
derived class needs to adjust some of it's own settings according to
that one, so it needs to know when it is changed (get/set methods). Am
I totally misusing inheritance?

I think you may have answered your own question here. *Some* objects
of the base class *do* need to know when the variable changes - namely
those which are objects of the derived class. So it would seem
worthwhile implementing get and set methods, even if, in the base
class itself, they're not doing much.
 
P

Paul Bibbings

Paul N said:
I think you may have answered your own question here. *Some* objects
of the base class *do* need to know when the variable changes - namely
those which are objects of the derived class. So it would seem
worthwhile implementing get and set methods, even if, in the base
class itself, they're not doing much.

I had a little trouble with understanding your idea here initially, but
I think that I have a sense of it now. Still, I think that, for me, it
is this idea that "the base class has no need to know when the variable
changes, and therefore I [avoided get/setters]" which struck me as an odd
justification. After all, getters and setters are part of the interface
of an object and an interface, of course, is the means by which we
interact with an object *from the outside*. So, rather than the OP's
conclusion, I would think that a situation in which a base class merely
*stores* a data member that other parts of the system alone need to know
about (in terms of when that data changes) is reason *for* providing an
interface through which that information becomes available. If those
"other parts of the system" are derived classes then it seems to follow
that a viable structuring would aim to keep the data private, have
protected getters and setters in the base class, and inherit publicly.

Regards

Paul Bibbings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top