By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,963 Members | 1,749 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,963 IT Pros & Developers. It's quick & easy.

override a public function to protected

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

"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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.