473,394 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

initializing non-static consts

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
4 2811

"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

"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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
4
by: hrmadhu | last post by:
Hi, I wish to declare a vector of deque of int, which I do as follows. #include<vector> #include<deque> #include<iostream> using namespace std; int main(int argc, char* argv) {
13
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
2
by: Andrew Ward | last post by:
The following program compiles and runs fine on my compiler (vc7.1): #include <memory> using namespace std; class X {}; auto_ptr<X> foo() {
17
by: Calle Pettersson | last post by:
Coming from writing mostly in Java, I have trouble understanding how to declare a member without initializing it, and do that later... In Java, I would write something like public static void...
7
by: nk | last post by:
Hi, I'm a newbie on this language. I would be very happy if you help me about the following issue: The code below, reads some names(strings), stores them, and stores the addresses in the pointer...
34
by: newsposter0123 | last post by:
The code block below initialized a r/w variable (usually .bss) to the value of pi. One, of many, problem is any linked compilation unit may change the global variable. Adjusting // rodata const...
13
by: John | last post by:
Is this a valid C++ program that will not crash on any machine? #include <iostream> using namespace std; int main( void ) { int i; cin >i; double X; X = 1123;
10
by: Jason Doucette | last post by:
Situation: I have a simple struct that, say, holds a color (R, G, and B). I created my own constructors to ease its creation. As a result, I lose the default constructor. I dislike this, but...
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.