This question already has an answer here:
Initializing members with members
(1 answer)
Closed 8 years ago.
DoubleVector::DoubleVector(unsigned int buffer) : len(buffer), data(new base_int[len]), start(len / 2), end(start)
This produces a very large values for both start and end even though the buffer is set to 50. len contains the correct value of 50 but start and end both contain some value over a million. I then changed to code to the following.
DoubleVector::DoubleVector(unsigned int buffer) : len(buffer), data(new base_int[len]), start(buffer / 2), end(start)
Now both start and end were initialized with the correct values of 25. Why? Are you not supposed to assume there is any order in which the variables will be initialized?
The order of base member initialisation is the order that the member variables appear in the class definition.
It's best not to rely on that. (As to do so makes code very brittle).
Related
This question already has answers here:
Initializing a member array in constructor initializer
(8 answers)
How do I initialize a member array with an initializer_list?
(8 answers)
How can i use member initialization list to initialize an array?
(2 answers)
Closed 6 months ago.
so I am having an issue with defining and declaring my constructor, normally this isnt a problem but this time I the private data is 2 sets of pre filled arrays. And I am not sure of the syntax for doing this as it doesnt seem to follow the normal way for say.
Below is the relevant parts of the code.
class Frequency_Values{
public:
Frequency_Values(unsigned short LSB_LUT_[], unsigned short MSB_LUT_[]);
Frequency_Values(const Frequency_Values& F);
Frequency_Values& operator = (const Frequency_Values& F);
~Frequency_Values(){};
private:
unsigned short MSB_LUT[410] = {1,2,3}; //not the same data, its cut down cause the actual array is large and would make it messy here
unsigned short LSB_LUT[410] = {1,2,3};
//------------------------Constructor definition-----------------------------------------
Frequency_Values::Frequency_Values(unsigned short LSB_LUT_[],unsigned short MSB_LUT_[]):LSB_LUT[](LSB_LUT_[]),MSB_LUT[](MSB_LUT_[]){}
//---------------------------------------------------------------------------------------
The way I have it gives the following errors,
Any help with this would be greatly appreciated.
Thanks, Dean.
This question already has answers here:
How do I declare a 2d array in C++ using new?
(29 answers)
Closed 1 year ago.
I am trying to create an 2D array. The number of columns is already defined, it will be a constant equal to 20. On the other side, I want the program will generate a number between 30 and 40, which will be the number of rows. I have already created the function to generate numbers between two values. The problem is that it won't allow me declare the array since one of the number of rows is not a constant value and the program can't figure out the space needed to reserve. How can I do it without using vectors or pointers?
const int COLUMNS = 20; /
int numOfRows = genNumInRange(30, 40), //Gets the random number from the function.
array[numOfRows][COLUMNS]; //
ERROR : Expression must have a constant value.
Any possible way I can do that?
You can malloc as many array[COLUMNS] as you need.
This question already has answers here:
How can I find the number of elements in an array?
(16 answers)
Closed 1 year ago.
I declare an array that can hold 10 parameters:
int a[10] = {2,3};
But I actually have 2 parameters, when I use sizeof() :
int n = sizeof(a) / sizeof(int);
It showed the length of the array is 10, but I want the result is: 2 as I only got 2 parameters.
How can I do that ? Thx.
The actual length of the array is 10, not 2. When you provide fewer elements than the array holds, the rest will be filled with zeros.
If you’d like to size the array based on how many values it’s initialized with, just leave the size out of the declaration: int a[] = {2,3};.
This question already has answers here:
What is the function of an asterisk before a function name?
(4 answers)
Closed 6 years ago.
I am fairly new to C++ and I am trying to decode the piece of code shown below. In particular for the BaseSetAssoc::BlkType* line, I am not sure what the asterisk means in this case. I would appreciate some insight.
BaseSetAssoc::BlkType*
NMRU::accessBlock(Addr addr, bool is_secure, Cycles &lat, int master_id)
{
// Accesses are based on parent class, no need to do anything special
BlkType *blk = BaseSetAssoc::accessBlock(addr, is_secure, lat, master_id);
if (blk != NULL) {
// move this block to head of the MRU list
sets[blk->set].moveToHead(blk);
DPRINTF(CacheRepl, "set %x: moving blk %x (%s) to MRU\n",
blk->set, regenerateBlkAddr(blk->tag, blk->set),
is_secure ? "s" : "ns");
}
return blk;
}
BlkType isn't a member function, it's a type, possibly an enum or struct if not an inner class.
The BaseSetAssoc:: is needed to access such "inner" types (defined within a class, i.e. BaseSetAssoc).
So BaseSetAssoc::BlkType* is just a BaseSetAssoc::BlkType pointer.
It's not "following", it's "preceding". As the comments have said: it means that it is returning a pointer to BaseSetAssoc::BlkType, rather than a whole BaseSetAssoc::BlkType.
What does this mean? It means mostly that the pointer can be NULL, or non-existent. Before using the result of this function, it is almost mandatory that you check if it is NULL first.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
(How) can I count the items in an enum?
Is there a way to get the number of constants in enum?
For example:
enum C{id,value};
And later this would return 2:
//pseudo code
get<C>::value
And also, is it possible to access those constants via [] optor? Like i.e.:
C[0] would return id
Usually, you start at zero and the last member gives the size of the enum excluding it.
enum C { id = 0, value, size };
C::size is the size of the enum. Is it possible to access those constants via subscript? No, it is unfortunately most assuredly not possible. However, in this case, you don't really want an enum- you just want a constant array.
A common idiom used for this is
enum C {
id,
value,
LAST_ENUM_C // or something similar.
};
but that assumes no gaps in the enum values here (i.e. no id = 3, value = 15).