tags:

views:

61

answers:

1

I have the following situation:

In project A, an object (say Obj1 of class A1) instantiates Obj2 of class A2.

Then, from Obj1, by many code paths, an object Obj3 (of class A3) can be instantiated. The A3 class is in another project.

The stack trace from Obj1's main method to instantiating an object of class A3 is 20 calls deep, and there are at least 100 places in the code where the function that instantiates the A3 class is called.

Now I want to add a non-static method to class A2 (say test()), and be able to call Obj2.test() (not A2::test()) from all instances of class A3.

How could I go about this in the most reasonable way?

Edit: some code:

 class A2
    {
      //...
      A2(string,string,...);
      double test();
    };

    A2::A2(string,string,...)
    {
      //...
    }

    double A2::test()
    {
      //return 4.2; //in reality it's more complicated
    }

    class A1{

    //...

      A2* Obj2;
    };

    A1::A1()
    {
      Obj2 = new A2(string,string,...);
    }

    A1:run()
    {
      //here there are many possible code paths (switches instantiating objects of other classes)
      //but all end up as: 
      A3* Obj3 = new A3();
      Obj3->test_wrapper();
    }

    class A3{
      //this is defined in another project, and I'd prefer that project doesn't reference A2 since A2  links against a couple of libraries...

      static double A3::test_wrapper();
    };

    static double A3::test_wrapper()
    {
      //if I had Obj2 here, I'd do:
      return Obj2.test();
      //but I don't... 
    }
+1  A: 

There's not enough information here to answer.

  1. Does A1 have an A2 or A3 member? Does A2 or A3 have an A1 member?
  2. What are the arguments to the instantiation of A2 or A3?

If A3 wants to call Obj2.test(), then either

  1. A3 needs a member of type A2
  2. A3 needs a member of type X that has a member of type A2 -- probably should forward the call through X, though
  3. The method in A3 that needs to call test needs to take an A2 argument
  4. The method can take an argument X that has an A2 member (again, forward through X).

It's probably not a good idea to start using some global thing to stuff an A2 into and get back from A3.

If there is only one method of A3 that needs to call A2, then can A2 be a member of that method? Do all callers have an A2 (or are they able to get one?).

Can the functionality of A2's test() go somewhere else?

Lou Franco
1. A1 has a member of class A2 (Obj2); neither A2 or A3 have an A1 member; 2. A2 instantiates with 3 strings; A3 instantiates with no argument
adrian8400
what method of A3 needs to call Obj2.test()? The constructor? Some other? Do the callers have an A2 object? Are you looking for a way to get around refactoring to pass around the A2 to appropriate place? If you don't want A3 to reference A2, do it through an interface.
Lou Franco
the functionality of test() cannot be moved, it depends on the entire A2 class. I need a single instance of A2 only.
adrian8400
@Lou Franco: A static method of A3 should call Obj2.test(). The immediate callers (that instantiate A3) don't have A2 (obj2), and I would need to edit a hundred functions or so to pass it as parameter
adrian8400
What parameters does the static method take? How would it know which A2 to call test() on?
Lou Franco
Does A3's static method just want to call A2 test for some test that's on the stack? If you are looking for a trick to get around passing it, and it's always on the stack, then look into Thread Local Storage.
Lou Franco
the static method takes no parameters. It should call Obj2.test() (the only instance of A2 is Obj2). I am looking for a way to avoid the refactoring (and passing A2) down 20 or so function calls until A3 gets instantiated. I'm afraid you lost me at your last comment, but you got me curious about Thread Local Storage. I could make Obj2 static but I'm not sure about it's lifetime. A1(Obj1 object) handles that now. Obj1 itself is not static, and cannot be (too much refactoring). Maybe just make Obj2 a global A2(through an interface, though, since I don't want A3 to reference A2)?
adrian8400
Thread local storage is kind of like a global except you can have one instance per thread. It's OS dependent, but just google it with your OS to find out the API. If you are single threaded, I guess you can just use a global. I would only suggest this if you absolutely cannot change the existing code.
Lou Franco