User-defined conversion operator from base class

前端 未结 11 1576
南旧
南旧 2020-12-01 02:40

Introduction

I am aware that \"user-defined conversions to or from a base class are not allowed\". MSDN gives, as an explanation to this rule, \"You

11条回答
  •  感情败类
    2020-12-01 03:17

    You should use your Solution B (the constructor argument); firstly, here's why not to use the other proposed solutions:

    • Solution A is merely a wrapper for Solution B;
    • Solution C is just wrong (why should a base class know how to convert itself to any subclass?)

    Also, if the Body class were to contain additional properties, what should these be initialized to when you perform your cast? It's far better to use the constructor and initialize the subclass's properties as is convention in OO languages.

提交回复
热议问题