473,395 Members | 1,975 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.

Base class access specifier

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
8 4658
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
> Unfortunately, there is not.

That's bad news (and perhaps C# language flaw). Thank you anyway.
Nov 16 '05 #3
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
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
> 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
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Xavier | last post by:
Hi, I have a question, in a "dreaded diamond" situation, regarding the following code: ---- Begin code #include <iostream> using namespace std;
1
by: Tony Johansson | last post by:
Hello! Here I have one base klass and three subklasses. I Just want to have your opinion about the design of these klasses. I don't think that my design is good. I think that I instead should...
6
by: Alf P. Steinbach | last post by:
#include <iostream> struct Belcher { int x; void belch() { std::cout << x << std::endl; } Belcher( int anX ): x( anX ) {} }; struct Person: private Belcher { Person(): Belcher( 666 ) {} };
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
10
by: Mike - EMAIL IGNORED | last post by:
In my POD class: class MyPodClass { public: MyPodClass(MyCtorArgs); ... private: short data1; short data2;
2
by: Water Cooler v2 | last post by:
If I recall correctly, in C++ there was an access specifier that could label a data member of a class as "can be accessed by everyone else EXCEPT the derived class" -- an accurate opposite of...
2
by: sarathy | last post by:
Hi, What is the difference b/w a "storage class" and "storage class specifier"? It is said that there, Storage Class --automatic static ( K&R ) Storage Class Specifier --auto extern register...
32
by: moleskyca1 | last post by:
This may be stupid question, but why is sizeof(Base) == 1 in: int main(int argc, char* argv) { class Base { }; cout << sizeof(Base) << endl; return 0; }
6
by: fcvcnet | last post by:
Hi, I read the book C++ Primer, Fourth Edition By Stanley B. Lippman, Jos¨¦e Lajoie, Barbara E. Moo "If we define a class using the class keyword, then any members defined before the first...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.