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. Friendship with method of other class in other File
Forum Updated to NodeBB v4.3 + New Features

Friendship with method of other class in other File

Scheduled Pinned Locked Moved Unsolved C++ Gurus
6 Posts 5 Posters 1.7k Views 2 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.
  • J Offline
    J Offline
    Josz
    wrote on last edited by Josz
    #1

    I have the file configuration below showed; All in independent files.
    I would like to give friendship only to "void myClass::print() method" from "myClass" and not to the full "myClass" object.

    Please, somebody know how to replace "friend myClass"; for "friend void myClass::print()"

    Thanks in advance

    //mysubclass.h
    #ifndef MYSUBCLASS_H_
    #define MYSUBCLASS_H_
    
    class myClass;  //forward declaration is needed
    
    class mySubClass
    {
      int i;
      friend myClass;    //<-- I would like to replace it for any like friend void myClass::print()
    };
    
    #endif
    
    //myclass.h
    #ifndef MYCLASS_H_
    #define MYCLASS_H_
    
    class myClass
    {
       mySubClass msc;
    
    public
       void print();
    };
    
    #endif
    
    //myclass.cpp
    #include "mysubclass.h"
    
    void myClass::print() {
      std::cout << msc.i << std::endl;
    }
    
    JKSHJ jsulmJ 2 Replies Last reply
    0
    • J Josz

      I have the file configuration below showed; All in independent files.
      I would like to give friendship only to "void myClass::print() method" from "myClass" and not to the full "myClass" object.

      Please, somebody know how to replace "friend myClass"; for "friend void myClass::print()"

      Thanks in advance

      //mysubclass.h
      #ifndef MYSUBCLASS_H_
      #define MYSUBCLASS_H_
      
      class myClass;  //forward declaration is needed
      
      class mySubClass
      {
        int i;
        friend myClass;    //<-- I would like to replace it for any like friend void myClass::print()
      };
      
      #endif
      
      //myclass.h
      #ifndef MYCLASS_H_
      #define MYCLASS_H_
      
      class myClass
      {
         mySubClass msc;
      
      public
         void print();
      };
      
      #endif
      
      //myclass.cpp
      #include "mysubclass.h"
      
      void myClass::print() {
        std::cout << msc.i << std::endl;
      }
      
      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @Josz I'm not sure if C++ lets you do that exactly. How about https://msdn.microsoft.com/en-us/library/465sdshe.aspx#Class members as friends ?

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      1
      • J Josz

        I have the file configuration below showed; All in independent files.
        I would like to give friendship only to "void myClass::print() method" from "myClass" and not to the full "myClass" object.

        Please, somebody know how to replace "friend myClass"; for "friend void myClass::print()"

        Thanks in advance

        //mysubclass.h
        #ifndef MYSUBCLASS_H_
        #define MYSUBCLASS_H_
        
        class myClass;  //forward declaration is needed
        
        class mySubClass
        {
          int i;
          friend myClass;    //<-- I would like to replace it for any like friend void myClass::print()
        };
        
        #endif
        
        //myclass.h
        #ifndef MYCLASS_H_
        #define MYCLASS_H_
        
        class myClass
        {
           mySubClass msc;
        
        public
           void print();
        };
        
        #endif
        
        //myclass.cpp
        #include "mysubclass.h"
        
        void myClass::print() {
          std::cout << msc.i << std::endl;
        }
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Josz Is mySubClass derived from myClass?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        J 1 Reply Last reply
        1
        • jsulmJ jsulm

          @Josz Is mySubClass derived from myClass?

          J Offline
          J Offline
          Josz
          wrote on last edited by
          #4

          @jsulm No

          1 Reply Last reply
          0
          • K Offline
            K Offline
            karlheinzreichel
            wrote on last edited by
            #5

            see
            https://www.geeksforgeeks.org/friend-class-function-cpp/

            regards
            karl-heinz

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              @karlheinzreichel solution requires mySubClass to know the full definition of myClass. You can't just pre-declare class myClass; //forward declaration is needed

              An alternative is

              class FriendHelper{
              public:
              static void actualPrint(myClass& sender);
              };
              

              now move the body of void print(); inside the body of actualPrint and change it to void print() {FriendHelper::actualPrint(*this);}

              No you can make FriendHelper a friend of both myClass and mySubClass

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              1

              • Login

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