Quote:Original post by Jingo
Here is a hack that lets you use private members in a derived class.
*** Source Snippet Removed ***
Though that is so ugly I shouldn't have even mentioned it.
WHAT THE HECK [lol] I'll have to give that a try [smile]
Quote:Original post by Jingo
Here is a hack that lets you use private members in a derived class.
*** Source Snippet Removed ***
Though that is so ugly I shouldn't have even mentioned it.
class Animal{ public: Animal(); ~Animal(){}; protected: float x,y,z;};class Dog : public Animal{ public: void Wag_Tail();};class Cat : public Animal{ public: void Chase_Mouse();};
class Base {private: int data;public: void SetData(int); int GetData() const;};class Derived : public Base {private:public: void func(int n) { SetData(n); }};
Quote:Original post by snk_kidQuote:Original post by MobileOakQuote:Original post by snk_kid
Making data members with protected access is most often "the sin of satan" and very contradictive, generally a sign of bad design unless there is real rationale for there use and there truely is such a tight coupling between parent & child types, in most case there isn't so don't fall into bad habbits.
Care to explain in more detail? I don't understand why derived classes shouldn't have access to their base class's member variables.
Because it's an implementation detail that doesn't need to be known, it should be pretty obvious why its called an abstraction, i even high-lighted the main terms coupling and also to enforce state invariants, polymorphic types and type inheritance in general is mainly about extending behaviours/operations not extending implementation, i'm not saying never, i'm say in most cases its abused without any rationale it may aswell be public. I'm not gonna go into any more details, just google it and you will get millions of hits about it.
In theory public virtual member functions considered are bad aswell but that is beyond the scope of this if you want to know more then google up on non-virtual interface idiom.
Quote:Original post by Roboguy
generally, it's a sign of bad design. Usually, the variables should be accessed through the members the base class provides.
Quote:
Also, you can access the base class's private fields (Note: this is a sign of even worse design) if you make it a friend, like this(it's been a while since I've used friends, so this may be a little off):
*** Source Snippet Removed ***
Remember: You should probably never use this, as it's usually bad design, just FYI.