Top.Mail.Ru
The C++ Community's Journal -- Day
? ?
The C++ Community's Journal -- Day [entries|friends|calendar]
The C++ Community

[ userinfo | livejournal userinfo ]
[ calendar | livejournal calendar ]

Exposing some class methods while hiding others [27 May 2009|08:27pm]
class UsefulClass  {
	public:
	usefulReadOnlyMethod(); // run me plz
	undesirableMethod();  // hide from coders 
};

class HasANotIsA {
	public:
	safeWrapper(){ foo->undesirableMethod(); } 

	protected: 
	UsefulClass foo; 
}

I want code using HasANotIsA to be able to run foo->usefulReadOnlyMethod() but not foo->undesirableMethod(). I can put the usefulClass in protected to hide the undesirable method, but that also hides the useful method. I can't take the UsefulClass out of protected and make it public without exposing the undesirable method.

Is there an alternative to wrapping every useful function inside HasANotIsA? That feels like a waste of time and would clutter up the class definition.

It seems that I want to change which methods of UsefulClass are public and which are private, which suggests I should extend the class, add a few wrappers where things change, and put the wrapperMethod in the extended class. I could then make the neutered class public in HasANotIsA. However, it is likely that future code could reach undesirableMethod() by implicitly casting it back to a UsefulClass during a general-purpose routine that has the most basic class as a parameter. Can this be avoided or discouraged?

Read more...Collapse )
4 comments|post comment

navigation
[ viewing | May 27th, 2009 ]
[ go | previous day|next day ]
Image