Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing protected members

Status
Not open for further replies.

gyps

Programmer
Feb 27, 2006
2
0
0
US
Hi

I'm using Visual C++ .Net version 1.1,
but this might be a general C++ question really.

Say we have a base class A with a protected member function "f", and a derived class B with some member function "g".

Now inside "g", I can access "f" for the "this" pointer, in other words:
f();
or
this->f();
compiles (which it should).

But if I try to access the same "f" for another pointer, it gives an error (C2248, cannot access protected member).

Even ((B* const)this)->f(); won't compile.

Is this expected behavior? Or is it just a problem with the MS compiler?

Thanks.. :)
 
Just to clarify, I'm trying to access the function "f" for another pointer of type "A*", or even "B*". It doesn't compile.
 
Code:
#include <iostream>
using namespace std;

class Base
{
public:
	Base() {};

protected:
	void f()
	{
		cout << "In Base::f()" << endl;
	}
};

class Derived : public Base
{
public:
	Derived() {};

public:
	void g()
	{
		f();				// Works fine.
		this->f();			// Works fine.

		Derived* d	= this;
		d->f();				// Works fine.

		Base* b		= this;
		b->f();				// error C2248...
	}

	void x( Base* base )
	{
		base->f();		// error C2248...
		((Derived*)base)->f();	// Works fine.
	}
};

int main()
{
	Derived d;
	d.g();

	return 0;
}
I believe when you cast this to a base class, it's technically not a this pointer anymore. Therefore, since f() is protected, it cannot be accessed.
As you can see in the x() function which takes a Base* pointer, you get the same error; but if you cast it back up to a Derived*, you don't get an error.
 
My guess is that through the cast I think the MS compiler loses the vfptr table with the real address of you function that is why it does not allow this maneuver.

I do not know if this is the expected behaviour but I am sure that you can find some workaround

s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Yes, it's expected behaviour:
C++ Std said:
11.5 Protected member access
1 When a friend or a member function of a derived class references a protected nonstatic member of a base class, an access check applies in addition to those described earlier in clause 11. Except when forming a pointer to member (5.3.1), the access must be through a pointer to, reference to, or object of the derived class itself (or any class derived from that class).
...
So you can't access protected members via base class pointers.
Apropos, this (slightly fanciful;) construct
Code:
((B* const)this)->f();
compiles and works OK...
No any vfptr table pointers in non-virtual classes...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top