How to protect a member function
-
Hi to all, i write a class (A) that works with an object from another class (B); class B needs to call a method from class A, but i would like this function from class A it's not visible when i use class A for my projects. Example: i write a numeric edit that use spin button; i write a spin button class derived from CSpinButtonCtrl and an edit class derived from a CEdit; edit have a function MyFunction() called from spin button class. So, when i use numeric edit in my projects, i would like MyFunction() (and also all class spin button) is not visible (protected). What is the correct way to do that, if it's possible ? Thanks
-
Hi to all, i write a class (A) that works with an object from another class (B); class B needs to call a method from class A, but i would like this function from class A it's not visible when i use class A for my projects. Example: i write a numeric edit that use spin button; i write a spin button class derived from CSpinButtonCtrl and an edit class derived from a CEdit; edit have a function MyFunction() called from spin button class. So, when i use numeric edit in my projects, i would like MyFunction() (and also all class spin button) is not visible (protected). What is the correct way to do that, if it's possible ? Thanks
-
Hi to all, i write a class (A) that works with an object from another class (B); class B needs to call a method from class A, but i would like this function from class A it's not visible when i use class A for my projects. Example: i write a numeric edit that use spin button; i write a spin button class derived from CSpinButtonCtrl and an edit class derived from a CEdit; edit have a function MyFunction() called from spin button class. So, when i use numeric edit in my projects, i would like MyFunction() (and also all class spin button) is not visible (protected). What is the correct way to do that, if it's possible ? Thanks
make MyFunction
private
, and make A afriend
of B. friend classes [^] -
make MyFunction
private
, and make A afriend
of B. friend classes [^] -
Hi to all, i write a class (A) that works with an object from another class (B); class B needs to call a method from class A, but i would like this function from class A it's not visible when i use class A for my projects. Example: i write a numeric edit that use spin button; i write a spin button class derived from CSpinButtonCtrl and an edit class derived from a CEdit; edit have a function MyFunction() called from spin button class. So, when i use numeric edit in my projects, i would like MyFunction() (and also all class spin button) is not visible (protected). What is the correct way to do that, if it's possible ? Thanks
An alternative to using "friend" classes would be to use messages and message handlers. In MS Windows, messages are what windows already use to communicate with one another. You can easily add new messages and handlers.
-
Hi to all, i write a class (A) that works with an object from another class (B); class B needs to call a method from class A, but i would like this function from class A it's not visible when i use class A for my projects. Example: i write a numeric edit that use spin button; i write a spin button class derived from CSpinButtonCtrl and an edit class derived from a CEdit; edit have a function MyFunction() called from spin button class. So, when i use numeric edit in my projects, i would like MyFunction() (and also all class spin button) is not visible (protected). What is the correct way to do that, if it's possible ? Thanks
(Bit late to the party... still think this is worth saying and it might help you in future) If you ever find yourself wanting to use a class but there's aspects of it's interface that you don't want to work with then don't try and modify the class you want to use - that way lies madness and hackiness of the first degree. If the class with the relatively fat interface really needs a big interface somewhere else you don't want to break that. So what can you do about it? Wheel out the software engineer's swiss army knife - another level of indirection! So if you've got a class:
class fat_and_dangerous
{
public:
void safe();
void dangerous();
};and don't want clients to use
dangerous()
implement another class using it:class skinny_and_safe
{
public:
skinny_and_safe() : fat_( new fat_and_dangerous ) {}void safe() { fat\_->safe(); } private: std::unique\_ptr fat\_;
}
and in most of your client code only use
skinny_and_safe
. Just out of interest... don't actually implement skinny_and_safe inline. It leaks too many implementation details to the client. Don't worry about performance - most compilers (actually linkers working with compilers) these days will elide the indirect call - until a profiler tells you that it's slow. -
(Bit late to the party... still think this is worth saying and it might help you in future) If you ever find yourself wanting to use a class but there's aspects of it's interface that you don't want to work with then don't try and modify the class you want to use - that way lies madness and hackiness of the first degree. If the class with the relatively fat interface really needs a big interface somewhere else you don't want to break that. So what can you do about it? Wheel out the software engineer's swiss army knife - another level of indirection! So if you've got a class:
class fat_and_dangerous
{
public:
void safe();
void dangerous();
};and don't want clients to use
dangerous()
implement another class using it:class skinny_and_safe
{
public:
skinny_and_safe() : fat_( new fat_and_dangerous ) {}void safe() { fat\_->safe(); } private: std::unique\_ptr fat\_;
}
and in most of your client code only use
skinny_and_safe
. Just out of interest... don't actually implement skinny_and_safe inline. It leaks too many implementation details to the client. Don't worry about performance - most compilers (actually linkers working with compilers) these days will elide the indirect call - until a profiler tells you that it's slow. -
Thanks very much to all: it's works and it's what i need. For Albert Holguin and Aescleal: i think with "friends" it's more simple; thanks likewise.