473,395 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

override a public function to protected

If I have a class A that has a virtual public function DoSomething()
Class A
{
public:
virtual void DoSomething();
}

then I override this function to protected is class B
Class B: public Class A
{
protected:
void DoSomething();
}

Is this valid? Thanks!

WJ
May 4 '07 #1
5 1880

"WJ" <WJ@discussions.microsoft.comwrote in message
news:23**********************************@microsof t.com...
If I have a class A that has a virtual public function DoSomething()
Class A
{
public:
virtual void DoSomething();
}

then I override this function to protected is class B
Class B: public Class A
{
protected:
void DoSomething();
}

Is this valid? Thanks!

WJ
Ignoring the syntax errors in your code, if your question is whether or not
it's syntactically legal in C++ (VC 8) to make the override protected, the
answer is yes. I'm interested to know what you want to achieve by doing
this, as if there is no class derived from B, it simply makes the override
version unusable.
May 4 '07 #2

"WJ" <WJ@discussions.microsoft.comwrote in message
news:23**********************************@microsof t.com...
If I have a class A that has a virtual public function DoSomething()
Class A
{
public:
virtual void DoSomething();
}

then I override this function to protected is class B
Class B: public Class A
{
protected:
void DoSomething();
}

Is this valid? Thanks!

WJ
Should have said "override is unusable outside B if no class is derived from
B".
May 4 '07 #3
OK, I'll keep on using the example.
in class A, DoSomething() is declared as public, I thought the funciton
should be protected, since it is not used outside.

Class A is in a library, which I can't change, so I wanted to change
DoSomething() to protected in class B.

Thanks for answering the question!

WJ

"pvdg42" wrote:
>
"WJ" <WJ@discussions.microsoft.comwrote in message
news:23**********************************@microsof t.com...
If I have a class A that has a virtual public function DoSomething()
Class A
{
public:
virtual void DoSomething();
}

then I override this function to protected is class B
Class B: public Class A
{
protected:
void DoSomething();
}

Is this valid? Thanks!

WJ

Ignoring the syntax errors in your code, if your question is whether or not
it's syntactically legal in C++ (VC 8) to make the override protected, the
answer is yes. I'm interested to know what you want to achieve by doing
this, as if there is no class derived from B, it simply makes the override
version unusable.
May 4 '07 #4

"WJ" <WJ@discussions.microsoft.comwrote in message
news:B5**********************************@microsof t.com...
OK, I'll keep on using the example.
in class A, DoSomething() is declared as public, I thought the funciton
should be protected, since it is not used outside.

Class A is in a library, which I can't change, so I wanted to change
DoSomething() to protected in class B.

Thanks for answering the question!
Well, that's certainly a valid case I hadn't thought of! Thanks for telling
me!
May 4 '07 #5

"pvdg42" <pv****@newsgroups.nospamwrote in message
news:Oq**************@TK2MSFTNGP02.phx.gbl...
>
"WJ" <WJ@discussions.microsoft.comwrote in message
news:23**********************************@microsof t.com...
>If I have a class A that has a virtual public function DoSomething()
Class A
{
public:
virtual void DoSomething();
}

then I override this function to protected is class B
Class B: public Class A
{
protected:
void DoSomething();
}

Is this valid? Thanks!

WJ

Should have said "override is unusable outside B if no class is derived
from B".
In any case, it's pointless to try to reduce the visibility like that...

B* someB;
static_cast<A*>(someB)->DoSomething();

is perfectly legal.

If you want to hide access to A's members from users of B, then you have to
use non-public inheritance.
May 7 '07 #6

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

Similar topics

14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
8
by: JPRoot | last post by:
Hi M. Jeffrey Tan, Just hopping you didn't forget me? :) Thanks JPRoot ----- \"Jeffrey Tan\" wrote: -----
2
by: ~toki | last post by:
How can i take the control of the key events in Class2 ? This is the code snipped that i'd tried (after try some others): public class Main : System.Windows.Forms.Form { protected virtual...
8
by: Keith Smith | last post by:
Could some one help me a little? I am trying to understand when/where you would ever want to use "protected override..." code such as this. How is this any different from creating a...
3
by: Richard | last post by:
I have a requirement to put a GDI style circle or rectangle border around the selected row of a datagrid/ It will overlap into the row above and below the selected row. Doing this in a the OnPaint...
14
by: knocte | last post by:
Hello. I have a problem with C# language. I want to define an algorithm on a static function inside an abstract class. This function calls a static variable inside the same class. Then I want to...
2
by: Ben Voigt | last post by:
I have a function that is called for several distinct reasons. It is conceivable that a subclass would want to handle those cases differently. Therefore, what I want to do is have multiple v-table...
13
by: Ben Voigt | last post by:
Is there any way to have an overridden method which is not callable from the assembly overridding it? A contrived example (though I want this feature, my program has nothing to do with food...
1
by: Joshua | last post by:
I'm creating a web control that has an image and a text box. I would like to then override the Visible and Text property of the usercontrol, so when you reference the visible property of the user...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.