0

I have an inherited class InhClass which is derived from a Base Class (abstract class). In my Inherited class i have to override a function of Base Class which returns a vector of some devicetype class objs. In that function i am using an external library function which is returning a vector of different class type.

Inherited Class
std::vector<devicetype> basefunction(){
externalclassfunction();    // returns std::vector<std::unique_ptr<BluetoothDevice>>                 
}

Is there any way to convert the vector of external library class unique pointer (BluetoothDevice) to the vector of class of devicetype class?

  • Can you shouw us th edefinition of `Inherited` and the base class? And some information about the external library too – Ivan Mar 01 '21 at 08:52
  • In theory you can cast vetors, but `BluetoothDevice` and `devicetype` must be compatible for that operation – Ivan Mar 01 '21 at 08:54
  • Not possible with a simple cast, you always need an underlined copy-semantic: Possible duplicate: https://stackoverflow.com/questions/18223036/i-want-a-vector-of-derived-class-pointers-as-base-class-pointers – Secundi Mar 01 '21 at 10:04
  • What is the relationship between `devicetype` and `BlueToothDevice`? Are they both classes, and one is inherited from the other? Is `devicetype` an enumerated type with particular possible values representing a bluetooth device? Or something else? – Peter Mar 01 '21 at 10:14
  • @Ivan Base Class Function: virtual std::vector getConnectableDevices() = 0; Inherited Class Function: std::vector getConnectableDevices(){ externallibraryfunction(); //returns std::vector> } – Arsalan Raghib Mar 01 '21 at 10:18
  • Consider using template instead of inheritance. – frachop Mar 01 '21 at 22:45
  • @frachop Virtual functions are not possible to define with templates. Is there any other thing you can suggest? – Arsalan Raghib Mar 02 '21 at 09:54
  • @Arsalan Raghib : see [Standard practice virtual interface classes vs templates](https://stackoverflow.com/questions/1165963/c-standard-practice-virtual-interface-classes-vs-templates) or [templates vs inheritance](http://groups.di.unipi.it/~nids/docs/templates_vs_inheritance.html) – frachop Mar 02 '21 at 10:20

0 Answers0