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::St ringStack() : index(0) {
memset(stack, 0, size * sizeof(string*) );
} 4 2838
"trying_to_lear n" <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
"trying_to_lear n" <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::St ringStack() : 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(siz e),index(0)
{
}
template <typename T>
void Stack<T>::push( const T* s)
{
assert(index<st ack.size());
stack[index++]=s;
}
template <typename T>
const T* Stack<T>::pop()
{
assert(index>0) ;
return stack[--index];
}
Greetings, Bane.
"Branimir Maksimovic" <bm***@eunet.yu > wrote in message
news:cm******** **@news.eunet.y u... int index;
.... assert(index<st ack.size());
another note: int is not good type for index, too :)
Greetings, Bane.
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::St ringStack() : index(0) { memset(stack, 0, size * sizeof(string*) ); }
--
Karthik. http://akktech.blogspot.com .
'Remove _nospamplz from the address to get the real one.' This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
def foo(self, data):
self.attr1=data
self.attr2.append(data)
The initialization of attr1 is obviously OK, all instances of Father
redefine it in the method foo. But the initialization of attr2 is wrong
|
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)
{
|
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
|
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()
{
|
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 main(String args) {
MyType aMember;
...
aMember = new MyType(...)
...
}
However, in C++ this does not seem to work. I declare in class (it's
| |
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 array, and writes them out. But it fails and
exits the program. I guess that it's about initializing the array but i
couldn't find a way to make it ok....
|
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 long double const_pi=0.0;
lines to
// rodata
|
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;
|
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 it's easy to solve: I
just make my own default constructor.
Problem:
My own default constructor is considered to be *initializing the
variable* (even though it doesn't), whereas the original one does
not. Thus, when I declare and use it before...
|
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 initialization takes place."
Does this mean all non-local objects will be zero-initialized before
they are initialized by their initializers(if they have)? For example:
int g_var = 3;
int main() {}
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |