473,910 Members | 6,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"this" and "base"

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, not only the members in the base class, but the
whole base class itself - as an object, which contains the members.
Therefore, this has *almost* the same effect as if you declared a base
class manually in the derived class as shown in figure 1:

public class Base
{
private string name;
// more members and methods...
}

public class Derived
{
// instance members:
Base baseField;
private uint numbersField;
}
and if you tried to access the private member "name" of baseField
inside a method in the derived class like: baseField.strin g, you would
get an error, because it is private and can only be accessed within
it's own class scope (container). And similarly, this holds true for
why you cannot access a private base member in a derived class in a
"base and derived class relationship". This theory makes good, perfect
logical sense (to me) of why you *cannot* access a private member
*because* you're trying to access a private member outside of the
container where it is defined. And also, the keyword "base" then
essentially becomes the reference to that base class object. Just like
"this" is a reference to an instantiated object. And this(not the
keyword), also, essentially means this: when you derived a class from a
base class, you then have "two" classes within one unit, and this unit
is the drived class.

To elaborate some more, let's throw in a problematic problem:
So with that just said, How then does the keywords "base" and "this"
refer to the same "data" (fields etc..) and "code" (methods etc..) - at
times - when they *are* two different objects which are within one unit
- the derived class (this == drived class object AND base == base class
object)? But, they also, sometimes can refer to different "code" (when
you override). I believe the reason is because when you do not override
a virtual method in the base class, the compiler (or early binding)
makes both objects "this" and "base" point to the same "code". But in
the case where you do override a virtual method in the derived class,
the compiler (or early bindig) makes each object (this AND base) point
to different methods (code).

Sorry for the long post but I decided to try to make some sense out of
this after I encountered a problem ( which I could've posted instead of
all this but decided against; in fear of being told "it's becase it's a
private". That alone, I believe is not a clear answer, but a vague one.
But anyways, I am going to include this code:

public class Car
{
private uint odometer;
}

public class RacingCar : Car
{
private uint odometer;

public static void Main( )
{
}
}

The above compliles with no errors and only two warnings (no
assignement). But the problem I see is this: Why do I *not* get an
error from the compiler, along the lines of "error: redefinition of
Car.odometer", because I inherit a member with the exact same signature
that's also declared in RacingCar? And as I said; someone looking at
this would probably say "because Car.odometer is private and it's
inside its own class scope". And it's true that it's within its own
scope, which would prove my theory of two seperate class objects
(through "base" and "this") exist when you have a derived class, but,
that alone is not enough of an answer to me so I wrote all this for
some enlightment and confirmationt. What's your take or view on this?

Jan 23 '06 #1
7 2260
> What's your take or view on this?

It doesn't work because the member "is a private".
But your explanation would be a good way to put it to beginner
programmers who had never heard of OO...

Jan 23 '06 #2
"What keeps you from accessing a Private member" is more of what you are
looking for I think? You know why (because it is private) but you are not
sure what (compiler, runtime, something else) is keeping you from accessing
it. Well.. It is both the Compiler and Runtime engine that keep you from
doing it. If you tried to compile something that accessed a private member
you would get a compile error. During runtime the access level (public,
private, protected) gets checked to determine if you are allowed access to
it.

You can verify this by doing the following:

Create a Control Library with a public method in it and compile it.
Create an application that calls the public method in this Control Library
and compile it.
Run the application and it should work fine.
Now go back to your Control Library and change that method to Private and
recompile _JUST_ the Control Library.
Now run the application.
It should give you an exception because the runtime engine is doing access
level checking!

When you Override it does not change what base points to. The reason you
would use base in the first place would be to get to the "original" version
of a method that you have overridden.

Lastly, the reason that your last example compiles and works fine is because
of Scope. When a member is declared makes all the difference. If you had a
member function in your derived class you could have a local variable called
odometer in it as well and it would work just fine!

So.. To answer your question in 3 words.. "Because it's Private!" :-)

Hope this helps ya out!
"relient" <xl************ ***@gmail.com> wrote in message
news:11******** ************@g4 3g2000cwa.googl egroups.com...
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, not only the members in the base class, but the
whole base class itself - as an object, which contains the members.
Therefore, this has *almost* the same effect as if you declared a base
class manually in the derived class as shown in figure 1:

public class Base
{
private string name;
// more members and methods...
}

