I am curious how to go about intializing a member variable if it is an array, and the size isn't given until it compilation. Doing so would better encapsulate the code which is the point of thise whole practice.
Here's a simple code example...
What I am interested in is having Stack's constructor take in a "size" arguement and pass it into m_stck. This way, there's one less define constant in the program. And of course, I can set each Stack object to different max sizes which would be a nice benefit. Some may just need 20, some may need 100. But just figuring out how to do this in general would be great to know down the road. Something like this is what I'm looking for...Code:#ifndef STACK_H #define STACK_H #include <iostream> #define SIZE 100 class Stack { private: int m_stck[SIZE]; int m_tos; public: Stack(); void Push(int i); int Pop(); }; #endif
Please note I'm using Stack as an example. I can see many classes that might require a similar task. Mainly what I'm trying to understand is how to pass a variable into a member array within an object. If I didn't, SIZE would always be the same value!Code:Stack::Stack(int size) { // pass size into m_size[]'s brackets. }



LinkBack URL
About LinkBacks



