class

hey guys
i have the code and it is to edit the bits of an integer but i dont know what the
: 1(example: bit0 : 1) do.

thanks
(and if I remove them the code doesnt work)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  
#include <Windows.h>
#include <stdio.h>

#ifndef bit
typedef int bit;
#endif

class _byte;
class _byte4;

class _byte {
public:

	_byte() {};
	~_byte() {};

	bit bit0 : 1;
	bit bit1 : 1;
	bit bit2 : 1;
	bit bit3 : 1;
	bit bit4 : 1;
	bit bit5 : 1;
	bit bit6 : 1;
	bit bit7 : 1;
};

class _byte4 {
public:

	_byte4(_byte4* p) :
		byte0((_byte*)p),
		byte1((_byte*)p + 1),
		byte2((_byte*)p + 2),
		byte3((_byte*)p + 3)
		{};
	~_byte4() {};
	_byte *byte0;
	_byte *byte1;
	_byte *byte2;
	_byte *byte3;
};

int main(void)   {


	int c = 0;
	_byte4 *dd = new _byte4((_byte4*)&c);

	printf("before: %d\n", c);

	dd->byte0->bit0 = 1;

	printf("after: %d\n", c);

	system("pause");
	return 0;
}
http://en.cppreference.com/w/cpp/language/bit_field
make sure that your code does not exhibit undefined behaviour (or that implementation defined is not a problem)


> _byte4 *dd = new _byte4((_byte4*)&c);
http://www.cplusplus.com/forum/general/138037/
This code is for demonstrating the use of pointers in class reference.But, there is no such thing made to print some change in value of "c".

You initialized a variable named "c" as integer with value 0.Then, you created a reference of class "_byte4" in main() function named "dd".

it calls the constructor "_byte4(_byte4* p)" and initializes the four members. these variables of type _byte

therefore, these four members can referenced to public members of class _byte.

dd->byte0->bit0 = 1;

If you remove those members then the upper code line will be a null pointer reference. thus, your code will not run.
Topic archived. No new replies allowed.