简体   繁体   中英

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.

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'
  1. You can set every public to private by doing private inheritance and expose the interface you want.

     class YourWidget : private BaseClass { }; YourWidget widget; widget.SetText(); // this is private 

    This prevents you from working through your base class though and destroys the "is-a" relationship. The following won't compile:

     BaseClass* ptr = new YourWidget(); //error, no conversion available 

    It also impacts every public member of BaseClass and I'm not sure its agreeable to how wxWidgets works.

  2. You can hide SetText by creating a private version in the derived class. This only works when accessing through a derived class pointer and doesn't provide any safety when working through a base class pointer. The base class SetText would still be called if the client has a base class ptr.

  3. You can wrap the BaseClass instead of deriving from it and expose the interface you'd like. This may or may not be possible depending on how your code works. If you want the normal interface but altered slightly, then you'll have to reimplement every function you want to forward onto BaseClass .

  4. You can have YourWidget inherit from a second interface that provides the interface you would like to expose to your code, and pass out pointers to this interface instead of a pointer to your BaseClass . ie

      class IMyWidget { public: virtual void SpecialSetText() = 0; } class YourWidget : public BaseClass, public IMyWidget { public: void SpecialSetText() {/*use SetText in a special way*/} }; 

    Then in your code, you can pass around IMyWidget pointers instead of BaseClass / YourWidget pointers. This gives specific parts of your code a specific interface into YourWidget and prevents (barring downcasting) other parts of the interface from being used.

根据您希望通过派生类访问的内容,您可以使用私有(或受保护)继承 ,这将不允许非友元类访问继承的成员/方法或通过在其中声明新方法来隐藏基本方法派生类,重定向或(如果您需要额外的参数或其他)错误。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM