Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [solved] Access class member from another class only
Forum Updated to NodeBB v4.3 + New Features

[solved] Access class member from another class only

Scheduled Pinned Locked Moved C++ Gurus
7 Posts 2 Posters 3.0k 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.
  • T3STYT Offline
    T3STYT Offline
    T3STY
    wrote on last edited by
    #1

    I am writing two classes: MyClassA, MyClassB, none derived from the other, but MyClassB stores and uses objects of type MyClassA. I would like to expose both classes to the user, but I would like some methods of the MyClassA to only be used from MyClassB, but not from anywhere outside MyClassB. In code:
    @class MyClassA{
    public:
    void FunctionA();
    void FunctionB();

    /* public? private? anything else ?*/
    void FunctionC(); // this method shall only be used from MyClassB!!!!
    };

    class MyClassB{
    private:
    MyClassA A_object;

    public:
    void MyOtherFunc(){
    // I want to be able to call
    A_object.FunctionC();
    }
    };

    int main(){
    // but I don't want to be able to call
    MyClassA object; // FunctionC from anywhere outside MyClassB
    object.FunctionC();
    }@

    When you don't want to expose a class memeber to the user you usually put it under the private or protected access types. But in my case private wouldn't let me use FunctionC from MyClassB(), while public would allow FunctionC to be used from any object outside MyClassB.

    So how do I deal with this? From searches I understood the keyword friend might be a solution but I don't get how should I use it.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      friend keyword will be your best friend :)

      For example
      @

      class ClassA
      {
      friend class ClassB;
      ....

      private:
      void func1();
      };
      @

      @
      class ClassB
      {
      ....

      public:
      void aFunc() {member.func1();}
      private:
      ClassA member;
      };
      @

      Bu the question is: "Why do you expose MyClassB to user if you don't want he uses it directly? "

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • T3STYT Offline
        T3STYT Offline
        T3STY
        wrote on last edited by
        #3

        I have some back-end functions in MyClassA that I sometimes need to call from MyClassB as well, but I don't want to expose these functions to the user. Also, MyClassB would be using those functions as back-end functions as well, so they're not going to be exposed to the user from MyClassB; still, MyClassB will expose some other public functions to the user, so that's why I want to expose MyClassB.

        Anyway, thank you for your help! I now understood what this friend was all about... in my tries I was using it the opposite (friending MyClassA from MyClassB...).

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          Could I suggest to create a new MyClassC accessible only to MyClassA and MyClassB?

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • T3STYT Offline
            T3STYT Offline
            T3STY
            wrote on last edited by
            #5

            Not quite since the back-end functions use objects inside the MyClassA and MyClassB classes; MyClassC would end up in a lot of unneeded data exchange functions to pass objects from a class to another... I thought about this solution too but I realized it's not as nice as it seems.
            I'm redefining the object model right now to see if I can make it work properly without the friend approach. Chances though are that I'll end up having many parameters in functions...

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mcosta
              wrote on last edited by
              #6

              Hi,

              it's ok.
              Remeber however that using friend keyword you break "Object Oriented Encapsulation":http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)

              So, use carefully and think about different way to obtain what yuo want

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              1 Reply Last reply
              0
              • T3STYT Offline
                T3STYT Offline
                T3STY
                wrote on last edited by
                #7

                thanks mcosta, I read about friend before and I already know it's out of the OOP concept. Now I figured out possible different way and I'm working on it :-)

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved