0

I'm trying to extend an existing application using an agile library. The library forces me to create a concrete interface class, InterfaceToA to interface to my application. However, within my application, the most natural place (without a major refactor) to instantiate this object is within another class's, say ClassA, implementation. I really need the interface class to have access to all of ClassA data and member functions as well (inherit from). So something like:

// Class declarations
class ClassA {
  public:
     double thing1;
     double thing2;
     f( double arg1, double arg2 );
     ff()
};

class InterfaceToA : public ClassA {
  public:
     g( double arg1, double arg2 );
};

// Class implementations
double ClassA::f( double arg1, double arg2 ){
    InterfaceToA interface;
    return interface.g(arg1,arg2);
}

double ClassA::ff(){
    return 1;
}

double InterfaceToA::g( double arg1, double arg2 ){
    return (arg1 + thing1) - (arg2 + thing2) + ff();
}

//Main function
int main(){
    ClassA myClassA;

    myClassA.thing1 = 1;
    myClassA.thing2 = 3;

    double output;

    output = myClassA.f(5,1);

    std::cout << str(output) << std::endl;

    return 0;
}

where in this case the expected output would be 3, (5+1) - (1+3) + 1 = 3. Is this possible in C++, I've been trying to think about this using both inheritance and nested classes, but I can quite figure out how to proceed.

Nate Kohl
  • 35,264
  • 10
  • 43
  • 55
johntfoster
  • 361
  • 5
  • 17
  • And the question is? I totally don't understand where you have a problem, maybe http://ideone.com/ZXils ? – Arpegius Jul 17 '12 at 22:16
  • It's not clear what your question is. Normally an interface like `InterfaceA` would be something that `ClassA` implements. Alternatively, the interface could contain an instance of `ClassA` as a member, and then use that instance in methods like `InterfaceToA::g` to compute results. – Nate Kohl Jul 17 '12 at 22:22
  • My question is: is what I need to do and proposed above valid/legal? – johntfoster Jul 18 '12 at 00:23

1 Answers1

0

I was able to figure it out. I needed to pass a reference to ClassA into InterfaceToA::g. The final compilable code is below

#include<iostream>

// Class declarations
class ClassA {
  public:
    double thing1;
    double thing2;
    double f( double arg1, double arg2 );
    double ff();
};

class InterfaceToA : public ClassA {
  public:
    double g( ClassA &myClass, double arg1, double arg2 );
};

// Class implementations
double ClassA::f( double arg1, double arg2 ){
    InterfaceToA interface;
    return interface.g(*this,arg1,arg2);
}

double ClassA::ff(){
   return 1;
}

double InterfaceToA::g( ClassA & myClass, double arg1, double arg2 ){
    return (arg1 + myClass.thing1) - (arg2 + myClass.thing2) + myClass.ff();
}

//Main function
int main(){
    ClassA myClassA;

    myClassA.thing1 = 1;
    myClassA.thing2 = 3;

    double output;

    output = myClassA.f(5,1);

    std::cout << output << std::endl;

    return 0;
}
johntfoster
  • 361
  • 5
  • 17