开发者

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

开发者 https://www.devze.com 2023-03-30 18:47 出处:网络
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(

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 cla开发者_运维技巧ss. 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. i.e.

     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.


Depending on what access you want to have through the derived class, you could use private (or protected) inheritance, which won't allow non-friend classes to access inherited members/methods or hide the base method by declaring a new method in the derived class which either redirects or (if you needed extra arguments or something) errors.

0

精彩评论

暂无评论...
验证码 换一张
取 消