Connecting Tech Pros Worldwide Forums | Help | Site Map

derived class can not access base class protected member?

=?Utf-8?B?R2Vvcmdl?=
Guest
 
Posts: n/a
#1: Oct 21 '07
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

David Lowndes
Guest
 
Posts: n/a
#2: Oct 21 '07

re: derived class can not access base class protected member?


>I met with a strange issue that derived class function can not access base
Quote:
>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
=?Utf-8?B?R2Vvcmdl?=
Guest
 
Posts: n/a
#3: Oct 22 '07

re: derived class can not access base class protected member?


Hi Dave,


What do you mean
Quote:
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:
Quote:
Quote:
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
>
David Lowndes
Guest
 
Posts: n/a
#4: Oct 22 '07

re: derived class can not access base class protected member?


>I think I use the code
Quote:
>
>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
=?Utf-8?B?R2Vvcmdl?=
Guest
 
Posts: n/a
#5: Oct 22 '07

re: derived class can not access base class protected member?


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:
Quote:
Quote:
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
>
David Lowndes
Guest
 
Posts: n/a
#6: Oct 22 '07

re: derived class can not access base class protected member?


>1. in derived class member function goo, create a new instance of base class
Quote:
>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
=?Utf-8?B?R2Vvcmdl?=
Guest
 
Posts: n/a
#7: Oct 22 '07

re: derived class can not access base class protected member?


So, Dave, as you mentioned below,
Quote:
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:
Quote:
Quote:
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
>
=?Utf-8?B?R2Vvcmdl?=
Guest
 
Posts: n/a
#8: Oct 22 '07

re: derived class can not access base class protected member?


So, Dave, as you mentioned below,
Quote:
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:
Quote:
Quote:
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
>
David Lowndes
Guest
 
Posts: n/a
#9: Oct 22 '07

re: derived class can not access base class protected member?


>A derived class can access *its* base class protected members, but
Quote:
Quote:
>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
=?Utf-8?B?R2Vvcmdl?=
Guest
 
Posts: n/a
#10: Oct 22 '07

re: derived class can not access base class protected member?


Cool, Dave. I appreciate all of your help on this topic.


regards,
George

"David Lowndes" wrote:
Quote:
Quote:
Quote:
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
>
Ben Voigt [C++ MVP]
Guest
 
Posts: n/a
#11: Oct 22 '07

re: derived class can not access base class protected member?



"George" <George@discussions.microsoft.comwrote in message
news:3A2015D0-666D-412D-997E-718C6CC21E41@microsoft.com...
Quote:
So, Dave, as you mentioned below,
>
Quote:
>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.
Quote:
>
>
regards,
George
>
"David Lowndes" wrote:
>
Quote:
Quote:
>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
>>

=?Utf-8?B?R2Vvcmdl?=
Guest
 
Posts: n/a
#12: Oct 23 '07

re: derived class can not access base class protected member?


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:
Quote:
>
"George" <George@discussions.microsoft.comwrote in message
news:3A2015D0-666D-412D-997E-718C6CC21E41@microsoft.com...
Quote:
So, Dave, as you mentioned below,
Quote:
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.
>
Quote:


regards,
George

"David Lowndes" wrote:
Quote:
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
>
>
>
>
Ben Voigt [C++ MVP]
Guest
 
Posts: n/a
#13: Oct 23 '07

re: derived class can not access base class protected member?



"George" <George@discussions.microsoft.comwrote in message
news:52F6EA86-CC84-4114-A454-DE3AE58A538E@microsoft.com...
Quote:
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.
Quote:
>
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:
>
Quote:
>>
>"George" <George@discussions.microsoft.comwrote in message
>news:3A2015D0-666D-412D-997E-718C6CC21E41@microsoft.com...
Quote:
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.
>>
Quote:
>
>
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
>>
>>
>>
>>

=?Utf-8?B?R2Vvcmdl?=
Guest
 
Posts: n/a
#14: Oct 24 '07

re: derived class can not access base class protected member?


Thanks Ben,


I am clear now.


regards,
George

"Ben Voigt [C++ MVP]" wrote:
Quote:
>
"George" <George@discussions.microsoft.comwrote in message
news:52F6EA86-CC84-4114-A454-DE3AE58A538E@microsoft.com...
Quote:
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.
>
Quote:

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:
Quote:
>
"George" <George@discussions.microsoft.comwrote in message
news:3A2015D0-666D-412D-997E-718C6CC21E41@microsoft.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
>
>
>
>
>
>
>
Peter Oliphant
Guest
 
Posts: n/a
#15: Oct 26 '07

re: derived class can not access base class protected member?


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

[==Peter==]

"George" <George@discussions.microsoft.comwrote in message
news:D783AA73-AC91-45C2-B534-112BEFEC3BAA@microsoft.com...
Quote:
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
=?Utf-8?B?R2Vvcmdl?=
Guest
 
Posts: n/a
#16: Oct 26 '07

re: derived class can not access base class protected member?


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:
Quote:
Quote:
private:
void foo()
>
Because foo( ) is defined as PRIVATE (not protected) in the base class
(typo?)... ;)
>
[==Peter==]
>
"George" <George@discussions.microsoft.comwrote in message
news:D783AA73-AC91-45C2-B534-112BEFEC3BAA@microsoft.com...
Quote:
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
>
>
Closed Thread