Re: How to prevent a function in base class being overloaded from child class

From: modemer (me_at_privacy.net.invalid)
Date: 03/22/05


Date: Tue, 22 Mar 2005 11:09:01 -0500


"Andrey Tarasevich" <andreytarasevich@hotmail.com> wrote in message
news:ovCdnY5oI8YJ-KLfRVn-hg@comcast.com...
> modemer wrote:
> >> > Question is as in subject.
> >> >
> >> > For example:
> >> >
> >> > class BaseClass {
> >> > public:
> >> > void func() { do something; } // I don't want this function
> > being
> >> > overloaded in its inherited class
> >> > };
> >> >
> >> > class ChildClass : BaseClass {
> >> > public:
> >> > void func() { do otherthing; } // this should be inhibited when
> >> > compiling
> >> > }
> >>
> >> The 'ChildClass::func' does not overload 'BaseClass::func'. The 'func'
> >> name from the base class is _hidden_ by the derived class' member.
> >>
> >> Perhaps when you learn how things work, you won't need the "prevention"
> >> you ask about. What particular problem do you think you're going to
> >> solve by preventing what you call "overloading"?
> >> ...
> > Let me say this question in another way. Suppose I am a base class
designer,
> > I believe my func() in base class is the best solution for certain goal.
> > This base class is going to be used by other programmers, they are just
> > allowed to create their own child class inherated from my base class,
but I
> > don't want to write any email notice to say "func() is reserved for base
> > class, don't use it in your derived class, otherwise base::func() will
never
> > be called when child::func() is called", I want to know how to let
compiler
> > raise an error when the programmer happened defines func() in his child
> > class.
>
> There's one thing that is not clear in your question. There are several
> details in your posts that seem to suggest that in the real code the
> base class' function is virtual. In the code sample above the function
> is not virtual. Several people here missed this important fact and gave
> you answers that apply to virtual functions only, and, surprisingly, you
> didn't object. Can you, please, clarify whether the function you are
> talking about is virtual or not?
>
> --
> Best regards,
> Andrey Tarasevich

Thanks for pointing out the key word "virtual". Actually in this question,
virtual or not is not important because my object is to prevent *specific*
function(not all functions) in base class being defined in derived class no
matter it's virtual or not as long as there is a way to do, if derived class
happened defined(we have to believe that programmers have different
understandings about a system), C++ compiler should raise an error, but
unfortunately, nobody could answer this question directly, that means there
is not this kind of feature in C++.

When I studied C++, I noticed that C++ is trying the best to offer all
possibilities to control how member function of class is handled, like
private/protect/public/virtual/static/overload/override/namingConvention
etc. This is why I am looking for if there is a feature like the one
mentioned in my question. If this feature exists, I think the code or logic
would be more clear, simple or even easier on bug control, source code
maintain.



Relevant Pages

  • Re: User-defined exceptions
    ... > have exception specification that is wider than corresponding base class ... > Andrey Tarasevich ... Kind regards ...
    (comp.lang.cpp)
  • Re: How to prevent a function in base class being overloaded from child class
    ... Placing the virtual keyword in front of an individual function allows it to ... Correct...there is no such feature. ... You state that "programmers have different understandings about a system". ... It's not neccesarily an error to "hide" a base class function. ...
    (comp.lang.cpp)
  • Re: How to prevent a function in base class being overloaded from child class
    ... > I believe my func() in base class is the best solution for certain goal. ... Simply don't make the function virtual, and writers of the derived classes ... It's just not possible to prevent programmers from screwing up their derived ...
    (comp.lang.cpp)
  • Re: oo mess: how to make life complicated for yourself with inheritence and collections.please help
    ... I've got a base class called "Feature" which some classes inherit. ... the poll feature has seperate methods that the base feature ... their common properties in the switch as i would be repeating myself ... You can't create an object of the base class and cast it to a subclass, but you can do it the other way around. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Microsoft & GNU compiler 2 :)
    ... > argument conclusive, but the decision has proven useful in ... > that it allows programmers to add and remove public and private ... If you add a private variable to the base class then the ... derived class suddenly can't see the global variable. ...
    (microsoft.public.vc.stl)