简体   繁体   English

从构造函数内部-有条件地调用成员变量的构造函数重载

[英]From within a constructor - conditionally call constructor overloads for member variable

Background: 背景:

What I would like to do is something similar to the following (the code does not work): 我想做的事情类似于以下内容(代码不起作用):

class A {
public:
  A(TypeX a)
  {
    /* initialize using TypeX */
  }
  A(TypeY a)
  {
    /* initialize using TypeY */
  }
};


class B {
private:
  A a;
  static const TypeX x;
  static const TypeY y;

public:
  B(bool useTypeX)
    : a(useTypeX ? x : y)
  {}
};

TypeX B::x;
TypeY B::y;

This would be my desired code (if the language supported it), but the ternary operator in the initializer-list construction of B::a does not perform the overload resolution for the two disparate types at compile-time (see: https://stackoverflow.com/a/8535301/1689844 ). 这将是我想要的代码(如果语言支持),但是B :: a的初始化列表结构中的三元运算符在编译时不会对两种不同的类型执行重载解析(请参阅: https:/ /stackoverflow.com/a/8535301/1689844 )。

Please note, I only have control over the design of the B class. 请注意,我只能控制B类的设计。 Class A, TypeX and TypeY are disparate types and out of my control (also, TypeX and TypeY do not share a common base class nor is TypeX able to be converted into TypeY or vice-versa). A类,TypeX和TypeY是完全不同的类型,并且不受我的控制(而且,TypeX和TypeY不共享公共基类,TypeX也不能转换成TypeY,反之亦然)。 Also, class A is expensive to initialize (so copy constructors are not feasible). 而且,类A的初始化成本很高(因此,复制构造函数不可行)。

Question: 题:

I would like to know if anyone knows of a more elegant solution to achieve my desired behavior than the following implementation of class B. Answers that utilize C++11 features are welcome, but I am confined to using C++03 and therefore I will only accept an answer that utilizes C++03 features: 我想知道是否有人比下面的类B实现更理想的解决方案来实现我的期望行为。欢迎使用C ++ 11功能的答案,但是我仅限于使用C ++ 03,因此我将仅接受利用C ++ 03功能的答案:

class B {
private:
  SomeSmartPointer<A> a;
  static const TypeX x;
  static const TypeY y;

public:
  B(bool useTypeX)
  {
    // SomeSmartPointer is a smart pointer
    //    which uses reference-counting to
    //    delete allocated memory when the
    //    reference count is 0 and it is
    //    copy-constructable
    if (useTypeX)
    {
      SomeSmartPointer<A> tmp(x);
      a = tmp;
    }
    else
    {
      SomeSmartPointer<A> tmp(y);
      a = tmp;
    }
  }
};

TypeX B::x;
TypeY B::y;

If A is copy constructable (or move constructable, but that's not relevant since you're not using C++11), then you can do 如果A是可复制构造的(或可移动构造的,但由于您不使用C ++ 11而无关紧要),则可以执行

class B {
private:
  A a;
  static const TypeX x;
  static const TypeY y;
  static A initA(bool useTypeX)
  {
    if (useTypeX)
       return A(x);
    else
       return A(y);
  }
public:
  B(bool useTypeX)
    : a(initA(useTypeX))
  {}
};

Edit: Of course, thinking about it, this is just as legal, eliminating the need for an extra function. 编辑:当然,考虑一下,这是合法的,不需要额外的功能。 The key is that you're explicitly creating them. 关键是要明确创建它们。 See https://ideone.com/yUIF0Z 参见https://ideone.com/yUIF0Z

B(bool useTypeX) : a( useTypeX ? A(x) : A(y) ) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM