473,698 Members | 2,942 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

acces base class member from derive class

Please go though following program:
#include <iostream.h>

class base {
public:
int basepublic;
protected:
int baseprotected;
};

class derived : public base {
public:
void compare(const derived *pb){
int a=pb->basepublic;
a=pb->baseprotecte d;
}
};

int main(){

return 0;
}

Now when i compile and run this program, it got all through.
MY question is how it is possible to access protected variable of a
different derive object in derive class's member function?
if i change argument of compare function from
const derived *pb to
const base *pb,
i get compilation error
" "1.cpp", line 14: Error: baseprotected is not accessible from
derived::compar e(const base*)."
which is expected.

Aug 22 '06 #1
3 1652
gyan wrote:
Please go though following program:
#include <iostream.h>

class base {
public:
int basepublic;
protected:
int baseprotected;
};

class derived : public base {
public:
void compare(const derived *pb){
int a=pb->basepublic;
a=pb->baseprotecte d;
}
};

int main(){

return 0;
}

Now when i compile and run this program, it got all through.
MY question is how it is possible to access protected variable of a
different derive object in derive class's member function?
Because *pb is a derived and compare is a method of derived. Forget
base members and just consider derived ones, you would expect to be able
to see those, wouldn't you?

--
Ian Collins.
Aug 22 '06 #2
But Pb has been defined in main, outside the scope of derived class, so how
can we access protected member of pb object.member function of a class
should have access only to data member of its own object i.e. "this" and
not other object.

Aug 23 '06 #3
gyan wrote:
But Pb has been defined in main, outside the scope of derived class, so how
can we access protected member of pb object.member function of a class
should have access only to data member of its own object i.e. "this" and
not other object.
Please quote context. I didn't realise you were replying to me.

In your example that you snipped, main was empty:

#include <iostream.h>
>
class base {
public:
int basepublic;
protected:
int baseprotected;
};

class derived : public base {
public:
void compare(const derived *pb){
int a=pb->basepublic;
a=pb->baseprotecte d;
}
};

int main(){

return 0;
}
pb is a pointer to a derived, so any other instance of a derived can
fiddle with its private parts.

--
Ian Collins.
Aug 23 '06 #4

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

Similar topics

9
5198
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
5
3609
by: eiji | last post by:
Hi folks, I hope this is not "off topic"! :-) Consider the next code: /* Declarations of types that could become platform-dependent */ #define MyChar char #define MyInt int
9
1879
by: olanglois | last post by:
Hi, I am not sure if I have found a compiler bug (I am using VC++.NET2003) or if this is the correct behavior defined by the language but I am sure someone can clear up my confusion. Suppose the following: class Base { protected: int x;
5
5016
by: Dimitry | last post by:
I am trying to make the following: struct Base { char param; }; class Derived1 : public Base { public:
6
2835
by: noel.hunt | last post by:
I have a base class, PadRcv, with virtual functions. User code will derive from this class and possibly supply it's own functions to override the base class virtual functions. How can I test that a user-defined class has overriden the virtual function in the base class? I have code like this which compiles with gcc 2.95.4: Attrib Implicits(PadRcv *obj){ Attrib accum = 0;
8
1687
by: ManicQin | last post by:
Hi lets say I have the next situation class base { pubic: ;) base() {} virtual ~base(){} virtual init() {//do init } };
3
2045
by: tomPee | last post by:
Hi, I have the following problem: I am trying to make some sort of base class menu that i can then use to derive other menu's from. Those menu's should then be able to interact with each other. And, i have most of the idea figured out and I thought out how i want to do it. But when i started coding i found a slight... difficulty. It might be easy to overcome, but google let me down :( and my own imagination made one happy jump, but...
3
1441
by: Rahul | last post by:
While reading Inside the C++ object model I came through the following paragraph Point3d origin, *ptr = &origin; A) origin.x = 0.0; B) ptr->x = 0.0; The book says "A & B statements performs equivalently if x is a member of a struct, class, single inheritance hierarchy, or multiple inheritance hierarchy" This is because compiler knows the offset of
10
4103
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not allowed to access the protected data members of the base object. This surprises me. Can someone explain why this is? I suspect there is a good reason and I am just having a slow day to not come up with it myself. Bob
0
8611
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
9170
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
9031
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
7741
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
6531
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
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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.