public class Derived
{
// instance members:
Base baseField;
private uint numbersField;
}
and if you tried to access the private member "name" of baseField
inside a method in the derived class like: baseField.strin g, you would
get an error, because it is private and can only be accessed within
it's own class scope (container). And similarly, this holds true for
why you cannot access a private base member in a derived class in a
"base and derived class relationship". This theory makes good, perfect
logical sense (to me) of why you *cannot* access a private member
*because* you're trying to access a private member outside of the
container where it is defined. And also, the keyword "base" then
essentially becomes the reference to that base class object. Just like
"this" is a reference to an instantiated object. And this(not the
keyword), also, essentially means this: when you derived a class from a
base class, you then have "two" classes within one unit, and this unit
is the drived class.

To elaborate some more, let's throw in a problematic problem:
So with that just said, How then does the keywords "base" and "this"
refer to the same "data" (fields etc..) and "code" (methods etc..) - at
times - when they *are* two different objects which are within one unit
- the derived class (this == drived class object AND base == base class
object)? But, they also, sometimes can refer to different "code" (when
you override). I believe the reason is because when you do not override
a virtual method in the base class, the compiler (or early binding)
makes both objects "this" and "base" point to the same "code". But in
the case where you do override a virtual method in the derived class,
the compiler (or early bindig) makes each object (this AND base) point
to different methods (code).

