Initialization lists for nested class members.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gsi
    New Member
    • Jul 2007
    • 51

    #1

    Initialization lists for nested class members.

    Hi all,
    I couldnt initialize the nested class members using constructor initialization list's. I am using Intel/Solaris/g++ compiler.

    [code=cpp]

    #include<iostre am>

    template<class T>
    class A{
    private:
    template<class U>
    struct B{
    U a;
    B():a(23){std:: cout<<a; }
    B(U e):a(e){std::co ut<<a;}
    };
    public:
    //B<int> k -----------> works fine and prints out 23.
    B<int> k(23); ----------->This doesn't work.
    };


    int main(){
    A<int> a;
    }

    [/code]

    Errors:

    st.cpp:13: error: expected identifier before numeric constant
    st.cpp:13: error: expected ',' or '...' before numeric constant.




    Thanks in advance,
    gs.
    Last edited by sicarie; Aug 13 '07, 12:56 PM. Reason: Code tags here are [code=cpp] and [/code], but you were close ;)
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    B<int> k just says that struct B sopecialized with int is a prioovate member of A.

    That's OK.

    But this:

    B<int> k(23);

    says to create an object K from B<int> and call the constructor with the int argunment. However, this is a template definition. No object is being created. Hence, you can't make function calls like this.

    Always remember that the compiler makes a copy of the template and plugs in the vaues for the type in the copy. It is the copy that compiles. The remplate itself never compiles.

    The trick here is to write a class using specific types and get that to compile. Then convert the class to a template.

    Comment

    • gsi
      New Member
      • Jul 2007
      • 51

      #3
      Hi,
      Thanks for your reply. But I have some queries.

      pls advice if I am wrong , you mean to say that , by saying B<int >k inside the class definition it involves an implicit function call to the default constructor of the struct specialization which the compiler itself takes care(during the object allocation), But by saying B<int>k(23) the user forces a function call to the overloaded constructor which he is not allowed to do(bfore the allocation).

      Also I couldnt figure out what this mean's "The trick here is to write a class using specific types and get that to compile. Then convert the class to a template".
      I tried to googling for the terms but couldnt make out anything, Pls give me a bit more explanation.

      Thanks in advance,
      gsi.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by gsi
        you mean to say that , by saying B<int >k inside the class definition it involves an implicit function call to the default constructor of the struct specialization which the compiler itself takes care(during the object allocation), But by saying B<int>k(23) the user forces a function call to the overloaded constructor which he is not allowed to do(bfore the allocation).
        Forget about the template part. Here is your code specialized with int:
        [code=cpp]
        class A{
        private:

        struct B{
        int a;
        B():a(23){std:: cout<<a; }
        B(int e):a(e){std::co ut<<a;}
        };
        public:
        //B<int> k -----------> works fine and prints out 23.
        B k(23); //----------->This doesn't work.
        };


        int main(){
        A a;
        }
        [/code]

        It still won't compile. It's the line B k(23) and the compiler is not expecting a constant. That is, you can't say in a class definition what constructor to call. That's because a class is a declaration (allocates no memory) but B k(23) is a definition (k must be created in order to process the 23). You cannot have function calls inside a class definition.

        B<int> k is really:
        [code=cpp]
        struct B{
        int a;
        B():a(23){std:: cout<<a; }
        B(int e):a(e){std::co ut<<a;}
        }k;
        [/code]

        There's no object yet. Just a copy of the template.

        Originally posted by gsi
        Also I couldnt figure out what this mean's "The trick here is to write a class using specific types and get that to compile. Then convert the class to a template".
        Suppose you need a function template to sum the elements of an array. You could just write the template, then specialize it, then debug it. However, it's often easier to write a regular function:

        [code=cpp]
        int Sum(int* arr, int len)
        {
        int total = 0;
        for (int i = 0; i < len; ++i)
        {
        total += arr[i]]
        }
        return total;
        }
        [/code]

        Then debug it. When it works, change the function into a template:
        [code=cpp]
        template<typena me T>
        T Sum(T* arr, int len)
        {
        T total = 0;
        for (int i = 0; i < len; ++i)
        {
        total += arr[i]]
        }
        return total;
        }
        [/code]

        Comment

        • gsi
          New Member
          • Jul 2007
          • 51

          #5
          Hi,
          Thanks a lot for the explanation...

          Yes i do realize now that function call's are illegal from a class definition since class definition is merely a type constructor.

          thanks,
          gsi.

          Comment

          Working...