Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I access the private data member without friend class?

How can I access the private data member without friend class?

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++friend classdesign patterninheritancepimpl idiom
3 Posts 3 Posters 418 Views
  • 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.
  • Y Offline
    Y Offline
    Yash001
    wrote on 7 Aug 2021, 00:15 last edited by
    #1

    How can i access the Private data in another class?

    class  Instruments {
     public:
       void isConnected();
    
    private:
      std::share<InstrumentsPrivate> m_data:   // InstrumentsPrivate contain all private data. 
    }
    
    class  Channel
    {
        void start(std::share<Instruments> deviceHandler ){
    
       // How can I access the deviceHandler->m_data->(member of InstrumentsPrivate).
      }
    }
    
    

    I do have one solution i can make the friend class of instruments. But this public API Header file . I can't add the friend class in public header file. is there any alternative solution?

    Thank you,

    P 1 Reply Last reply 7 Aug 2021, 01:00
    0
    • Y Yash001
      7 Aug 2021, 00:15

      How can i access the Private data in another class?

      class  Instruments {
       public:
         void isConnected();
      
      private:
        std::share<InstrumentsPrivate> m_data:   // InstrumentsPrivate contain all private data. 
      }
      
      class  Channel
      {
          void start(std::share<Instruments> deviceHandler ){
      
         // How can I access the deviceHandler->m_data->(member of InstrumentsPrivate).
        }
      }
      
      

      I do have one solution i can make the friend class of instruments. But this public API Header file . I can't add the friend class in public header file. is there any alternative solution?

      Thank you,

      P Offline
      P Offline
      Pl45m4
      wrote on 7 Aug 2021, 01:00 last edited by
      #2

      @Yash001

      Public getter in instrument class, which will return the data


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      3
      • K Offline
        K Offline
        Kent-Dorfman
        wrote on 7 Aug 2021, 01:49 last edited by
        #3

        You cannot, without modifying the class where the private member is declared. That's why it was made private. Without a get/set method pair it is only visible internally to the class.

        1 Reply Last reply
        2

        3/3

        7 Aug 2021, 01:49

        • Login

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