473,549 Members | 2,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

derived class can not access base class protected member?

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.

Expand|Select|Wrap|Line Numbers
  1. error C2248: 'base::~base' : cannot access protected member declared in
  2. class 'base'
  3.  
Expand|Select|Wrap|Line Numbers
  1. class base
  2. {
  3. protected:
  4. ~base() {}
  5. private:
  6. void foo()
  7. {
  8. base* b = new base;
  9. delete b;
  10. }
  11. };
  12.  
  13. class derived : public base
  14. {
  15. public:
  16. ~derived() {}
  17. private:
  18. void goo()
  19. {
  20. base* b = new derived;
  21. delete b; // error in this line
  22. }
  23. };
  24.  

thanks in advance,
George
Oct 21 '07 #1
15 3063
>I met with a strange issue that derived class function can not access base
>class's protected member. Do you know why?
George,

The code you have isn't using b as a base class, it's just the same as
though the classes were unrelated.

If you add:

friend class derived;

to class base, it will then compile, but whether that's what you
really want is another question.

Dave
Oct 21 '07 #2
Hi Dave,
What do you mean
The code you have isn't using b as a base class, it's just the same as
though the classes were unrelated.
I think I use the code

base* b = new derived;
delete b; // error in this line

in function goo, which is in derived class right?
regards,
George

"David Lowndes" wrote:
I met with a strange issue that derived class function can not access base
class's protected member. Do you know why?

George,

The code you have isn't using b as a base class, it's just the same as
though the classes were unrelated.

If you add:

friend class derived;

to class base, it will then compile, but whether that's what you
really want is another question.

Dave
Oct 22 '07 #3
>I think I use the code
>
base* b = new derived;
delete b; // error in this line

in function goo, which is in derived class right?
The class is a derived class, but your usage isn't.

I'm not sure what you're really trying to do, but since "derived" is
derived from "base" it already is a base class, there's no need to
create one.

Dave
Oct 22 '07 #4
Hi Dave,
What I want to do is,

1. in derived class member function goo, create a new instance of base class
object;

2. call protected method of the base class object instance.

But I do not know why there is access violation error in step 2, since I
think we can access protected member from derived class, right?
regards,
George

"David Lowndes" wrote:
I think I use the code

base* b = new derived;
delete b; // error in this line

in function goo, which is in derived class right?

The class is a derived class, but your usage isn't.

I'm not sure what you're really trying to do, but since "derived" is
derived from "base" it already is a base class, there's no need to
create one.

Dave
Oct 22 '07 #5
>1. in derived class member function goo, create a new instance of base class
>object;

2. call protected method of the base class object instance.

But I do not know why there is access violation error in step 2, since I
think we can access protected member from derived class, right?
A derived class can access *its* base class protected members, but
clearly from the error you're getting, it can't do it for an arbitrary
instance of the base class. I think you need to use "friend" to do
that.

Dave
Oct 22 '07 #6
So, Dave, as you mentioned below,
A derived class can access *its* base class protected members, but
clearly from the error you're getting, it can't do it for an arbitrary
instance of the base class.
I think we can understand that C++ access module is based on instance level,
not class level. Right?
regards,
George

"David Lowndes" wrote:
1. in derived class member function goo, create a new instance of base class
object;

2. call protected method of the base class object instance.

But I do not know why there is access violation error in step 2, since I
think we can access protected member from derived class, right?

A derived class can access *its* base class protected members, but
clearly from the error you're getting, it can't do it for an arbitrary
instance of the base class. I think you need to use "friend" to do
that.

Dave
Oct 22 '07 #7
So, Dave, as you mentioned below,
A derived class can access *its* base class protected members, but
clearly from the error you're getting, it can't do it for an arbitrary
instance of the base class.
I think we can understand that C++ access module is based on instance level,
not class level. Right?
regards,
George

"David Lowndes" wrote:
1. in derived class member function goo, create a new instance of base class
object;

2. call protected method of the base class object instance.

But I do not know why there is access violation error in step 2, since I
think we can access protected member from derived class, right?

A derived class can access *its* base class protected members, but
clearly from the error you're getting, it can't do it for an arbitrary
instance of the base class. I think you need to use "friend" to do
that.

Dave
Oct 22 '07 #8
>A derived class can access *its* base class protected members, but
>clearly from the error you're getting, it can't do it for an arbitrary
instance of the base class.

I think we can understand that C++ access module is based on instance level,
not class level. Right?
I'm not sure what you'd call it (I'm not a language expert, I just use
it).

Dave
Oct 22 '07 #9
Cool, Dave. I appreciate all of your help on this topic.
regards,
George

"David Lowndes" wrote:
A derived class can access *its* base class protected members, but
clearly from the error you're getting, it can't do it for an arbitrary
instance of the base class.
I think we can understand that C++ access module is based on instance level,
not class level. Right?

I'm not sure what you'd call it (I'm not a language expert, I just use
it).

Dave
Oct 22 '07 #10

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

Similar topics

3
2336
by: Teis Draiby | last post by:
I want to write a base class that includes a member function that creates an instance of a derrived class and returns a pointer to it. Problem: The derived class definition has to follow the base class definition. Therefore I can't specify the return type of the function, that returns a pointer to the derived class, because the derived class...
4
2983
by: Siemel Naran | last post by:
Can Derived class static member access protected member from base class? class Base { protected: void setvariable(int); }; class Derived : Base { public: static std::auto_ptr<Base> out(new Derived());
3
3836
by: Jasleen | last post by:
I want to access the protected member hresult of the Exception class, so I derived my own class from ApplicationException and SystemException. public class LoggedException : SystemException { public LoggedException() : base() { } public LoggedException(String message) : base (message)
14
24553
by: Edward Diener | last post by:
In C++ one can change the access level in a derived class by the "using" keyword. So, for instance, a public method in a base class can be made private in a derived class. Is there any way of doing this in C# ?
3
2046
by: keith.thornhill | last post by:
hey all, got a problem here using Visual basic .net 2005 i have two pairs of base/derived classes. lets call them Base/Derived and BaseStruct/DerivedStruct. i want to be able to instantiate a DerivedStruct in my Derived class, but have the instance of it be accessible from both the Base class (as BaseStruct) and Derived class (as...
15
3823
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can* populate the variable, but the population is not *required* by the derived class (which must be the case). What would meet the requirements is if I...
4
2932
by: softwaredoug | last post by:
Here is some test code I've been attempting to compile (Visual Studio 2003) test.h: class Base { protected: Base() {} public:
3
1967
by: Edan | last post by:
I have a base class with protected members (`Base`). The function `MakeBase()` is a member function of another class, that returns a `Base` object initialized with private members of this class. Now the thing is, I want to extend the functionality of `Base` class. I do that by inheriting `Base` class by `Derived`, but then I can't use...
10
4054
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...
0
7532
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...
0
7462
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...
0
7730
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. ...
0
7975
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...
1
7492
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...
0
6059
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...
0
3510
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...
1
1069
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
777
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...

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.