none
non-type template parameters vs. constructor arguments RRS feed

  • Question

  • Can anyone tell me where I should use non-type template parameters and where I should use constructor arguments?

    I'm pretty sure that width and height should be in the constructor b/c they correspond to private class members,
    but windowed and lighted are not part of the class.

    template <Typename real> // real is float or double
    class DX9Renderer
    {
    public:
        // Constructor
        DX9Renderer(int width, int height, bool windowed, bool lighted)
        {
            D3DPRESENT_PARAMETERS d3dpp;    // a struct to hold various device information

            pd3d = Direct3DCreate9(D3D_SDK_VERSION);    // create the D3D interface
           
            ZeroMemory(&d3dpp, sizeof(d3dpp));    //clear out the struct for use
            d3dpp.Windowed = windowed;
       // . . .
           d3ddev->SetRenderState(D3DRS_LIGHTING, lighted);
       // . . .
    Saturday, May 3, 2008 1:56 AM

Answers

  • Hi,

     

    I think non-type template parameters and constructor parameters are different concepts. They differ in following aspects.

     

    Firstly, the non-type template parameters can be accessed by all member functions of the class, but the constructor parameters can only be accessed by the constructor.

     

    Secondly, the non-type template parameters are considers as constant, by the constructor parameters are considered a variable. For example, you can use the non-type template parameter to set length of the class’s array member.

    Code Snippet

    template<int nLen>

    class Test

    {

    private:

              int a[nLen];

    };

     

     

     

    Thirdly, template definitions with different non-type template parameters are considered as different classes, for example

    Code Snippet

    Test<6> a;

    Test<7> b;

     

     

     

    "a” and “b” are of different types.

     

    Fourthly, values of the non-type template parameters must be fixed during compile time and you cannot declare a non-type template parameter as a floating point, class, or void type. But there are no such restrictions on constructor parameters.

     

    As stated in the second point, to define a fixed-size array is the typical application of non-type template parameters. Under other situations, I suggest you use function parameters.

     

    Hope this helps!

     

    Monday, May 5, 2008 8:49 AM

All replies

  • Hi,

     

    I think non-type template parameters and constructor parameters are different concepts. They differ in following aspects.

     

    Firstly, the non-type template parameters can be accessed by all member functions of the class, but the constructor parameters can only be accessed by the constructor.

     

    Secondly, the non-type template parameters are considers as constant, by the constructor parameters are considered a variable. For example, you can use the non-type template parameter to set length of the class’s array member.

    Code Snippet

    template<int nLen>

    class Test

    {

    private:

              int a[nLen];

    };

     

     

     

    Thirdly, template definitions with different non-type template parameters are considered as different classes, for example

    Code Snippet

    Test<6> a;

    Test<7> b;

     

     

     

    "a” and “b” are of different types.

     

    Fourthly, values of the non-type template parameters must be fixed during compile time and you cannot declare a non-type template parameter as a floating point, class, or void type. But there are no such restrictions on constructor parameters.

     

    As stated in the second point, to define a fixed-size array is the typical application of non-type template parameters. Under other situations, I suggest you use function parameters.

     

    Hope this helps!

     

    Monday, May 5, 2008 8:49 AM
  • That answers my question; thanks.
    Monday, May 5, 2008 5:12 PM