English 中文(简体)
在模板专业化中添加单一构造方法
原标题:Add a single constructor method in template specialization

我有以下几类:

template <typename T>
class Fixed2DContainer {

    T* ptr;

public:
    const int total_cols;
    const int total_rows;

    Fixed2DContainer(int cols, int rows);
    T& operator()(int col_n, int row_n);
    ~Fixed2DContainer();

private : //disallow copy
    Fixed2DContainer& operator=(const Fixed2DContainer&);
    Fixed2DContainer operator()(const Fixed2DContainer&);
};

Now I d like to specialize this template for some class so that the only change is that I can have an another constructor. Basically I want to be able to do:

Fixed2DContainer<Image>("filename.jpg");

is there an elegant way to do this? I am fairly new to template so i have no idea of the difficulty

最佳回答

我在先前评论中所说的话的快速例子......

template <typename T>
class Fixed2DContainerBase {

  T* ptr;

public:
  const int total_cols;
  const int total_rows;

  Fixed2DContainerBase(int cols, int rows);
  T& operator()(int col_n, int row_n);
  ~Fixed2DContainerBase();

private : //disallow copy
  Fixed2DContainerBase& operator=(const Fixed2DContainerBase&);
  Fixed2DContainerBase(const Fixed2DContainerBase&);
};

// primary template
template <typename T>
class Fixed2DContainer : public Fixed2DContainerBase<T> {

  Fixed2DContainer(int cols, int rows);
  ~Fixed2DContainer();
};

// explicit specialization
template <>
class Fixed2DContainer<Image> : public Fixed2DContainerBase<Image> {

  Fixed2DContainer(int cols, int rows);
  Fixed2DContainer(const std::string&);
  ~Fixed2DContainer();
};

N.B. 由于基类是不可计量的,因此衍生的班级也将如此。 如果所有清理工作都能由主线主人进行,则可能没有必要在衍生课程中确定一位主子。

问题回答

如果您的编辑支持的话,C++11有主导建筑设计器<>m>,在你希望做以下工作时,会得到很多帮助:

template <typename T>
class Base {};              // has all the implementation

template <typename T>
class Template {
   using Base::Base;
   //Template() = delete;   // You might need this if you don t want
                            // the default constructor
};

template <>
class Template<int> {
   using Base::Base;
   Template( std::string const & x ) {}
   //Template() = delete;   // You might need this if you don t want
                            // the default constructor
};

我有同样的问题。 不幸的是,每个建筑商都必须在通用模板中存在。 你们可以避免有人在经营时使用错误的构造;

template <typename T>
class Fixed2DContainer {

    // omitted

    Fixed2DContainer(string fileName)
    {
        // Do not allow construction of Fixed2DContainer with
        // string argument in general case
        ASSERT(false);
        // or
        throw new ExceptionType("Not implemented");
    }

    // omitted
};

template<>
class Fixed2DContainer<Image> {

    // omitted

    Fixed2DContainer(string fileName)
    {
        // Actual construction code
    }

    // omitted
};

优先选择阿瑟,因为你的法典将打破主张,而例外情形中,它会中断捕获量,使其稍微难以减少。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?