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:

private:
void foo()

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

[==Peter==]

"George" <George@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:D783AA73-AC91-45C2-B534-112BEFEC3BAA@xxxxxxxxxxxxxxxx
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


.



Relevant Pages

  • Re: why private virtual function?
    ... private as declared by the "owner"/the base class itself should not be ... so as it is the decision of the derived class about what it wants. ... using declaration looks like a hack to circumvent the bad ... void bar{foo();} // Uh oh. ...
    (microsoft.public.vc.language)
  • Re: virtual keyword for the derived class
    ... virtual_start void foo(); ... The compiler then could check in the case of virtual_cont that in the Base ... transforms derived class functions into virtual ones. ...
    (comp.lang.cpp)
  • Re: Overriding newbie question.
    ... public virtual int MyMethod ... class Bar: Foo ... The other fundemental thing is this will only work if the method is being called on a derived class object. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Base class calling derived class methods
    ... void connect_to_db; ... class bar_1: public foo ... This method just opens a database connection, and each derived class can ... instance is at least a bar instance or further derived otherwise your ...
    (microsoft.public.dotnet.languages.vc)
  • Re: [CLOS] Ensuring a method exists
    ... (defgeneric foo (a b) ... (defclass base () ... I'd like to ensure that if somebody writes a derived class: ... to define the foo method? ...
    (comp.lang.lisp)