0

I'm fairly nooby at C++ but have this question that I hope makes sense:

Can a method in class B call a method within class A under these conditions:

  • B cannot inherit from A
  • you cannot create an object of A within B
  • the method in A cannot be static

For example, can this be done by passing an object of A by reference to the method call of B.

My next question would be, what if an object of B was created WITHIN an object of A. How does A reference itself to pass to the method of object B?

An example of what I'm imagining:

class A;

class B
{
    int x = 10;
    int y = 10;
public:
    DoThis(A* obj);
};

B::DoThis(A* obj)
{
    obj->DoThat(int x, int y);
}
class A
{
public:
    DoSomething();
    DoThat(int x, int y);
};

A::DoSomething()
{
    B objB;
    objB.DoThis(this);
}

A::DoThat(int x, int y)
{
   std::cout << x << y;
}
int main()
{
    A* objA = new A;
    objA->DoSomething();

}
Andrew Cina
  • 134
  • 10
  • Regarding `A` passing instance of itself to `B`: https://stackoverflow.com/questions/16492736/what-is-the-this-pointer – orhtej2 Sep 18 '19 at 21:01
  • 3
    Regarding the main question: you sort of provided the code that does what you wanted, what is the problem here? – orhtej2 Sep 18 '19 at 21:02
  • @orhtej2 For some reason when I write the actual code out it doesn't work. If there isn't anything obvious that I'm missing in the above then I might be making the error elsewhere, but your "sort of" makes me a unsure :) – Andrew Cina Sep 18 '19 at 21:05
  • What definition of "use" are you ... using? – Kaz Sep 18 '19 at 21:11
  • Well you didn't specify any data type for `B::x` and `B::y` in your class definition. And you should forward declare `class A;` before the definition of `B`. – Timo Sep 18 '19 at 21:13
  • @Kaz To be speceific, **call** method in A but use variables initialized in B. – Andrew Cina Sep 18 '19 at 21:13
  • @Timo It was sort of pseudo code to explain the concept. But the types could be float for example. Fixing now. – Andrew Cina Sep 18 '19 at 21:15
  • Also, the declaration of `A::DoThat` is different from the definition. – Timo Sep 18 '19 at 21:16
  • @AndrewCina before posting, you should run your code to make it most bug free – Amadeus Sep 18 '19 at 21:17

1 Answers1

1

The only problem is that your code contains numerous rudimentary errors, like lack of type specifiers in declarations, lack of terminating semicolons, wrong order (using a class A before it has been declared), mismatches between calls and declarations and such.

Here is a patch to fix the problems. I also added a , (comma space) between the two numbers being output, and a terminating newline.

--- dothis-ORIG.cc  2019-09-18 14:13:15.002235916 -0700
+++ dothis.cc   2019-09-18 14:16:56.548099037 -0700
@@ -1,32 +1,36 @@
+#include <iostream>
+
+class A;
+
 class B
 {
-    x = 10;
-    y = 10;
+    int x = 10;
+    int y = 10;
 public:
-    DoThis(A* obj);
-}
-
-B::DoThis(A* obj)
-{
-    obj->DoThat(x, y)
-}
+    void DoThis(A* obj);
+};

 class A
 {
 public:
-    DoSomething();
-    DoThat();
+    void DoSomething();
+    void DoThat(int x, int y);
+};
+
+void B::DoThis(A* obj)
+{
+    obj->DoThat(x, y);
 }

-A::DoSomething()
+void A::DoSomething()
 {
     B objB;
     objB.DoThis(this);
 }

-A::DoThat(x, y)
+void A::DoThat(int x, int y)
 {
-   std::cout << x << y;
+   std::cout << x << ", " << y << std::endl;
 }

 int main()
Kaz
  • 55,781
  • 9
  • 100
  • 149
  • It seems I'm getting some circular references in my headers then. I've searched around and tried to fix the issue, but I'm having no luck. – Andrew Cina Sep 18 '19 at 21:38