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

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

Post by modeme » Wed, 23 Mar 2005 05:24:54


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
}
 
 
 

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

Post by modeme » Wed, 23 Mar 2005 05:29:51


sorry, typo, should be: prohibited

 
 
 

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

Post by Victor Baz » Wed, 23 Mar 2005 05:31:55


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
 
 
 

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

Post by Pete Becke » Wed, 23 Mar 2005 05:48:35


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

--

Pete Becker
Dinkumware, Ltd. ( http://www.yqcomputer.com/ )
 
 
 


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

Post by Juli Alb » Wed, 23 Mar 2005 06:14:40


Don't write the class.

--
Salu2
 
 
 

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

Post by modeme » Wed, 23 Mar 2005 06:19:29


being

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.
 
 
 

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

Post by Pete Becke » Wed, 23 Mar 2005 06:21:55


<g>

--

Pete Becker
Dinkumware, Ltd. ( http://www.yqcomputer.com/ )
 
 
 

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

Post by modeme » Wed, 23 Mar 2005 06:32:36


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

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

Post by Howar » Wed, 23 Mar 2005 06:38:51


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
 
 
 

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

Post by Phli » Wed, 23 Mar 2005 06:38:57


designer,

I hope nobody's business card ever says that...

I
never
compiler

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.

--
Phlip
http://www.yqcomputer.com/
 
 
 

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

Post by Jeff Schwa » Wed, 23 Mar 2005 07:06:37


You are entitled to your opinion.


Can you make func() private?


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 );
}
 
 
 

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

Post by modeme » Wed, 23 Mar 2005 07:41:54


designer,
I
never
compiler

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.