By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,127 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,127 IT Pros & Developers. It's quick & easy.

initializing non-static consts

P: n/a
I'm learning consts in C++ and the book says that u have to initialize
non-static consts inside the constructor initializer list, however
"const string* stack[size]" isn't initialized in the constructor
initializor list,instead its initialized inside the constructor main
body using memset.I dont understand this,why isnt this uniform

class StringStack {
static const int size = 100;
const string* stack[size];
int index;
public:
StringStack();
void push(const string* s);
const string* pop();
};

StringStack::StringStack() : index(0) {
memset(stack, 0, size * sizeof(string*));
}
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"trying_to_learn" <no****@no.no> wrote in message
news:cm**********@gist.usc.edu...
I'm learning consts in C++ and the book says that u have to initialize
non-static consts inside the constructor initializer list, however "const
string* stack[size]" isn't initialized in the constructor initializor
list,instead its initialized inside the constructor main body using
memset.I dont understand this,why isnt this uniform

class StringStack {
static const int size = 100;
const string* stack[size];
int index;
public:
StringStack();
void push(const string* s);
const string* pop();
};


Simple, what you have isn't const. You have a non-constant array of pointers
to constant strings. Its the strings that are constant not the array.

A constant array would look like this

const string* const stack[size];

See the second const?

john
Jul 22 '05 #2

P: n/a

"trying_to_learn" <no****@no.no> wrote in message
news:cm**********@gist.usc.edu...
I'm learning consts in C++ and the book says that u have to initialize
non-static consts inside the constructor initializer list, however "const
string* stack[size]" isn't initialized in the constructor initializor
list,instead its initialized inside the constructor main body using
memset.I dont understand this,why isnt this uniform
'Cause arrays don't have constructors.
If you don't like this solution you can easilly replace array with
vector.

class StringStack {
static const int size = 100;
const string* stack[size];
Note: this isn't array of const pointers, rather
array of non const pointers to const strings.
int index;
public:
StringStack();
void push(const string* s);
const string* pop();
};

StringStack::StringStack() : index(0) {
memset(stack, 0, size * sizeof(string*));
note: this won't work if NULL pointers don't have
such memory pattern
}


template <typename T>
class Stack{
vector<const T*> stack;
int index;
public:
Stack(int size = 100);
void push(const T* s);
const T* pop();
};

template <typename T>
Stack<T>::Stack(int size):stack(size),index(0)
{
}
template <typename T>
void Stack<T>::push(const T* s)
{
assert(index<stack.size());
stack[index++]=s;
}
template <typename T>
const T* Stack<T>::pop()
{
assert(index>0);
return stack[--index];
}

Greetings, Bane.
Jul 22 '05 #3

P: n/a

"Branimir Maksimovic" <bm***@eunet.yu> wrote in message
news:cm**********@news.eunet.yu...

int index; .... assert(index<stack.size());


another note: int is not good type for index, too :)

Greetings, Bane.
Jul 22 '05 #4

P: n/a
trying_to_learn wrote:
I'm learning consts in C++ and the book says that u have to initialize
non-static consts inside the constructor initializer list, however
"const string* stack[size]" isn't initialized in the constructor
initializor list,instead its initialized inside the constructor main
body using memset.I dont understand this,why isnt this uniform

class StringStack {
Also as a coding convention have static const members as *all caps* .
And when you talk about array indices, please use size_t instead of int.
That fits better. (defined in <cstddef> )
static const int size = 100;
const string* stack[size];
static const size_t SIZE = 100;
const string* stack[SIZE];

int index;
size_t index // Hence
public:
StringStack();
void push(const string* s);
const string* pop();
};

StringStack::StringStack() : index(0) {
memset(stack, 0, size * sizeof(string*));
}

--
Karthik. http://akktech.blogspot.com .
'Remove _nospamplz from the address to get the real one.'
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.