Sorry for the long post but I decided to try to make some sense out of
this after I encountered a problem ( which I could've posted instead of
all this but decided against; in fear of being told "it's becase it's a
private". That alone, I believe is not a clear answer, but a vague one.
But anyways, I am going to include this code:

public class Car
{
private uint odometer;
}

public class RacingCar : Car
{
private uint odometer;

public static void Main( )
{
}
}

The above compliles with no errors and only two warnings (no
assignement). But the problem I see is this: Why do I *not* get an
error from the compiler, along the lines of "error: redefinition of
Car.odometer", because I inherit a member with the exact same signature
that's also declared in RacingCar? And as I said; someone looking at
this would probably say "because Car.odometer is private and it's
inside its own class scope". And it's true that it's within its own
scope, which would prove my theory of two seperate class objects
(through "base" and "this") exist when you have a derived class, but,
that alone is not enough of an answer to me so I wrote all this for
some enlightment and confirmationt. What's your take or view on this?

Jan 23 '06 #3
C.C. (aka Me):
<quote>worte stuff</quote>

Ok, So I thought about what you said and I wrote something new, which,
ironically makes sense also:P

Tell me what you think about it:

When you inherit from a base class, you inherit their members
regardless of their access modifiers. If they are public members, you
can refer to them with the "this" keyword and "base" keyword (that is
if you do not hide them as will be explained next). But you can hide
their base implementation and implement your own version by declaring
members, in the derived class, with the same name and signature as the
base members you wish to hide. Then, the "this" keyword refers to your
implementation instead of the base implementation (which are totally
seperate now). But you can still gain access to the original
implementation by using the "base" keyword.

If the base members are private members, they can only be refered to
with the "base" keyword because of their scope (defined within base
class scopeness).

Jan 23 '06 #4
relient <xl************ ***@gmail.com> wrote:

<snip>
If the base members are private members, they can only be refered to
with the "base" keyword because of their scope (defined within base
class scopeness).


No. Using "base" doesn't give you any more access to the members. If
they're private, then only code in that type (and in nested type) can
get at them.

Basically, just because you've inherited the members doesn't mean you
have access to them - inheritance and access are separate concepts.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 23 '06 #5
The answer is much simpler than you imagine. As a programmer, you should
know it already. When you write a program, you create a set of business
rules that determine how the program operates, what it does, how it does it,
when it must, can and cannot perform certain operations, what the exceptions
to the general rules are, etc. These rules are enformced with conditional
logic in the program. If you don't write the code correctly, the rules are
broken.

These rules generally follow a certain logic, but ultimately, they are
arbitrary. The creator of the program is the arbiter of the rules.

The same applies to a programming language (or any language for that
matter). There are business rules that govern how a programming language is
compiled and run, and these rules are determined by the author of the
compiler and the programming language. These decisions are arbitrary.

For example, since C# and VB.Net compile to the same MSIL, why can't you use
pointer arithmetic in VB.Net? Well, one might imagine all sorts of
apparently logical reasons, but when it comes down to it, the answer is that
the developers of VB.Net made a decision that VB.Net would not support
pointer arithmetic. It is entirely possible that they could have decided
otherwise. They have even talked about supporting it in future versions. But
the point is that, ultimately, they made a decision to create the rule in
that fashion, and that they made it arbitrarily.

Here's another example: In some states, the legal drining age is 18. In
others, the legal age is 21. Why? Because the legislatures of these states
made the decision that the rule would be thus.

Rules and law are not so much about right and wrong, subjectively logical
versus subjectively illogical, but simply because a decision must be made.
Without rules, there is chaos.

The answer to your question is therefore, the reason that you cannot access
a private inherited field in a base class from a derived class is that the
developers of the CLI made an arbitrary decision that the rule should be
thus. You can imagine all you like what the logic behind it is. However, no
matter how much you speculate, you will never know unless they tell you. And
even if you know, it changes nothing. The rule remains. It is what it is.
Until the creator decides otherwise.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"relient" <xl************ ***@gmail.com> wrote in message
news:11******** ************@g4 3g2000cwa.googl egroups.com...
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, not only the members in the base class, but the
whole base class itself - as an object, which contains the members.
Therefore, this has *almost* the same effect as if you declared a base
class manually in the derived class as shown in figure 1:

public class Base
{
private string name;
// more members and methods...
}

public class Derived
{
// instance members:
Base baseField;
private uint numbersField;
}
and if you tried to access the private member "name" of baseField
inside a method in the derived class like: baseField.strin g, you would
get an error, because it is private and can only be accessed within
it's own class scope (container). And similarly, this holds true for
why you cannot access a private base member in a derived class in a
"base and derived class relationship". This theory makes good, perfect
logical sense (to me) of why you *cannot* access a private member
*because* you're trying to access a private member outside of the
container where it is defined. And also, the keyword "base" then
essentially becomes the reference to that base class object. Just like
"this" is a reference to an instantiated object. And this(not the
keyword), also, essentially means this: when you derived a class from a
base class, you then have "two" classes within one unit, and this unit
is the drived class.

To elaborate some more, let's throw in a problematic problem:
So with that just said, How then does the keywords "base" and "this"
refer to the same "data" (fields etc..) and "code" (methods etc..) - at
times - when they *are* two different objects which are within one unit
- the derived class (this == drived class object AND base == base class
object)? But, they also, sometimes can refer to different "code" (when
you override). I believe the reason is because when you do not override
a virtual method in the base class, the compiler (or early binding)
makes both objects "this" and "base" point to the same "code". But in
the case where you do override a virtual method in the derived class,
the compiler (or early bindig) makes each object (this AND base) point
to different methods (code).

Sorry for the long post but I decided to try to make some sense out of
this after I encountered a problem ( which I could've posted instead of
all this but decided against; in fear of being told "it's becase it's a
private". That alone, I believe is not a clear answer, but a vague one.
But anyways, I am going to include this code:

public class Car
{
private uint odometer;
}

public class RacingCar : Car
{
private uint odometer;

public static void Main( )
{
}
}

The above compliles with no errors and only two warnings (no
assignement). But the problem I see is this: Why do I *not* get an
error from the compiler, along the lines of "error: redefinition of
Car.odometer", because I inherit a member with the exact same signature
that's also declared in RacingCar? And as I said; someone looking at
this would probably say "because Car.odometer is private and it's
inside its own class scope". And it's true that it's within its own
scope, which would prove my theory of two seperate class objects
(through "base" and "this") exist when you have a derived class, but,
that alone is not enough of an answer to me so I wrote all this for
some enlightment and confirmationt. What's your take or view on this?

Jan 23 '06 #6
Why do you think so much about things that are so clear?

The people who have designed the language have defined that a private
member can only by accessed from within the class. Even derived classes
have no access to private base-class members.

Have you thought about this: what if your theory was the one used to
design a language? It would mean that making a different between
private, protected and public is of no sence because then you can create
a inheriting class that makes all private members public.

So, just take it as it is: make your private members protected (this is
why something like protected exists).

Have you ever asked someone why 3 = 2 + 1. I can easily write a theory
that says that 2 + 1 = 4.

Greetz,
Rudderius

relient wrote:
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, not only the members in the base class, but the
whole base class itself - as an object, which contains the members.
Therefore, this has *almost* the same effect as if you declared a base
class manually in the derived class as shown in figure 1:

public class Base
{
private string name;
// more members and methods...
}

public class Derived
{
// instance members:
Base baseField;
private uint numbersField;
}
and if you tried to access the private member "name" of baseField
inside a method in the derived class like: baseField.strin g, you would
get an error, because it is private and can only be accessed within
it's own class scope (container). And similarly, this holds true for
why you cannot access a private base member in a derived class in a
"base and derived class relationship". This theory makes good, perfect
logical sense (to me) of why you *cannot* access a private member
*because* you're trying to access a private member outside of the
container where it is defined. And also, the keyword "base" then
essentially becomes the reference to that base class object. Just like
"this" is a reference to an instantiated object. And this(not the
keyword), also, essentially means this: when you derived a class from a
base class, you then have "two" classes within one unit, and this unit
is the drived class.

To elaborate some more, let's throw in a problematic problem:
So with that just said, How then does the keywords "base" and "this"
refer to the same "data" (fields etc..) and "code" (methods etc..) - at
times - when they *are* two different objects which are within one unit
- the derived class (this == drived class object AND base == base class
object)? But, they also, sometimes can refer to different "code" (when
you override). I believe the reason is because when you do not override
a virtual method in the base class, the compiler (or early binding)
makes both objects "this" and "base" point to the same "code". But in
the case where you do override a virtual method in the derived class,
the compiler (or early bindig) makes each object (this AND base) point
to different methods (code).

Sorry for the long post but I decided to try to make some sense out of
this after I encountered a problem ( which I could've posted instead of
all this but decided against; in fear of being told "it's becase it's a
private". That alone, I believe is not a clear answer, but a vague one.
But anyways, I am going to include this code:

public class Car
{
private uint odometer;
}

public class RacingCar : Car
{
private uint odometer;

public static void Main( )
{
}
}

The above compliles with no errors and only two warnings (no
assignement). But the problem I see is this: Why do I *not* get an
error from the compiler, along the lines of "error: redefinition of
Car.odometer", because I inherit a member with the exact same signature
that's also declared in RacingCar? And as I said; someone looking at
this would probably say "because Car.odometer is private and it's
inside its own class scope". And it's true that it's within its own
scope, which would prove my theory of two seperate class objects
(through "base" and "this") exist when you have a derived class, but,
that alone is not enough of an answer to me so I wrote all this for
some enlightment and confirmationt. What's your take or view on this?

Jan 23 '06 #7
>> 2 + 1 = 4.

It does, if 1 inherits from 2, and its private count property isn't
made public.

Jan 24 '06 #8

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

Similar topics

21
2181
by: Alex Martelli | last post by:
I hesitate a bit to post this, but... on the Italian Python NG, somebody was asking whether there was any way to convert an integer number x into a string which represents it in an arbitrary base N (given a sequence with a len of N that gives the digits to use) as "a single expression". I haven't found a really general way, much less a clear one, but, the best I have so far is...: def number_in_base(x, N, digits, maxlen=99): return...
14
2269
by: Ernst Murnleitner | last post by:
Dear Readers, Is it possible to forbid conversion from this or use of this in general except where it is explicitly wanted? Reason: I changed my program from using normal pointers to classes A, ... typedef A * APtr;
11
2114
by: Joseph Turian | last post by:
Fellow hackers, I have a class BuildNode that inherits from class Node. Similarly, I have a class BuildTree that inherits from class Tree. Tree includes a member variable: vector<Node> nodes; // For clarity, let this be "orig_nodes" BuildTree includes a member variable:
9
2380
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a class, if the class has more than than one cosntructor, by making sure that each constructor has a different signature. I have managed to learn that and get
4
1771
by: Gecko | last post by:
I noticed that every time I override a class member, the intellisense behavior is to automatically add a call to the base class of the overridden member such as: public override void SomeMethod() { /// Some quality code goes here. base.SomeMethod(); }
2
1724
by: Wade | last post by:
Hi all, We have created some "Base" class pages for our WebForms and UserControls. For instance, when we create a WebForm called "WebForm1.aspx", instead of inheriting from "System.Web.UI.Page" we implement from our "Base" class page which itself inherits from "System.Web.UI.Page" -- I know, pretty standard. We do the same with our UserControls, instead they inherit from "System.Web.UI.UserControl". Now, there are some methods that we...
2
2206
by: qazmlp1209 | last post by:
class base { public: base() { } base(int number) { priNumber = number ;
3
1597
by: Rob | last post by:
I have these classes (elided methods): class Base { public: Base(string name) {...} }; class Derived : public Base {
3
1678
by: Ravi | last post by:
Is this the correct way to think of "base class"? The "base class" is a class from which other classes are derived. The "base class" will never be derived from another class.
0
11349
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10921
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...
0
10541
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
9727
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...
1
8099
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7250
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5939
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4337
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.