initializing non-static consts

  • Thread starter trying_to_learn
  • Start date
T

trying_to_learn

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*));
}
 
J

John Harrison

trying_to_learn said:
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
 
B

Branimir Maksimovic

trying_to_learn said:
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.
 
K

Karthik Kumar

trying_to_learn said:
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top