Skip to main content
gamedev.net
🔒 Locked

Silly inheritance question

Started by chipmeisterc Jan 23, 2007 at 3:21 PM 3 replies 600+ views
Original Post
chipmeisterc
chipmeisterc
You know when you have those days, where you cant remember for the life of you something once so very simple! Well it would appear that my brain cells have abandoned me and I'm having one of those days. If I have a BaseObject class with a private member variable say int m_ID; and then I have an inherited class 'Inherited: public BaseObject' If In any of my 'Inherited' functions I try to reference m_ID i am greeted with 'Cannot access private member variable defined in BaseObject' which would surely mean either make it public (surely breaking the rules of oop) or redefining it in Inherited, i.e also give 'Inherited' a private int m_ID; My question is will this work, and do what i want it to do? or am i talking nonsense. If I then had an accessor function in my BaseObject like virtual int GetId( void ) { return m_ID;} would that return the right value ? i.e 'Inherited's' m_ID or am i being stupid? Thanks in advance! Chris
njpaul
njpaul
Make the variable protected. Subclasses can then access it, but other classes cannot.
CTar
CTar
Only your friends may touch your private parts. You only make something private if it really is private. If it isn't really completely private, and you just want to protect it from the outside then you make it protected (friends and derived classes can use it).

The GetId would also do the job if it's public or protected, but unless you want all of your derived classes to get your id, then you shouldn't do it.
chipmeisterc
chipmeisterc
Protected!
I am an idiot!! Haha thankyou!!, I really should have remembered that! :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.