Named Design Idiom and Inheritance

I have several classes with several named constructors . When I inherit them, how do I inherit constructors? The problem is that they return objects of the base class, not the child class.

Side question: can I use C ++ 0x "using" to reduce the number of templates?

+3
source share
4 answers
struct Foo {
    template<typename T> static T one () { return T(1); }
};

struct A { int x; A(int i) : x(i) {}};
struct B : A { B(int i) : A(i) {}};

What allows you to do things like

A a = Foo::one<A> (); 
B b = Foo::one<B> ();
+3
source

Neither do you inherit "classic" constructors, nor do you inherit "named" constructors. You must create specific constructors for each derived class.

, :

class SpiralPoint: public Point{
  private: SpiralPoint(float t, float r)
     :Point(Point::polar(r*t, t))  { };
};
+1

ctor - , . , ctors static. virtual. , static virtual .

: ++ 0x "using", ?

using ctors. , , . , ++ 0x?

+1

() !

template <class T>
class Color {

 public:

  static T Red   () { return T (0); }
  static T Green () { return T (1); }
  static T Blue  () { return T (2); }

 protected:

  explicit Color (int raw) : raw (raw) {
  }

 private:

  int raw;

};

class MoreColor : public Color <MoreColor> {

 public:

  static MoreColor Octarina() { return MoreColor(8); }

 private:

  friend class Color <MoreColor>;
  explicit MoreColor (int raw) : Color <MoreColor> (raw) {}
};

void Test() {
  MoreColor o = MoreColor::Octarina();
  MoreColor r = MoreColor::Red();
}

: D

0

Source: https://habr.com/ru/post/1721232/


All Articles