What does this construction means? [duplicate] - c++

This question already has answers here:
What does a colon in a struct declaration mean, such as :1, :7, :16, or :32?
(3 answers)
Closed 9 years ago.
class Test
{
struct
{
unsigned has_some_value1: 1;
unsigned has_some_value2: 1;
} info;
};
What does unsigned has_some_value1: 1; means?
Should be the following statement true: sizoef(type) == bit1 + ... + bitn ?

These are called "bit fields". has_some_value1 occupies one bit. has_some_value2 also occupies one bit—maybe the next physical bit in memory, or maybe not (depends how your compiler is configured to handle bit field alignment).

A bitfield in a nonstatic instance of an un-named struct called "info", which is itself a member of "Test".

Related

How to declare/define constructor with private data that is of a pre filled array [duplicate]

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.

Is it legal to access class members as an array? [duplicate]

This question already has answers here:
Can I treat a struct like an array?
(8 answers)
Is it legal to index into a struct?
(10 answers)
Closed 2 years ago.
Consider this code:
struct Point {
double x;
double y;
double z;
void setByIndex(int i, double value) {
(&x)[i] = value;
}
};
The setByIndex() function works and does what I want. But can I be sure this is not a UB (when called with a proper i)? Could you please provide references to the standard with the explanation?

Meaning of asterisk following a member function [duplicate]

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.

: operator in c/c++ [duplicate]

This question already has an answer here:
What is the meaning of colon (:) operator in "uint isWidget : 1;" in Qt? [duplicate]
(1 answer)
Closed 9 years ago.
can anyone tell me, what is happening in this code. I tried to search a lot of places but couldn't understand what exactly is the commented part of the code doing.
#include<stdio.h>
struct XYZ {
//int a:6; this one.
char s;
}structure;
int main() {
printf("%lu",sizeof(structure));
return 0;
}
I am getting the output as 4.
That line is commented out. It doesn't do anything.
If it wasn't commented out, it would mean that the size of int a is limited to 6 bits only. It's useful for bit-fields inside of a structure.

sizeof(blank class) == 1. why? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C++: What is the size of an object of an empty class?
#include <iostream>
class C
{
};
int main()
{
std::cout << sizeof(C) << std::endl;
return 0;
}
output:
1
Why 1, but not zero ?
From Stroustrup's mouth sizeof. To ensure that the addresses of two different objects will be different. For the same reason, "new" always returns pointers to distinct objects.
Because the C++ standard requires all objects to have a nonzero size. This helps ensure that every object has a unique address.
The c++ standard says that every class/struct must have at least 1 byte.