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
Post a Comment