473,769 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4678
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.co m

"Stanislav Simicek" <St************ **@discussions. microsoft.com> wrote in
message news:EF******** *************** ***********@mic rosoft.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.publi c.dotnet.langua ges.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 GetOnlySomethin g() { return Get("Something" ); }
int SetOnlySomethin g() { 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
Substitutabilit y 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.co m>
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******** *************** ***********@mic rosoft.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******** *************** ***********@mic rosoft.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 GetOnlySomethin g() { return Get("Something" ); }
int SetOnlySomethin g() { 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
4628
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
1310
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 NOT have done the base class abstract it is so now but that is wrong I think. The base class should be concrete Define then method fetchWeaponName, fetchAllowedAnimal and getPrice in the
6
1686
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
2249
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 me. So, I'm here for an answer (more of a confirmation), hopefully. First let me say that I know people keep saying; it doesn't work because the member "is a private". I believe there's more to it than just simply that... Theory: You inherit,...
10
2079
by: Mike - EMAIL IGNORED | last post by:
In my POD class: class MyPodClass { public: MyPodClass(MyCtorArgs); ... private: short data1; short data2;
2
1291
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 "protected". I think I allude to the "friend" modifier in C++, which is different from the VB.NET "friend". Does VB.NET have any such specifier? Is there a method for a child class to "not" inherit some properties off its base class?
2
3277
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 static typedef What does a specifier mean? I see the name "specifier" also
32
2184
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
2265
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 access label are implicitly private; ifwe usethe struct keyword, then those members are public. Whether we define a class using the class keyword or the struct keyword affects only the default initial access level." Now I defined a struct with...
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9422
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10038
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9987
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9857
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8867
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2812
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.