Jump to content
  • Advertisement
Sign in to follow this  
Scythen

derived class access to base class protected members

This topic is 4877 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, we all know that you can access base class protected members within a member function like this
class A
{
protected:
	int m_int;
};
class B : public A
{
public:
	int Func()
	{
		return m_int;
	}
};

But what you cant do is this
//  A is from last example
class B : public A
{
public:
	int Func(const A &a)
	{
		return a.m_int;
	}
};

Unfortunately, this is exactly what I have a need for. In my case, A defines several virtual functions, some of which modify objects of type A. I'd like derived classes to be able to override them and change the implementation. I don’t want to be forced into making derived classes friends of the base class... that’s just ugly :( Anyone know of a nice way to get around this problem? [Edited by - Scythen on February 13, 2005 8:20:53 PM]

Share this post


Link to post
Share on other sites
Advertisement
No, thats not the problem... thanks for the try though.
It's a "cannot access protected member declared in class 'A'" error.

I'm not trying to modify m_int in the example.
This works fine

class B : public A
{
public:
int Func(const B &b)
{
return b.m_int;
}
};


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Include an accessor method in A and use it to get the value you want

instead of

return a.m_int;

use

return a.getm_int();


Share this post


Link to post
Share on other sites
[headshake]Nope, unfortunately that’s no good either...
I don’t want to expose the protected data through public assessors.
I do want derived classes to have access to the base class protected data.
[crying] The more I think about this, the less I believe it is possible...

Share this post


Link to post
Share on other sites
Another possibility, though not particularly elegant, is to make the derived class a friend of the base class.

[edit] Example:
#include <iostream>

using namespace std;


class derived;

class base {
public:
base() : i( 2 ) {}
protected:
int i;

friend class derived;
};

class derived : public base {
public:
int again( const base &b ) {
return b.i;
}
};

int main() {
base b;
derived d;

cout << d.again( b );

cin.get();
return 0;
}


Regards,
jflanglois

[Edited by - jflanglois on February 13, 2005 10:49:26 PM]

Share this post


Link to post
Share on other sites
The following works, but is an ugly evil hack (it also seems to work on classes derived from A when A has a virtual function or a pure virtual function, but I only did a couple of quick tests...):
//  A is from last example
class C : public A
{
public:
int Func( const A& a)
{
return static_cast<const C&>(a).m_int;
}
};

edit: cleaned up stuff that meant nothing and took up space...[smile]

[Edited by - lucky_monkey on February 14, 2005 7:40:54 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by lucky_monkey
Quote:
Original post by Scythen
I don’t want to be forced into making derived classes friends of the base class... that’s just ugly :(

Curse my inability to read late at night. Appologies D:

Share this post


Link to post
Share on other sites
Why do your derived classes need access to the implementation details of your base classes?

This isn't a good design and would have to be something very specific to justify doing it.

What exactly is the protected data?

A good way to look at it is to say 'my classes do something and provide a service, rather than contain something'.

Work out why you are deriving from your base classes, what service they provide. It may be that you can make them a member instead of deriving.

If you need to override virtual functions, see this article about making virtual functions private by default (from the Guru of the Week website)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!