samedi 7 mars 2015

Can you force classes inheriting from abstract base class to only have the public methods defined in base case?

Is it possible to have an abstract class but FORCE implementing classes to have only the public methods that are in the abstract class?


I don't care how the private methods work, but I want to force the classes to have only one public method.


For example, say I have the following abstract class:



class MyObjectTransform
{
public:
virtual ~MyObjectTransform()
{
}

virtual MyObject transform(MyObject input) = 0;
};


Then I want to force all objects inheriting from MyObjectTransform to ONLY have a single (other than the constructor) public method, transform. I don't care what private methods the inheriting classes have. Is this possible?


Update: The goal here is to force the developer to only expose functionality through a single method. For example, consider this situation:



class ATransform
{
private:
MyObject A_Stuff(MyObject input);

public:
override MyObject transform(MyObject input)
{
return this->A_stuff(input);
}
};

class BTransform
{
public:
MyObject B_Stuff(MyObject input);

override MyObject transform(MyObject input)
{
return this->B_stuff(input);
}
};


The problem here is that the dev can call B_Stuffdirectly. I want to prevent this.


Aucun commentaire:

Enregistrer un commentaire