Another impossible question
-
Lets say I have a class with quite a few public members, each time one of these functions is called I need to check a variable (by calling another function) before I can allow the code to proceed. Obviously I could insert the function call into each public function, but is there an alternative? What I'm worried about is somebody subclassing and forgetting to add the call into their extended function.
-
Lets say I have a class with quite a few public members, each time one of these functions is called I need to check a variable (by calling another function) before I can allow the code to proceed. Obviously I could insert the function call into each public function, but is there an alternative? What I'm worried about is somebody subclassing and forgetting to add the call into their extended function.
-
Private functions
A cynic is a man who, when he smells flowers, looks around for a coffin.
-H.L. Menckenhey thats a great idea, I will declare all of the class functions as private, that will stop those hackers from subclassing my class. One small drawback though, it won't work.
-
Lets say I have a class with quite a few public members, each time one of these functions is called I need to check a variable (by calling another function) before I can allow the code to proceed. Obviously I could insert the function call into each public function, but is there an alternative? What I'm worried about is somebody subclassing and forgetting to add the call into their extended function.
-
Lets say I have a class with quite a few public members, each time one of these functions is called I need to check a variable (by calling another function) before I can allow the code to proceed. Obviously I could insert the function call into each public function, but is there an alternative? What I'm worried about is somebody subclassing and forgetting to add the call into their extended function.
Are these public members variables or functions? If these are functions, you can use something like the Pimpl-Idiom to hide the implementation a bit, so that if they subclass it, they won't have access to the "meat" of the object. For example:
// in the header file class _MyClass; class MyClass { public: MyClass(); ~MyClass(); // declare as virutal if you want this to be able to be derived from void someFunc(); // whatever other functions you want private: _MyClass* m_pImpl; }; // in the implementation class _MyClass { public: _MyClass() { } void someFunc() { // do whatever } }; MyClass::MyClass() : m_pImpl(new _MyClass) { } MyClass::~MyClass() { delete m_pImpl; } void MyClass::someFunc() { m_pImpl->someFunc(); }
That way, deriving from MyClass would allow them to call the MyClass public/protected methods, but have no access to the actual data and can't circumvent (at least not easily) any requirements you are trying to place on the class.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
hey thats a great idea, I will declare all of the class functions as private, that will stop those hackers from subclassing my class. One small drawback though, it won't work.
waldermort wrote:
hey thats a great idea, I will declare all of the class functions as private, that will stop those hackers from subclassing my class. One small drawback though, it won't work.
JWood's answer was too short. He probably meant something like:
class Base {
public:
void doSomething() { // public non-virtual
checkVar();
doSomethingImp();
}
private:
virtual void doSomethingImp() {
// ...
}
};class Derived : public Base {
public:private:
virtual void doSomethingImp() {
// overrides Base::doSomethingImp()
}
}-- modified at 17:43 Thursday 7th September, 2006 see also: http://www.gotw.ca/publications/mill18.htm[^]
-
Are these public members variables or functions? If these are functions, you can use something like the Pimpl-Idiom to hide the implementation a bit, so that if they subclass it, they won't have access to the "meat" of the object. For example:
// in the header file class _MyClass; class MyClass { public: MyClass(); ~MyClass(); // declare as virutal if you want this to be able to be derived from void someFunc(); // whatever other functions you want private: _MyClass* m_pImpl; }; // in the implementation class _MyClass { public: _MyClass() { } void someFunc() { // do whatever } }; MyClass::MyClass() : m_pImpl(new _MyClass) { } MyClass::~MyClass() { delete m_pImpl; } void MyClass::someFunc() { m_pImpl->someFunc(); }
That way, deriving from MyClass would allow them to call the MyClass public/protected methods, but have no access to the actual data and can't circumvent (at least not easily) any requirements you are trying to place on the class.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
That's an excellent idea, I must remember this method. But in my current instance, I have simply added the function call to each function. If anybody does decide to override the methods, they are just going to have to read the docs.