Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!hplabs!hp-pcd!hplsla!jima From: jima@hplsla.HP.COM (Jim Adcock) Newsgroups: comp.lang.c++ Subject: Re: Class initialization Message-ID: <6590254@hplsla.HP.COM> Date: 18 Sep 89 18:15:08 GMT References: Organization: HP Lake Stevens, WA Lines: 72 // re first question: // Unfortunately, your reasonable request still does not seem to be supported // by C++ compilers. You should be able to meet your needs by declaring // a constant static class member, and defining an initial value for it. // But present compilers will not allow you to give it an initial value until // after the class declaration, and will not accept a class declaration with // an array size whose bounds are not known at class declaration time. #include typedef char* cstring; // What today's C++ compilers should be willing to accept, but won't: // // class cstringstack; // // static const int cstringstack::maxStackSize = 100; ?!?!?!? // // class cstringstack // { // static const int maxStackSize; ?!?!?!? // cstring element[maxStackSize]; // int top; // public: // cstringstack(): top(0) {} // void push(const cstring s){element[top++] = s;} // cstring pop(){return element[--top];} // cstringstack& popprint(){printf("%s\n",this->pop()); return *this;} // }; // Or maybe they should accept: // // class cstringstack // { // static const int maxStackSize; // cstring element[maxStackSize]; ?!?!?!? // int top; // public: // cstringstack(): top(0) {} // void push(const cstring s){element[top++] = s;} // cstring pop(){return element[--top];} // cstringstack& popprint(){printf("%s\n",this->pop()); return *this;} // }; // // static const int cstringstack::maxStackSize = 100; ?!?!?!? // the best you can do with today's compilers: static const int cstringstack__maxStackSize = 100; class cstringstack { cstring element[cstringstack__maxStackSize]; int top; public: cstringstack(): top(0) {} void push(const cstring s){element[top++] = s;} cstring pop(){return element[--top];} cstringstack& popprint(){printf("%s\n",this->pop()); return *this;} }; void main() { cstringstack stk; stk.push("hi mom"); stk.push("hello dad"); stk.push("bye world"); stk.popprint().popprint().popprint(); }