C++ access private member in composition of two classes from base class

你说的曾经没有我的故事 提交于 2019-12-12 02:58:30

问题


Since I'm a newbie in C++, here it goes!

I have a base class (I'm not using inheritance anywhere) with two objects from two other classes. I need to have access from a private member to the other in another class.

class base
{
private:
    myClass1 m1;
    myClass2 m2;
public:
    base() {};
    ~base() {};
};

class myClass1
{
private:
    int m_member1;
public:
    myClass1() {};

    ~myClass1() {};
};

class myClass2
{
private:
    int m_member2;
public:
    myClass2() {};

    ~myClass2() {};

    int sum_members_because_yes(void)
    {
        return (myClass1::m_member1 + m_member2); //HOW CAN I DO THIS???
    }
};

How can I have access of m_member1 from myClass1 in myClass2? :)

(I want to avoid inheritance, because on my code the myClass1 and 2 is not a base class...)

Thanks


回答1:


There are many ways to do it.

To allow access to m_member1 at all, you could make m_member1 public. Or you could declare something a friend of myClass1, like this:

class myClass1
{
private:
  int m_member1;
  ...
  friend class base;
};

or this:

class myClass1
{
private:
  int m_member1;
  ...
  friend class myClass2;
};

or this:

class myClass1
{
private:
  int m_member1;
  ...
  friend class int myClass2::sum_members_because_yes(void);
};

Or you could give myClass1 a public accessor:

class myClass1
{
  ...
public:
  ...
  int get_m_member1() const
  {
    return(m_member_1);
  }
};

Then to allow m2 to reach m1, you could give m2 a pointer to m1:

class myClass2
{
  ...
public:
  myClass1 *pm1;
};

class base
{
  ...
public:
  base()
  {
    m2.pm1 = &m1;
  };
};

or you could relay the value of m_member1 through the base upon the request of m2, but this answer is getting long.

(And once you're comfortable with all of this, you should take note that sum_members_because_yes really belongs in base.)




回答2:


I am not that clear what you want to achieve. I think it is to be able to have two classes holding data inside another class and have a function that returns some combination of the two classes data. I would do this in the following way:

#include <iostream>
using namespace std;

class B {
private:
    int mMember;
public:
    B(int aNumber) : mMember(aNumber) {}
    ~B() {}
    int getNumber() { return mMember; }
};

class C {
private:
    int mMember;
public:
    C(int aNumber) : mMember(aNumber) {}
    ~C() {}
    int getNumber() { return mMember; }
};

class A {
private:
    B mB;
    C mC;

public:
    A(B b, C c) : mB(b), mC(c) {}
    ~A() {}
    int sumMembers() {
        return mB.getNumber() + mC.getNumber();
    }
};

int main() {
    B b(2);
    C c(2);
    A a(b, c);

    cout << a.sumMembers() << endl; // prints 4

}

Here we have two classes (B and C) which contain data and then class A which has member of type B and C. The function that returns the sum should be a method of class A because it holds instances of B and C. The private data of the data classes is accessed via a getter, this allows access to the number but not the ability to change it's value. In my example the B and C classes are the same. If the two objects are two instances of the same 'thing' then they should be two instances of the same class, not different classes. I hope I have answered the correct question.



来源:https://stackoverflow.com/questions/31230928/c-access-private-member-in-composition-of-two-classes-from-base-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!