Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2007

Tip: Looking for answers? Try searching our database.

derived class can not access base class protected member?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
George - 21 Oct 2007 16:45 GMT
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.

[Code]
error C2248: 'base::~base' : cannot access protected member declared in
class 'base'
[/Code]

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

class derived : public base
{
public:
    ~derived() {}
private:
    void goo()
    {
        base* b = new derived;
        delete b; // error in this line
    }
};
[/Code]

thanks in advance,
George
David Lowndes - 21 Oct 2007 18:28 GMT
>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
George - 22 Oct 2007 06:45 GMT
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

> >I met with a strange issue that derived class function can not access base
> >class's protected member. Do you know why?
[quoted text clipped - 12 lines]
>
> Dave
David Lowndes - 22 Oct 2007 08:07 GMT
>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
George - 22 Oct 2007 08:30 GMT
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

> >I think I use the code
> >
[quoted text clipped - 10 lines]
>
> Dave
David Lowndes - 22 Oct 2007 08:40 GMT
>1. in derived class member function goo, create a new instance of base class
>object;
[quoted text clipped - 3 lines]
>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
George - 22 Oct 2007 08:56 GMT
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

> >1. in derived class member function goo, create a new instance of base class
> >object;
[quoted text clipped - 10 lines]
>
> Dave
David Lowndes - 22 Oct 2007 09:38 GMT
>> 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
George - 22 Oct 2007 09:49 GMT
Cool, Dave. I appreciate all of your help on this topic.

regards,
George

> >> 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
[quoted text clipped - 7 lines]
>
> Dave
George - 22 Oct 2007 08:58 GMT
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

> >1. in derived class member function goo, create a new instance of base class
> >object;
[quoted text clipped - 10 lines]
>
> Dave
Ben Voigt [C++ MVP] - 22 Oct 2007 14:17 GMT
> So, Dave, as you mentioned below,
>
[quoted text clipped - 5 lines]
> 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
[quoted text clipped - 14 lines]
>>
>> Dave
George - 23 Oct 2007 08:55 GMT
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

> > So, Dave, as you mentioned below,
> >
[quoted text clipped - 29 lines]
> >>
> >> Dave
Ben Voigt [C++ MVP] - 23 Oct 2007 16:39 GMT
> Hi Ben,
>
[quoted text clipped - 13 lines]
>
> 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
> {
[quoted text clipped - 64 lines]
>> >>
>> >> Dave
George - 24 Oct 2007 04:10 GMT
Thanks Ben,

I am clear now.

regards,
George

> > Hi Ben,
> >
[quoted text clipped - 88 lines]
> >> >>
> >> >> Dave
Peter Oliphant - 26 Oct 2007 00:51 GMT
> private:
> void foo()

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

[==Peter==]

> Hello everyone,
>
[quoted text clipped - 36 lines]
> thanks in advance,
> George
George - 26 Oct 2007 10:23 GMT
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

> > private:
> > void foo()
[quoted text clipped - 44 lines]
> > thanks in advance,
> > George

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.