c++ - Can I create object in the same class? -


can create object of class in same class ? if not can ?

example stack class

template <class t> class stack {  void push(t d) { ... }  t pop() { ... }  //my question 1 void sort() { // errors when ?  stack st;  st.push(4); }  }; 

without templates fine, e.g.

class stack {   public: //<-- added can use   void push(int d)   {   //...   }    int pop()   {   //...   }    void sort()   {     stack st;     st.push(4);   } }; 

now, in templated case, need to can tell sort function kind of stack make.

  void sort()   {     stack<t> st;     //---^^^     st.push(4);   } 

more edit section 14.6.1

"the injected-class-name can used or without template-argument-list. when used without template-argument-list, equivalent injected-class-name followed template-parameters of class template enclosed in <>."

so, can stack st;
not able have member variable of type stack<t> though.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -