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. Avoiding friends
Qt 6.11 is out! See what's new in the release blog

Avoiding friends

Scheduled Pinned Locked Moved C++ Gurus
1 Posts 1 Posters 867 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.
  • G Offline
    G Offline
    GrahamL
    wrote on last edited by
    #1

    Hi
    I have a class for which I want to restrict access to its data.
    The restriction is that only 2 other classes should be able to access the data.
    In an ideal world I would like to only allow these 2 classes to create objects of the original class.
    I have manged to restrict the data access by using friends but was wondering if there is a way to do this avoiding the use of friends

    My code so far:
    @
    class C1
    {
    public:
    C1(int x, int y)
    : m_x(x),m_y(y)
    {

    }
    friend class C2;
    friend class C3;
    private:
    int m_x;
    int m_y;
    };

    class C2
    {
    public:
    C2()
    : m_C1(new C1(1,2))
    {

    }
    void setData(int x,int y)
    {
    m_C1->m_x = x;
    m_C1->m_y = y;
    }
    private:
    C1* m_C1;
    };
    class C3
    {
    public:
    C3()
    : m_C1(new C1(1,2))
    {

    }
    void setData(int x,int y)
    {
    m_C1->m_x = x;
    m_C1->m_y = y;
    }
    private:
    C1* m_C1;
    };
    @

    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