Doomulation
?????????????????????????
I was wondering if there's a way to create const data members in either a struct or a class. The thing is that I wish to create a struct or class with members that I will use fwrite to output directly to the disk. I'm trying to copy a specific format here. Some of the data must be the same always, so I'm trying to make them const and initialize them. Like so:
Since posttyp is const, I have no way to initialize it. Static data does not work on arrays, either. Plus it would destroy the purpose of a class to do this. So is there a way to initialize the data while retaining const, or must it be removed for this to work? I suppose I could bypass const with the following:
(Yes, I'm aware that it is actually 3 bytes, but it must not include the NULL char.)
Only problem is that the class fails to compile when using const data members :/
EDIT:
Did some more tests... it appears that if you put any const member in a struct or class, you'll be needing your own constructor for it. And if you do declare one, it'll complain that the const members couldn't be initialized (even if you do not specify anything to initialize them with.)
Code:
class test
{
public:
test();
void test_fnc();
private:
const char posttyp[2];
char datum[8];
char autogiro[8];
char empty1[44];
char mottagare_kundnr[6];
char mottagare_pgnr[10];
char empty2[2];
} _test;
Since posttyp is const, I have no way to initialize it. Static data does not work on arrays, either. Plus it would destroy the purpose of a class to do this. So is there a way to initialize the data while retaining const, or must it be removed for this to work? I suppose I could bypass const with the following:
Code:
memcpy((void*)&posttyp,"01",2);
Only problem is that the class fails to compile when using const data members :/
EDIT:
Did some more tests... it appears that if you put any const member in a struct or class, you'll be needing your own constructor for it. And if you do declare one, it'll complain that the const members couldn't be initialized (even if you do not specify anything to initialize them with.)
Last edited: