C++ 如何从另一个类的成员函数访问数据成员和成员函数?

C++ 如何从另一个类的成员函数访问数据成员和成员函数?,c++,C++,如果我写一个类- class A { int x; void show() { cout<<a; } }; int main() { A a; a.show(); a.x; } A类 { int x; 无效显示() { 可以用和你的主菜一样的方法 class B{ ... void foo(){ A a; a.show(); } } 有趣的阅读。首先,你的例子不正确 class A {

如果我写一个类-

class A
{
    int x;
    void show()
    {
       cout<<a;
    }
};

int main()
{
   A a;
   a.show();
   a.x;
}
A类
{
int x;
无效显示()
{

可以用和你的主菜一样的方法

   class B{
...
  void foo(){
    A a;
    a.show();
  }
}

有趣的阅读。

首先,你的例子不正确

class A
{
    int x; // x is private
    void show() //show is private also
    {
       cout<<a;
    }
};

int main()
{
   A a;
   a.show(); //you can't access private members from outside
   a.x;
}

您的代码甚至无法编译[
cout@Prasoon-同意,尝试编辑,但必须至少更改6个字符。Qoute a带有“a”作为字符串。他可能打算在其中添加x。
class A
{
    int x; // x is private
    void show() //show is private also
    {
       cout<<a;
    }
};

int main()
{
   A a;
   a.show(); //you can't access private members from outside
   a.x;
}
class A {
public:
// ...
stativ void do_stuff() {}
};

class B {
//....
void do complicated stuff() {/*...*/ A::do_stuff();}
};