473,545 Members | 2,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
}
Jul 23 '05 #1
20 2857

"modemer" <me@privacy.net .invalid> wrote in message
news:d1******** **@domitilla.ai oe.org...
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
compiling
}

Jul 23 '05 #2
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::fu nc' does not overload 'BaseClass::fun c'. 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 "overloadin g"?

V
Jul 23 '05 #3
Victor Bazarov wrote:

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 "overloadin g"?


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.dinkumware.com)
Jul 23 '05 #4
What you're asking is, "How do I make this function 'final,'" I think. You
don't.
http://www.parashift.com/c++-faq-lit....html#faq-23.9

- JFA1
Jul 23 '05 #5
Pete Becker wrote:
Victor Bazarov wrote:

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 "overloadin g"?


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/

Jul 23 '05 #6
Pete Becker wrote:
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.

--
Salu2
Jul 23 '05 #7

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:%a******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
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::fu nc' does not overload 'BaseClass::fun c'. 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 "overloadin g"?

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.
Jul 23 '05 #8
Julián Albo wrote:
Pete Becker wrote:

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.


<g>

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #9

"James Aguilar" <jf**@cec.wustl .edu> wrote in message
news:d1******** **@newsreader.w ustl.edu...
What you're asking is, "How do I make this function 'final,'" I think. You don't.
http://www.parashift.com/c++-faq-lit....html#faq-23.9

- JFA1

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.
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
1806
by: Joe | last post by:
Hi, Can I pass a "generic" class pointer as an argument to a function? For instance say classA and B are both derived from Z. { int iType =1;
6
7950
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a Child object goes out of scope? Specifically, I am dealing with a C library that provides a function that must be called to "destruct" a...
7
3666
by: juli jul | last post by:
Hello, I have a function in base class and a lot of child classes of it. Couple of them need an input argument in this function and all the other don't. How can I do it using ovveride and virtual? Thank you! *** Sent via Developersdex http://www.developersdex.com ***
9
1788
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't figure out what attribute to put on the base class method to prevent this. TIA, Larry Woods
6
1297
by: Joe HM | last post by:
Hello - The following code will crash if the TestA() instances are declared as Private and work just fine if they are Public. Is there a way to hide these instances to callers from outside the Module? It should only be possible to call the one instance of TestA() without the second argument. ModuleX
5
3386
by: EqDev | last post by:
I have a class that is a control derived from UserControl. I want to use serialization and deserialization with this calss but I get an exception "Cannot serialize member System.ComponentModel.Component.Site of type System.ComponentModel.ISite because it is an interface.". I am not interested in serializing any member from the base class...
20
4925
by: alexandre.braganti | last post by:
Hello, First sorry for my poor English, I am French ;-) I've got a comprehension problem of what happend in one of the project i'm working on. Basically I've got a class gs_object than has got a VIRTUAL function createList(). This createList() function is overloaded in another class named ct_server that inherits gs_object. in my code,...
3
1569
by: a | last post by:
Hi, I need clarification for virtual method and pure virtual method. e.g Class Base{ virtual void func(){ ---- } } Class Child : public Base{ void func()
7
2697
by: Mark | last post by:
Hi, I have an abstract class which I want my other classes to inherit from. In the constructor of the abstract class I want to check if certain virtual functions have been overloaded or not. Is this possible? For example, let's say I have class Base {
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7808
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7423
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7757
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4945
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.