我心里有这样的想法:
// This is called at multiple locations and each handles the returned object
// differently. Handling also differs depending on the returned object type.
void GetObject() {
// Check runtime condition
// Object1 and Object2 are completely dissimilar and do not share a base class.
if (condition1) {
// Operations to prepare for construction of Object1
throw Object1{ var1, var2, var3 };
}
if (condition2) {
// Operations to prepare for construction of Object2
throw Object2{ var4, var5 }; …Run Code Online (Sandbox Code Playgroud)