Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Invocation...Permission problem on a derived private method from public method

1 view
Skip to first unread message

Venkat

unread,
Apr 19, 2007, 1:16:38 AM4/19/07
to
Hi,

Is there a good explanation why compilation tags error for the call to
AA.method(). It is public in the base class. If cast to baseclass,
then compiler says it's ok.

class A {

public:
A() {}
virtual ~A() {}
virtual void method(void) { std::cout << "A::method" <<
std::endl; }
};// A

class AA : public A {
public:
AA() {}
virtual ~AA() {}

private:
virtual void method(void) {
A::method();
std::cout << "AA::method" << std::endl;
}
};// A

int main()
{
AA obj;
A* p = &obj;
p->method();
//((A&)obj).method();

obj.method(); // ERROR:

drvd.cpp:26: error: `virtual void AA::method()' is private
drvd.cpp:46: error: within this context

return 0;
}

gcc -o drvd drvd.cpp -lstdc++ -lm
drvd.cpp: In function `int main()':
drvd.cpp:26: error: `virtual void AA::method()' is private
drvd.cpp:46: error: within this context


Thanks
Venkat


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Maciej Sobczak

unread,
Apr 19, 2007, 4:51:10 AM4/19/07
to
Venkat wrote:

> Is there a good explanation why compilation tags error for the call to
> AA.method().

A good explanation is that you call it "through" the AA type, where this
function is private.

> If cast to baseclass,
> then compiler says it's ok.

Because it is OK in the base class.

The problem is with the design of these two classes. NVI (Non Virtual
Interface) would probably be much cleaner here, without such effects.


--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/

Salt_Peter

unread,
Apr 19, 2007, 7:09:11 AM4/19/07
to
On Apr 19, 1:16 am, Venkat <swara...@yahoo.com> wrote:
> Hi,
>
> Is there a good explanation why compilation tags error for the call to
> AA.method(). It is public in the base class. If cast to baseclass,
> then compiler says it's ok.

Its not ok.
Casting to baseclass calls the public A::method(), not the private
AA::method().

When calling an overridden virtual member function polymorphically,
Only the access specifier in the pointer's type is relevent.

When calling the member function directly,
the access specifier in the type involved is relevent, as expected.

So the reason is: the interface(s) provided dictates a member
function's accessibility.
The pointer to baseclass does not have the same interface as the
derived object does in this case.
Why? because it was written that way.

see void accessor() below...

>
> class A {
>
> public:
> A() {}
> virtual ~A() {}
> virtual void method(void) { std::cout << "A::method" <<
> std::endl; }
>
> };// A
>
> class AA : public A {
> public:
> AA() {}
> virtual ~AA() {}
>
> private:
> virtual void method(void) {
> A::method();
> std::cout << "AA::method" << std::endl;
> }

public:
void accessor()
{
method();


}
>
> };// A
>
> int main()
> {
> AA obj;
> A* p = &obj;
> p->method();
> //((A&)obj).method();

// calls A::method() on the object's base
static_cast< A >(obj).method();

// now has a public interface to access the private virtual
override
obj.accessor();

>
> obj.method(); // ERROR:

Its an error because you chose that interface.

>
> drvd.cpp:26: error: `virtual void AA::method()' is private
> drvd.cpp:46: error: within this context
>
> return 0;
>
> }
>
> gcc -o drvd drvd.cpp -lstdc++ -lm
> drvd.cpp: In function `int main()':
> drvd.cpp:26: error: `virtual void AA::method()' is private
> drvd.cpp:46: error: within this context
>
> Thanks
> Venkat
>
> --

> [ Seehttp://www.gotw.ca/resources/clcm.htmfor info about ]

Venkat

unread,
Apr 20, 2007, 2:42:11 PM4/20/07
to
On Apr 19, 1:51 am, Maciej Sobczak <no.s...@no.spam.com> wrote:
> Venkat wrote:
> > Is there a good explanation why compilation tags error for the call to
> > AA.method().
>
> A good explanation is that you call it "through" the AA type, where this
> function is private.
>
> > If cast to baseclass,
> > then compiler says it's ok.
>
> Because it is OK in the base class.
>
> The problem is with the design of these two classes. NVI (Non Virtual
> Interface) would probably be much cleaner here, without such effects.
>

Note that base class could be given to consumers who have no idea
of AA, but only A. So the idea is callback handler, but implementation
could be different from A. I'm not sure why it's seen as a design
problem.

Thanks
Venkat

--

Venkat

unread,
Apr 20, 2007, 6:48:56 PM4/20/07
to

I'm of the impression interface of AA is itself and its parent base
class(es). If so, method() is visible via the base class A. or
instance and interface is 1-to-1 mapped?

Any pointers where the access permissions and interface associations
are explained? (TCPL is not helpful afaik).

Thanks
Venkat

>
>
>
> > drvd.cpp:26: error: `virtual void AA::method()' is private
> > drvd.cpp:46: error: within this context
>
> > return 0;
>
> > }
>
> > gcc -o drvd drvd.cpp -lstdc++ -lm
> > drvd.cpp: In function `int main()':
> > drvd.cpp:26: error: `virtual void AA::method()' is private
> > drvd.cpp:46: error: within this context
>
> > Thanks
> > Venkat
>


--

uglystone

unread,
Apr 21, 2007, 4:44:27 AM4/21/07
to
{ Quoted clc++m banner and signature removed. -mod }

There is an explanation in the book "inside the c++ object model"
please refer to chapter one !
According to its' explation,
When we write
p->method();
the type of p determines the following at compile time:
The fixed, available interface (that is, p may invoke only the A
public interface),
The access level of that interface (for example, method() is a public
member of A )
so access level is detemined at complie time!
but run-time don't check it!

Maciej Sobczak

unread,
Apr 23, 2007, 6:51:45 AM4/23/07
to
Venkat wrote:

> Note that base class could be given to consumers who have no idea
> of AA, but only A. So the idea is callback handler, but implementation
> could be different from A. I'm not sure why it's seen as a design
> problem.

It is a design problem in the context that you have provided in your
first post - there was nothing about callbacks, only two classes used
directly.
With the callback, the calling site will use the base interface only, so
the effect you have shown cannot take place anyway.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]

0 new messages