0

I have been playing with the Interactive Brokers API in C++ and have run into troubles surrounding virtual functions and inheritance. In the very limited docs and misleading examples in the api download they provide, it states that you must have a Client class that inherits two base classes EWrapper and EClientSocket.

EWrapper has a list of over 100 virtual functions, and provides all the communication to other classes and their methods used to get marketdata, make trades, etc. When I try inheriting these 2 classes into a Client Class I keep getting errors saying, essentially, I am missing a function(s) in my derived class that is/are virtual functions in the EWrapper base class and/or the EClientSocket base class. So my question is this, must I declare all 100 of the virtual functions in the EWrapper base class and all virtual functions in the EClientSocket Class in my derived Client Class with their proper types/parameters in my Client Class Header file? And then specify the Client Class's version of them outside the class in another Client.cpp file per usual?

if a base class has a bunch of virtual functions, in any class you make that inherits that base class, do you have to declare all the virtual functions in your derived class for it even to compile?

I have been coding a lot in python lately and need a brush up on my virtual functions/C++ Inheritance. Have not fully switched gears in my head yet. Thank you.

  • You need to implement every virtual function that's abstract (`= 0`) in the base. – lorro Oct 28 '22 at 14:05
  • So in my header file in my Client Class just declare public all those virtual functions that are abstract in the base, and I also need to implement a version of them in my Client.cpp? I am implementing all functions in Client Class outside of the class. I do not have to implement functions that are not abstract despite the class being abstract? Just those that are equal to 0? – Brendan Lydon Oct 28 '22 at 14:13
  • @BrendanLydon Public/protecte/private and abstract are entirely unrelated concepts. You should not redeclare a protected or private function as public; it is not meant for public use. (And yes, you only need to implement the "pure virtual" functions. I would recommend that you get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list).) – molbdnilo Oct 28 '22 at 14:17
  • @molbdnilo I am aware of that. All the pure virtual functions I saw in the example are actually private in my class. The example just seems to be outdated as it does not have all the pure virtual functions in the base implemented. I have my C++ for dummies opened right next to me. As I am a dummie. This link clarified for anyone else having troubles with pure virtual functions: https://www.geeksforgeeks.org/pure-virtual-functions-and-abstract-classes/#:~:text=1)%20A%20class%20is%20abstract,pure%20virtual%20function%20show(). – Brendan Lydon Oct 28 '22 at 14:20
  • The [docs](https://interactivebrokers.github.io/tws-api/client_wrapper.html#ewrapper_impl) appear to say you should _not_ derive from `EClientSocket` (because that's what they provided you to send outgoing requests). Are you sure you're reading the documentation and/or code correctly? – Useless Oct 28 '22 at 16:02
  • @Useless They say that in the docs, and then I've seen examples where there is a TestClientCpp Class inheriting both EWrapper and EClientSocket. So, I am not sure what is what. – Brendan Lydon Oct 28 '22 at 16:20
  • @Useless I have not delved too deeply into it yet, but my main question was about pure virtual functions. – Brendan Lydon Oct 28 '22 at 16:28
  • FWIW, it's generally easier to answer a question when it's just one question (and distilling your confusion into a single question often clarifies things anyway). You can see from my answer how many questions you actually asked. – Useless Oct 28 '22 at 21:02

1 Answers1

0

I was expecting to find an existing duplicate for this, but here goes:

if a base class has a bunch of virtual functions, in any class you make that inherits that base class, do you have to declare all the virtual functions in your derived class for it even to compile?

For what to even compile? You need to be more specific.

If a class has pure virtual functions, it is called an abstract class and cannot be instantiated.

A pure virtual function is a non-static method declared like

virtual ReturnType method(args) = 0;

Any class derived even indirectly from an abstract class, is itself abstract, unless every inherited pure virtual function has been overridden. They don't all have to be overridden in the same class. I'm going to refer to these non-abstract derived classes as concrete.

It's good practise to make sure you got the signature correct by using the override keyword in your derived class:

ReturnType method(args) override;

You can certainly compile a program that has abstract classes defined in it. You just can't instantiate those classes. You can instantiate concrete derived classes, and you may be able to get references to abstract base classes from a library that keeps the concrete subclass secret.


So my question is this, must I declare all 100 of the virtual functions in the EWrapper base class and all virtual functions in the EClientSocket Class in my derived Client Class with their proper types/parameters in my Client Class Header file? And then specify the Client Class's version of them outside the class in another Client.cpp file per usual?

If we ignore the fact that you don't need to implement EClientSocket in the first place because it isn't abstract and the documentation explicitly tells you not to, the answers are:

  1. You can't instantiate an EWrapper subclass until all the pure virtual functions are overridden

  2. If you don't trust the documentation you have, look at the code. You can easily search for = 0, so do that. You can even copy and paste the function prototypes so long as you change = 0 to override

  3. You can just write a do-nothing implemention inline if you really don't care about handling a particular callback:

     void method(args) override {}
     ReturnType method(args) override {
       return {};
     }
    

    or whatever.

    You may need at least one virtual implemented out-of-line (ie, in the .cpp file) for linker reasons, but hopefully you actually want to handle at least one of the callbacks.

Useless
  • 64,155
  • 6
  • 88
  • 132
  • This helps thank you. I have a book "Algorithmic Trading with Interactive Brokers Python and C++" and in their examples they have EClientSocket being inherited along with EWrapper. It is an older book so it is clearly outdated. – Brendan Lydon Oct 29 '22 at 12:26