-3

I am in my classA.cpp

In class A there is a method:

doSomething ()

Ok, now, I'm in my main.cpp and I'm creating an object from class A and i can use the method of this class and it works.

A a1;
a1.doSomething ()

Now I am in my ClassB.cpp And here I wish I could create a method like this:

orderA ()
{
   a1.doSomething ()
}

But of course, I can not do it because ClassB does not know the objects. I told myself that when creating ClassB I could pass it the reference of the object (a1). But I do not know how. I can not understand how to define the type of the object(a1) in classB.h etc... I am a beginner as you can see. Can someone explain to me?

Finally, in my main.cpp I wish I could create a ClassB object and do:

B b1;
b1.orderA;

Thanks for your help :)

SergeyA
  • 61,605
  • 5
  • 78
  • 137
R3dp1ll
  • 21
  • 2

4 Answers4

2

You pass a reference by .... well by passing a reference. Here is a toy example:

struct A {
    void doSomething() {}
};

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

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

PS: suggested readings: here.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
2

If you are asking about syntax of the reference that's

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

and your main.cpp

A a1;
B b1;
b1.orderA(a1);
Dmitrii
  • 66
  • 3
0

the example with class:

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;
}
Aneury Perez
  • 82
  • 2
  • 5
  • 1
    Addendum: References cannot be reseated, so storing a reference as a member makes the object impossible to assign. – user4581301 Sep 27 '18 at 19:18
0

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();
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43