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

Base class access specifier

P: n/a
Hello,

I've several C++ classes that must be ported to C#. Is there any
construction in C# similar to C++:

class A : protected B
{
...
}
Nov 16 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Stanislav,

Unfortunately, there is not. Anything that is public on the base class
is exposed in the derived class, and only the protected, protected internal
and public (and possibly internal members) are accessibile to the derived
class.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Stanislav Simicek" <St**************@discussions.microsoft.com> wrote in
message news:EF**********************************@microsof t.com...
Hello,

I've several C++ classes that must be ported to C#. Is there any
construction in C# similar to C++:

class A : protected B
{
...
}

Nov 16 '05 #2

P: n/a
> Unfortunately, there is not.

That's bad news (and perhaps C# language flaw). Thank you anyway.
Nov 16 '05 #3

P: n/a
You wouldn't want to hide the public methods of the Object class would
you? It is comforting knowing every object has a ToString() method.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Wed, 13 Oct 2004 06:57:04 -0700, "Stanislav Simicek"
<St**************@discussions.microsoft.com> wrote:
Unfortunately, there is not.


That's bad news (and perhaps C# language flaw). Thank you anyway.


Nov 16 '05 #4

P: n/a
Personally I think its a good thing. Inheritance is best used for expressing an is-a relationship not code re-use. Code re-use is all non public inheritance is for and I think encapsulation / delegation is a much more preferable way of handling that. One of the deisgn goals of C# was to remove some of the unnecessary complexity in C++ - I think non-public inheritance was a valid case for removal.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
Unfortunately, there is not.


That's bad news (and perhaps C# language flaw). Thank you anyway.

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.775 / Virus Database: 522 - Release Date: 08/10/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #5

P: n/a
> You wouldn't want to hide the public methods of the Object class would you?

No, in general I would not. But sometimes, I need to hide all methods of the
base class, and this is not possible in C#. I am using C++ for too long time,
perhaps. Consider the following scenario:

I have a class A:

class A
{
int Get(...);
int Set(...);
};

which does whatever it does and servers as a base class for both class B,
which is used internally and class C, which is exposed to be used by 3rd
party. Let's assume that A.Get and A.Set methods are not "safe", and can be
use only by us (in derived class B), but we can provide "safe" limited
functionality to 3rd party (class C). This can be easily done in C++ as:

class C : protected A
{
int GetOnlySomething() { return Get("Something"); }
int SetOnlySomething() { return Set("Something"); }
};

while having

class B : public A
{
....
};

A.Get and A.Set are not available for public use. (A methods could be
protected, but let's say we need them public due to way they are used by us
internally). The point is, that if anyone adds any method to class A, it is
not automatically available in class C, so C "safety" remains.

In C#, you have to implement a new (or override existing virtual) method
with "protected" access modifier, and it will not be automatically done if a
public method is added to class A, which makes class C not to be "safe"
anymore.

You may find this example complicated, but it represents real situation
(designed before I've joined the company :-). I also believe, that the whole
thing could be solved by changing/improving C++ OO design - but using
"protected" base class access modifier in C# would allow me to port the whole
bunch of C++ classes very easily...

Regards,
---
Stanislav
Nov 16 '05 #6

P: n/a
Stanislav Simicek <St**************@discussions.microsoft.com> wrote:
You wouldn't want to hide the public methods of the Object class would you?
No, in general I would not. But sometimes, I need to hide all methods of the
base class, and this is not possible in C#.


Thank goodness. That would, of course, break the Liskov
Substitutability Principle.
I am using C++ for too long time, perhaps.
Possibly :)
Consider the following scenario:

I have a class A:

class A
{
int Get(...);
int Set(...);
};

which does whatever it does and servers as a base class for both class B,
which is used internally and class C, which is exposed to be used by 3rd
party. Let's assume that A.Get and A.Set methods are not "safe", and can be
use only by us (in derived class B), but we can provide "safe" limited
functionality to 3rd party (class C).


<snip>

This sounds like exactly the reason for the protected modifier. If Get
and Set are only meant to be called from derived classes, they should
be protected.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

P: n/a
"Stanislav Simicek" <St**************@discussions.microsoft.com> wrote in
message news:92**********************************@microsof t.com...
A.Get and A.Set are not available for public use. (A methods could be
protected, but let's say we need them public due to way they are used by us internally). The point is, that if anyone adds any method to class A, it is not automatically available in class C, so C "safety" remains.


A better way, which C++ does not have, is to define and expose an interface
to the 3rd party, not the class itself.

-- Alan
Nov 16 '05 #8

P: n/a
"Stanislav Simicek" <St**************@discussions.microsoft.com> wrote in
message news:92**********************************@microsof t.com...
You wouldn't want to hide the public methods of the Object class would
you?
No, in general I would not. But sometimes, I need to hide all methods of the base class, and this is not possible in C#. I am using C++ for too long time, perhaps. Consider the following scenario:

I have a class A:

class A
{
int Get(...);
int Set(...);
};

which does whatever it does and servers as a base class for both class B,
which is used internally and class C, which is exposed to be used by 3rd
party. Let's assume that A.Get and A.Set methods are not "safe", and can be use only by us (in derived class B), but we can provide "safe" limited
functionality to 3rd party (class C). This can be easily done in C++ as:

class C : protected A
{
int GetOnlySomething() { return Get("Something"); }
int SetOnlySomething() { return Set("Something"); }
};

while having

class B : public A
{
...
};

A.Get and A.Set are not available for public use. (A methods could be
protected, but let's say we need them public due to way they are used by us internally). The point is, that if anyone adds any method to class A, it is not automatically available in class C, so C "safety" remains.

In C#, you have to implement a new (or override existing virtual) method
with "protected" access modifier, and it will not be automatically done if a public method is added to class A, which makes class C not to be "safe"
anymore.

You may find this example complicated, but it represents real situation
(designed before I've joined the company :-). I also believe, that the whole thing could be solved by changing/improving C++ OO design - but using
"protected" base class access modifier in C# would allow me to port the whole bunch of C++ classes very easily...

Regards,
---
Stanislav


Hiding the base members wrecks the is-a relationship of inheritance. An
elephant that no longer has a trunk isn't an elephant anymore.

Use interfaces to selectively expose members to consumers. The interface
gives the outside world a specific "view" or subset of the total members of
the class(es).

-- Alan
Nov 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.