Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change the status of individual members of a base class to private?

I am using wxWidgets where, if you have ever used it, you will know that there are a lot of public functions in the base class. I recently ran into a situation where I would not want a method SetText() be called directly from the derived class. That is, the derived class inherited the SetText() function but I do not want this function to be available to clients. Instead, I am providing two new functions which call SetText() but not before some extra operations are performed.

Currently, the client (me!) can forget to call the special functions and simply call SetText(). Consequently, some extra operations will not be performed. These operations are subtle enough that it may be overlooked easily.

So, can I mark individual functions as private so that the client cannot possibly call them OR simply make it impossible for clients to call it directly (they will have to use my functions to call it indirectly)?

Note that SetText() is not virtual.

EDIT: To future programmers who stumble on this question, check both the marked answer and Doug T.'s answer.

like image 932
Samaursa Avatar asked Aug 24 '11 21:08

Samaursa


People also ask

Can a base class be private?

With private inheritance, public and protected member of the base class become private members of the derived class. That means the methods of the base class do not become the public interface of the derived object. However, they can be used inside the member functions of the derived class.

Can private members of base class become members of derived class?

Private members of the base class cannot be used by the derived class unless friend declarations within the base class explicitly grant access to them.

Can we declare public/private protected in base class?

No, we cannot declare a top-level class as private or protected.

Can private members of a base class are inheritable?

Yes, Private members of base class are inherited in derived class..But objects of derived class have no access to use or modify it..


1 Answers

There are actually two ways of doing this. Doug T. gave a very good overview of the first one - using private/protected inheritance and composition - so I won't go into that one.

The problem with using private/protected inheritance is that it masks everything. Then, you have to selectively expose the members that you still want to be public. If you want most everything to remain public, and are only trying to mask out a single thing, then this can become a major headache. This gives rise to the need for the second way to do this - with the using keyword.

For example, in your case, you could simply declare your class as follows:

class Child : public Parent
{
    //...
    private:
        using Parent::SetText; // overrides the access!
};

This only masks the SetText method!

Just remember though, a pointer to a Child can always be cast to a pointer to a Parent, and access that method again becomes possible - but that's a problem with inheritance, too:

class Parent
{
public:
    void SomeMethod() { }
    void AnotherMethod() { }
};

class ChildUsing : public Parent
{
private:
    using Parent::SomeMethod;
};

class ChildPrivateInheritance : private Parent
{
};

void main()
{
    Parent *p = new Parent();
    ChildUsing *a = new ChildUsing();
    ChildPrivateInheritance *b = new ChildPrivateInheritance();
    p->SomeMethod();             // Works just fine
    a->SomeMethod();             //  !! Won't compile !!
    a->AnotherMethod();          // Works just fine
    ((Parent*)a)->SomeMethod();  // Compiles without a problem
    b->SomeMethod();             //  !! Won't compile !!
    b->AnotherMethod();          //  !! Won't compile !!
    ((Parent*)b)->SomeMethod();  // Again, compiles fine

    delete p; delete a; delete b;
}

Attempting to access SomeMethod on an instance of ChildUsing produces (in VS2005):

error C2248: 'ChildUsing::SomeMethod' : cannot access private member declared in class 'ChildUsing'

However, attempting to access either SomeMethod or AnotherMethod on an instance of ChildPrivateInheritance produces:

error C2247: 'Parent::SomeMethod' not accessible because 'ChildPrivateInheritance' uses 'private' to inherit from 'Parent'
like image 181
Nate Avatar answered Sep 30 '22 03:09

Nate