473,463 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Create 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 3039
>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

"George" <Ge****@discussions.microsoft.comwrote in message
news:3A**********************************@microsof t.com...
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?
Based on the compile-time type of the instance. It doesn't matter that the
object really is a "derived" -- if it's being accessed through a base
pointer, you get the same kind of access as to other objects subtyped from
base.
>

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 #11
Hi Ben,
I found my previous conclusion that then entry point of an instance must be
public and C++ provides instance level (not class level) access model is not
correct. I have developed the following sample,

in my sample, calling private member instance2.goo2() is correct in Visual
Studio 2005, even if instance2 is not *this*. So I do not think C++ provides
instance level access model. But from my original question, it seems that the
access model is instance level -- derived class can not access protected
member of base class.

So, what is the access model? Any comments?

class base
{
protected:
~base() {}
private:
void foo()
{
base* b = new base;
delete b;
}
};

class derived : public base
{
public:
~derived() {}
private:
void goo (derived& instance2) //derived class object
{

instance2.goo2();
}

void goo2()
{
}
};
regards,
George

"Ben Voigt [C++ MVP]" wrote:
>
"George" <Ge****@discussions.microsoft.comwrote in message
news:3A**********************************@microsof t.com...
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?

Based on the compile-time type of the instance. It doesn't matter that the
object really is a "derived" -- if it's being accessed through a base
pointer, you get the same kind of access as to other objects subtyped from
base.


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 23 '07 #12

"George" <Ge****@discussions.microsoft.comwrote in message
news:52**********************************@microsof t.com...
Hi Ben,
I found my previous conclusion that then entry point of an instance must
be
public and C++ provides instance level (not class level) access model is
not
correct. I have developed the following sample,

in my sample, calling private member instance2.goo2() is correct in Visual
Studio 2005, even if instance2 is not *this*. So I do not think C++
provides
instance level access model. But from my original question, it seems that
the
access model is instance level -- derived class can not access protected
member of base class.

So, what is the access model? Any comments?
As I explained, it is based on the compile-time type. Members and friends
of class derived can access private and protected members of derived through
any derived* (this includes references, local variables, etc, as long as the
compiler can get a "this" pointer of type derived* using the normal casting
rules). I'm ignoring const for this discussion.
>
class base
{
protected:
~base() {}
private:
void foo()
{
base* b = new base;
delete b;
}
};

class derived : public base
{
public:
~derived() {}
private:
void goo (derived& instance2) //derived class object
{

instance2.goo2();
}

void goo2()
{
}
};
regards,
George

"Ben Voigt [C++ MVP]" wrote:
>>
"George" <Ge****@discussions.microsoft.comwrote in message
news:3A**********************************@microso ft.com...
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?

Based on the compile-time type of the instance. It doesn't matter that
the
object really is a "derived" -- if it's being accessed through a base
pointer, you get the same kind of access as to other objects subtyped
from
base.
>

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 23 '07 #13
Thanks Ben,
I am clear now.
regards,
George

"Ben Voigt [C++ MVP]" wrote:
>
"George" <Ge****@discussions.microsoft.comwrote in message
news:52**********************************@microsof t.com...
Hi Ben,
I found my previous conclusion that then entry point of an instance must
be
public and C++ provides instance level (not class level) access model is
not
correct. I have developed the following sample,

in my sample, calling private member instance2.goo2() is correct in Visual
Studio 2005, even if instance2 is not *this*. So I do not think C++
provides
instance level access model. But from my original question, it seems that
the
access model is instance level -- derived class can not access protected
member of base class.

So, what is the access model? Any comments?

As I explained, it is based on the compile-time type. Members and friends
of class derived can access private and protected members of derived through
any derived* (this includes references, local variables, etc, as long as the
compiler can get a "this" pointer of type derived* using the normal casting
rules). I'm ignoring const for this discussion.

class base
{
protected:
~base() {}
private:
void foo()
{
base* b = new base;
delete b;
}
};

class derived : public base
{
public:
~derived() {}
private:
void goo (derived& instance2) //derived class object
{

instance2.goo2();
}

void goo2()
{
}
};
regards,
George

"Ben Voigt [C++ MVP]" wrote:
>
"George" <Ge****@discussions.microsoft.comwrote in message
news:3A**********************************@microsof t.com...
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?

Based on the compile-time type of the instance. It doesn't matter that
the
object really is a "derived" -- if it's being accessed through a base
pointer, you get the same kind of access as to other objects subtyped
from
base.

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 24 '07 #14
private:
void foo()
Because foo( ) is defined as PRIVATE (not protected) in the base class
(typo?)... ;)

[==Peter==]

"George" <Ge****@discussions.microsoft.comwrote in message
news:D7**********************************@microsof t.com...
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. class derived : public base
  13. {
  14. public:
  15. ~derived() {}
  16. private:
  17. void goo()
  18. {
  19. base* b = new derived;
  20. delete b; // error in this line
  21. }
  22. };
  23.  


thanks in advance,
George
Oct 25 '07 #15
Hi Peter,

I am not invoking foo in derived class, the error occurs when I invoke
destructor (protected, by using the statement delete b below). I am confused
why I can not access the protected method of base class in derived class?

void goo()
{
base* b = new derived;
delete b; // error in this line
}
have a good weekend,
George

"Peter Oliphant" wrote:
private:
void foo()

Because foo( ) is defined as PRIVATE (not protected) in the base class
(typo?)... ;)

[==Peter==]

"George" <Ge****@discussions.microsoft.comwrote in message
news:D7**********************************@microsof t.com...
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 26 '07 #16

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

Similar topics

3
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...
4
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...
3
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 {...
14
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...
3
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...
15
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*...
4
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
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...
10
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
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...
0
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...
0
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...
0
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 ...

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.