473,394 Members | 1,932 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,394 software developers and data experts.

Protected access for instance members

Hi,

Why must derived classes access protected base members through an instance
of a derived class? The following code would work in C++, why doesn't it in
C#?

public class A
{
protected int x;
}

public class B: A
{
static void F(A a)
{
a.x = 1; // Generates compile error...must access through instance of B
}
}

Maybe someone needs to revisit the C# language specification...

Thanks,
Nate
Nov 16 '05 #1
3 1754
Nathan Wiegman wrote:
Hi,

Why must derived classes access protected base members through an instance
of a derived class? The following code would work in C++, why doesn't it in
C#?

public class A
{
protected int x;
}

public class B: A
{
static void F(A a)
{
a.x = 1; // Generates compile error...must access through instance of B
}
}


???

class A
{
protected:
int x;
};

class B : A
{
public:
static void F(A a)
{
a.x = 1;
}
};

error C2248: 'A::x' : cannot access private member declared in class 'A'
--
Tom Porterfield
Nov 16 '05 #2
Ah...looks like I was wrong. You can't do that, even in C++. But, still, my
question is:

Why Not?

It's almost as if I need a new access modifier...perhaps
"allow_derived_access".

Thanks,
Nate

"Tom Porterfield" wrote:
Nathan Wiegman wrote:
Hi,

Why must derived classes access protected base members through an instance
of a derived class? The following code would work in C++, why doesn't it in
C#?

public class A
{
protected int x;
}

public class B: A
{
static void F(A a)
{
a.x = 1; // Generates compile error...must access through instance of B
}
}


???

class A
{
protected:
int x;
};

class B : A
{
public:
static void F(A a)
{
a.x = 1;
}
};

error C2248: 'A::x' : cannot access private member declared in class 'A'
--
Tom Porterfield

Nov 16 '05 #3
"Nathan Wiegman" <Na***********@discussions.microsoft.com> wrote in message
news:8B**********************************@microsof t.com...
Ah...looks like I was wrong. You can't do that, even in C++. But, still,
my
question is:

Why Not? I was surprised too, but I found the explanation in Stroustrup's "The C++
programming Language":
"A derived class can access a base class' protected members only for objects
of its own type. ... This prevents subtle errors that would otherwise occur
when one derived class corrupts data belonging to other derived classes."

This seems to make sense. If you have classes B and C both derived from
class A (which has a protected member x), it's not reasonable to allow a
member function of class B to access a class C's x value.

Chris Jobson
It's almost as if I need a new access modifier...perhaps
"allow_derived_access".

Thanks,
Nate

"Tom Porterfield" wrote:
Nathan Wiegman wrote:
> Hi,
>
> Why must derived classes access protected base members through an
> instance
> of a derived class? The following code would work in C++, why doesn't
> it in
> C#?
>
> public class A
> {
> protected int x;
> }
>
> public class B: A
> {
> static void F(A a)
> {
> a.x = 1; // Generates compile error...must access through
> instance of B
> }
> }


???

class A
{
protected:
int x;
};

class B : A
{
public:
static void F(A a)
{
a.x = 1;
}
};

error C2248: 'A::x' : cannot access private member declared in class 'A'
--
Tom Porterfield

Nov 16 '05 #4

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

Similar topics

7
by: John A. Byerly | last post by:
Hi, I have a couple of classes that are causing compile errors. I hope someone can shed some light as to why. class A { public: A(X* pX) : m_pX(pX) {};
4
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is,...
5
by: Laban | last post by:
Hi, I am new to C# & .NET. I have a class A which contains a protected member M that I would like to access. If I derive my own class B from A, I can access M from within the class, and also...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
16
by: Fir5tSight | last post by:
Hi All, I have a small C#.NET program that is as follows: using System; class A { protected int x = 123; }
2
by: Rob Richardson | last post by:
Greetings! Consider the following: class CBase { public: CBase(); virtual ~CBase();
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
8
by: Mayur H Chauhan | last post by:
All, For my knowledge, if I declare Class as follow, then it thows compilation error. Protected Class Book End Class Even same for...
5
by: Timothy Madden | last post by:
Hy static members of non-integral type need to be declared in the class, but defined (and constructed or initialized) outside the class. Like this class SystemName { public:
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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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
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...

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.