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

M

modemer

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
}
 
M

modemer

modemer said:
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

sorry, typo, should be: prohibited
 
V

Victor Bazarov

modemer said:
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"?

V
 
P

Pete Becker

Victor said:
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"?

The problem of a derived class designer trying to solve problems in a
problem domain that the base class designer knows little or nothing
about, using techniques that the base class designer didn't think of.
See also: "How can I force all derived classes, however remote, to
override this function," "How can I prevent anyone from deriving from
this class," and "How can I make everyone do what I know is best for them."
 
H

Hang Dog

Pete said:
The problem of a derived class designer trying to solve problems in a
problem domain that the base class designer knows little or nothing
about, using techniques that the base class designer didn't think of.
See also: "How can I force all derived classes, however remote, to
override this function," "How can I prevent anyone from deriving from
this class," and "How can I make everyone do what I know is best for them."

http://www.siscom.net/~morgan/
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Pete said:
See also: "How can I force all derived classes, however remote, to
override this function," "How can I prevent anyone from deriving from
this class," and "How can I make everyone do what I know is best for
them."

Don't write the class.
 
M

modemer

Victor Bazarov said:
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"?

V

I feel so sleepy now, sorry for making 2 mistakes in this question. Right,
it shouldn't be overload.

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.
 
H

Howard

modemer said:
I feel so sleepy now, sorry for making 2 mistakes in this question. Right,
it shouldn't be overload.

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.

Simply don't make the function virtual, and writers of the derived classes
will know (if they know C++) that the function cannot be overridden.

But, it's not your job to try to prevent them from "hiding" your function by
writing their own. They should know better. You could add a comment in the
class definition, where the function is declared, that this function is not
intended to be overridden and should also not be hidden by writers of
derived classes, but, honestly, simply making it not virtual should be
sufficient.

It's just not possible to prevent programmers from screwing up their derived
classes, if they really want to. But if you do your job well, and document
anything you think is important, then the rest should really be up to them
as professional programmers.

Just my $.02, of course.

-Howard
 
P

Phlip

modemer said:
Let me say this question in another way. Suppose I am a base class
designer,

I hope nobody's business card ever says that...
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.

The ultimate way to do this in C++ is a sternly worded comment.

To attempt to back that up with muscle, read the book /Unit Test Frameworks/
by Paul Hamill, and focus on the Abstract Test pattern.

Write test cases for each of your base class's behaviors. Make the setUp()
for this test suite virtual. Write another Sternly Worded Comment advising
your Derived Class Designers to inherit the test suite and override the
setUp(). Create the derived class object in this method, and expect it to
pass all the tests the base class object passes.

This is an automated way to approach enforcing the Liskov Substitution
Principle, which a more formal title for the principle you seek - the Don't
Screw My Class Up Principle.
 
J

Jeff Schwab

modemer said:
Suppose I am a base class designer,
I believe my func() in base class is the best solution for certain goal.

You are entitled to your opinion.
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,

Can you make func() private?
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.

Why do you care about an unrelated function in a derived class?

If you want to limit access to the internals of your Base objects, make
those internals private, and provide access only through methods in the
base class. E.g:

#include <iostream>

struct Base {
public:
Base( ): m_i( 0 ) { }
virtual ~Base( ) { }

virtual void set( int i ) {
// Override me all you want. You can't set m_i
// without going through proper channels.

std::cout << "I am the One True Way to set m_i.\n";
m_i = i;
}

virtual int get( ) {
std::cout << "I am the One True Way to get m_i.\n";
return m_i;
}


private:

Base( Base const& ) {
throw "Don't copy Base.\n";
}

Base operator = ( Base const& ) {
throw "Don't assign to Base.\n";
}

int m_i;
};

struct Derived: Base {
void set( int i ) {
// For a compile time error, try this: m_i = i;

Base::set( i ); // Pretty much your only option.
}
};

int main( ) {
Derived d;
d.set( 3 );
}
 
M

modemer

modemer said:
I feel so sleepy now, sorry for making 2 mistakes in this question. Right,
it shouldn't be overload.

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.

Thank you guys gave so many suggestions. But it looks like all of these
suggestions are just like "tricky/rule" or even "algorithm", not a C++
standard feature, now I know C++ doesn't support this feature and I have to
find any way to workaround with my this quesion.
 
A

Andrey Tarasevich

modemer said:
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?
 
D

David White

modemer said:
Thank you guys gave so many suggestions. But it looks like all of these
suggestions are just like "tricky/rule" or even "algorithm", not a C++
standard feature, now I know C++ doesn't support this feature and I have to
find any way to workaround with my this quesion.

I'd be interested in a real example of a member function that absolutely
should never be overridden by a derived class. It seems to me that to come
to this conclusion you would have to think of every imaginable derived class
and know that overriding your function can never work (but if that's true,
would anyone ever want to override it?). Also, as someone else mentioned,
the fact that your function is not virtual and cannot actually be overridden
anyway - only hidden - is a strong sign that it shouldn't be messed with. I
would expect most programmers to instinctively avoid hiding base class
functions because an object's behaviour would vary according to whether the
static type of a pointer or reference is the base or derived class. I think
you're in trouble if you have to design your classes to thwart incompetence.

DW
 
R

Ron House

modemer said:
I am a little bit disappointed with the FAQ's answer. It just likes choose
yes or no for base class inheritability. But I just want part of functions
in base class are final, others could be overrided. Maybe like you said, I
am not allowed to do what I want.

I must admit I find myself wondering if there is _any_ legitimate reason
for what you are trying to do. Suppose I inherit from your class and
want to do something as simple and proper as to count the number of
times your function is called. So I override it, in my version add to a
counter and then call your version. What, specifically, could you be
doing in that function that is messed up by my doing this? Unless you
are avoiding virtuals altogether, and so inheritance is static - but
that is my problem, or the problem of the class users, who must avoid
situations requiring dynamic resolution.
 
J

James Aguilar

modemer said:
I am a little bit disappointed with the FAQ's answer. It just likes choose
yes or no for base class inheritability. But I just want part of functions
in base class are final, others could be overrided. Maybe like you said, I
am not allowed to do what I want.

That's exactly what I'm saying. There is no facility in the C++ language
that supports that behavior. Java lacks const-ness, which, IMO, is far more
important.

- JFA1
 
M

modemer

Andrey Tarasevich said:
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?

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.
 
D

David Harmon

On Tue, 22 Mar 2005 11:09:01 -0500 in comp.lang.c++, "modemer"
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

I would be a lot more interested if you were asking how to accomplish
some goal, rather than asking how to *prevent* accomplishing
something.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top