Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to method pointer to protected method?

This code:

class B {
 protected:
  void Foo(){}
}

class D : public B {
 public:
  void Baz() {
    Foo();
  }
  void Bar() {
    printf("%x\n", &B::Foo);
  }
}

gives this error:

t.cpp: In member function 'void D::Bar()':
Line 3: error: 'void B::Foo()' is protected
  • Why can I call a protected method but not take its address?
  • Is there a way to mark something fully accessible from derived classes rather than only accessible from derived classes and in relation to said derived class?

BTW: This looks related but what I'm looking for a reference to where this is called out in the spec or the like (and hopefully that will lead to how to get things to work the way I was expecting).

like image 911
BCS Avatar asked Apr 28 '11 17:04

BCS


People also ask

How can we access protected method from derived class?

You can access a protected member only via inheritance (apart from the methods of the same class). Say for example you have a class Derived1 which inherits from Derived , then objects of Derived1 can call foo() . EDIT: MSDN article on protected access specifier.

Can you access a protected member of a class from a non member function?

Protected members in a class are similar to private members as they cannot be accessed from outside the class. But they can be accessed by derived classes or child classes while private members cannot.

Can you override a protected method C++?

Yes, the protected method of a superclass can be overridden by a subclass. If the superclass method is protected, the subclass overridden method can have protected or public (but not default or private) which means the subclass overridden method can not have a weaker access specifier.

What can access protected methods?

protected access modifier allows your subclasses be outside your superclass package and yet still inherit pieces of the class including methods and constructors.. The only way the subclass can access the protected methods is by inheritance...


1 Answers

You can take the address through D by writing &D::Foo, instead of &B::Foo.

See this compiles fine : http://www.ideone.com/22bM4

But this doesn't compile (your code) : http://www.ideone.com/OpxUy


Why can I call a protected method but not take its address?

You cannot take its address by writing &B::Foo because Foo is a protected member, you cannot access it from outside B, not even its address. But writing &D::Foo, you can, because Foo becomes a member of D through inheritance, and you can get its address, no matter whether its private, protected or public.

&B::Foo has same restriction as b.Foo() and pB->Foo() has, in the following code:

void Bar() {
    B b;
    b.Foo();     //error - cannot access protected member!
    B *pB = this;
    pB->Foo();   //error - cannot access protected member!
  }

See error at ideone : http://www.ideone.com/P26JT

like image 142
Nawaz Avatar answered Sep 25 '22 02:09

Nawaz