Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to protect a member function

How to protect a member function

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Drugodrf
    wrote on last edited by
    #1

    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

    C J A A 4 Replies Last reply
    0
    • D Drugodrf

      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

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      It seems that you are looking for friend[^]. For your example you can make your CEdit derived class a friend class of the spin control class:

      class CMyEdit : CEdit {
      friend class CMySpinControl;
      protected:
      // This can be called from CMySpinControl.
      void MyFunction();
      };

      1 Reply Last reply
      0
      • D Drugodrf

        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

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        make MyFunction private, and make A a friend of B. friend classes [^]

        image processing toolkits | batch image processing

        D 1 Reply Last reply
        0
        • C Chris Losinger

          make MyFunction private, and make A a friend of B. friend classes [^]

          image processing toolkits | batch image processing

          D Offline
          D Offline
          Drugodrf
          wrote on last edited by
          #4

          As i can, i try. Thanks to all

          1 Reply Last reply
          0
          • D Drugodrf

            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

            A Offline
            A Offline
            Albert Holguin
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • D Drugodrf

              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

              A Offline
              A Offline
              Aescleal
              wrote on last edited by
              #6

              (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.

              D 1 Reply Last reply
              0
              • A Aescleal

                (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.

                D Offline
                D Offline
                Drugodrf
                wrote on last edited by
                #7

                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.

                A 1 Reply Last reply
                0
                • D Drugodrf

                  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.

                  A Offline
                  A Offline
                  Aescleal
                  wrote on last edited by
                  #8

                  Friends will work but you should always be aware of the coupling that introduces into your design.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups