0

Here is my situation:

struct A {
    int numberAllChildStructsUse;
}

struct B : A {
    std::string strUniqueToB;
}

struct C : A {
    std::string strUniqueToC;
}

(in some source file)

B b = thisFunctionReturnsBOrCStructsAsTypeA(int idThatGuaranteesItIsTypeBNotC);

Error: no suitable user-defined conversion from A to B exists when I try that above line. What is the best way to get this to work? Struct A represents the commonalities that all Bs, Cs share. The member vars inside of B and C are unique to each other. That function returns A type structs, but I know that they are actually in fact either Bs or Cs, and can tell based off of the parameter I supply.

dmscs
  • 255
  • 2
  • 12

2 Answers2

3

If your function returns by value, slicing possibly happens and you can't get a B.

If your function returns a reference or a pointer, you have 2 options:

  1. If you are absolutely certain that the dynamic type is a pointer/reference to B, you can use static_cast. It has zero overhead, but if you get the type wrong you get UB.

  2. If you are not sure if the dynamic type is a pointer/reference to B, and your type is polymorphic (has a virtual function), then you can use dynamic_cast. It will report the error so you can handle the failure case at the cost of some overhead. In my experience, dynamic_cast is often a sign of poor design, so be careful not to obfuscate stuff.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
2

You can use dynamic_cast for this, but only if the class hierarchy is polymorphic.

To achieve the latter, write

struct A {
    int numberAllChildStructsUse;
    virtual ~A() = default;
};

reference: https://en.cppreference.com/w/cpp/language/dynamic_cast

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    It is worth mentioning that more often than not `dynamic_cast` tends to point to flaws in design. Ideally, when IS-A principle is properly observed, this cast is not required. – SergeyA Aug 03 '21 at 13:23
  • Thank you. Why is it important that its polymorphic? – dmscs Aug 03 '21 at 13:24
  • @dmscs:If it's not polymorphic then `dynamic_cast` doesn't work. If you want a polymorphic type then you probably also want a virtual destructor. The technique I give kills both these birds with one stone. – Bathsheba Aug 03 '21 at 13:25