【问题标题】:Method of an object that launches a method from another object [closed]从另一个对象启动方法的对象的方法[关闭]
【发布时间】:2018-09-27 18:59:27
【问题描述】:

我在我的 classA.cpp

在A类中有一个方法:

doSomething ()

好的,现在,我在我的 ma​​in.cpp 中,我正在从类 A 创建一个对象,我可以使用这个类的方法并且它可以工作。

A a1;
a1.doSomething ()

现在我在我的 ClassB.cpp 中 在这里我希望我可以创建一个这样的方法:

orderA ()
{
   a1.doSomething ()
}

当然,我不能这样做,因为 ClassB 不知道对象。我告诉自己,在创建 ClassB 时,我可以将对象 (a1) 的引用传递给它。但是我不知道怎么做。我无法理解如何在 classB.h 等中定义对象(a1)的类型...如您所见,我是初学者。谁能给我解释一下?

最后,我希望在我的 main.cpp 中创建一个 ClassB 对象并执行以下操作:

B b1;
b1.orderA;

感谢您的帮助:)

【问题讨论】:

  • 看来你需要一本好教科书,所以不能替代。

标签: c++ class reference


【解决方案1】:

您通过....传递引用。通过传递引用。这是一个玩具示例:

struct A {
    void doSomething() {}
};

struct B {
    void doSomethingWithA(A& a) {
        a.doSomething();
    }
};

int main() {
   A a;
   a.doSomething();
   B b;
   b.doSomethingWithA(a);
}

PS:建议阅读:here.

【讨论】:

    【解决方案2】:

    如果您询问的是参考的语法

    orderA (A& a1)
    {
       a1.doSomething ();
    }
    

    还有你的 main.cpp

    A a1;
    B b1;
    b1.orderA(a1);
    

    【讨论】:

      【解决方案3】:

      类的例子:

      class A
      {
         public:
             void doSomething(){}
      };
      
      class B{
          A &ref;
        public:
           B(A& r):ref{r} /// c++03 or before ref(r)
           {
           }
          void orderA ()
           {
               ref.doSomething();
           }
      };
      
      
      int main()
      {
         A a1;
         B b(a1);
         b.orderA();
         ...
         return 0;
      }
      

      【讨论】:

      • 附录:引用不能被重新定位,因此将引用存储为成员会使对象无法分配。
      【解决方案4】:

      ClassA.h

      #ifndef CLASS_A_INCLUDED  // include-guard to prevent
      #define CLASS_A_INCLUDED  // multiple inclusion of the header
      
      class A {
      public:
          void doSomething();
      };
      
      #endif /* CLASS_A_INCLUDED */
      

      ClassA.cpp

      #include "ClassA.h"
      
      void A::doSomething()
      {}
      

      ClassB.h

      #ifndef CLASS_B_INCLUDED
      #define CLASS_B_INCLUDED
      
      #include "ClassA.h"  // incude the declaration of ClassA
                           // so that members of ClassA can be used.
      
      class B {
      private:
          A &a1;  // declare the member a1 as
                  // reference to an object of type ClassA
      public:
          B(A &a);  // add a constructor that takes a reference
                    // to an object of type ClassA
          void orderA();
      };
      #endif /* CLASS_B_INCLUDED */
      

      ClassB.cpp

      #include "ClassB.h"
      
      B::B(A &a)
      : a1{ a }  // use the initializer list of the constructor
                 // to initialize the reference a1
      {}
      
      void B::orderA()
      {
          a1.doSomething;  // call a method on the object referenced by a1
      }
      

      main.cpp

      #include "ClassA.h"
      #include "ClassB.h"
      
      int main()
      {
          A a1;
          a1.doSomething();
      
          B b1{ a1 };  // pass the object a1 to the constructor of B
          b1.orderA();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-04
        • 2013-05-23
        • 1970-01-01
        • 2022-12-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多