static const foo = 1 works ?!
-
I have the following code :
class Test
{
public:
static const foo = 1;
static const bar = 2;
};int _tmain(int argc, _TCHAR* argv[])
{
cout << Test::foo << endl << Test::bar << endl;return 0;
}
- and absolutely no idea why it is working. I left the type declaration by accident and realized later that it shouldn't work - at least in my book. But it does. Can anyone explain that to me ? Is there some kind of default data-type for such situations ? If so, what is it ? wbr Brainley
-
I have the following code :
class Test
{
public:
static const foo = 1;
static const bar = 2;
};int _tmain(int argc, _TCHAR* argv[])
{
cout << Test::foo << endl << Test::bar << endl;return 0;
}
- and absolutely no idea why it is working. I left the type declaration by accident and realized later that it shouldn't work - at least in my book. But it does. Can anyone explain that to me ? Is there some kind of default data-type for such situations ? If so, what is it ? wbr Brainley
If you mean why does this get treated like a const int, that is because that is exactly what you created when you typed
const foo
. I believe thatint
is treated as the default type in a scenaro like this, which is why you sometimes see code likeunsigned uiABC = 0
to create anunsigned **int**
variable. Peace!-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFiles -
I have the following code :
class Test
{
public:
static const foo = 1;
static const bar = 2;
};int _tmain(int argc, _TCHAR* argv[])
{
cout << Test::foo << endl << Test::bar << endl;return 0;
}
- and absolutely no idea why it is working. I left the type declaration by accident and realized later that it shouldn't work - at least in my book. But it does. Can anyone explain that to me ? Is there some kind of default data-type for such situations ? If so, what is it ? wbr Brainley
Mr.Brainley wrote:
and realized later that it shouldn't work
Why shouldn't it work? It's static, meaning all instances share the same varialble, and it's const meaning it will never change. So it's perfectly safe to declare it as you have done.
-
If you mean why does this get treated like a const int, that is because that is exactly what you created when you typed
const foo
. I believe thatint
is treated as the default type in a scenaro like this, which is why you sometimes see code likeunsigned uiABC = 0
to create anunsigned **int**
variable. Peace!-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFilesI wonder if it would be treated as a QWORD on a 64-bit machine :confused:
-
If you mean why does this get treated like a const int, that is because that is exactly what you created when you typed
const foo
. I believe thatint
is treated as the default type in a scenaro like this, which is why you sometimes see code likeunsigned uiABC = 0
to create anunsigned **int**
variable. Peace!-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFilesIs that MS-Specific or ISO ? wbr Brainley
-
I wonder if it would be treated as a QWORD on a 64-bit machine :confused:
Not if they keep the width of the
int
type at 32-bits... But I do not have a 64-bit environment handy... Peace!-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFiles -
I have the following code :
class Test
{
public:
static const foo = 1;
static const bar = 2;
};int _tmain(int argc, _TCHAR* argv[])
{
cout << Test::foo << endl << Test::bar << endl;return 0;
}
- and absolutely no idea why it is working. I left the type declaration by accident and realized later that it shouldn't work - at least in my book. But it does. Can anyone explain that to me ? Is there some kind of default data-type for such situations ? If so, what is it ? wbr Brainley
-
Is that MS-Specific or ISO ? wbr Brainley
I think that is standard, or at least assumed. I have seen bits of older code, on older VAXen or U*ix boxes, that has things like:
static SomeVariable = 1;
unsigned SomeOtherValue = 0;Peace!
-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFiles -
Mr.Brainley wrote:
and realized later that it shouldn't work
Why shouldn't it work? It's static, meaning all instances share the same varialble, and it's const meaning it will never change. So it's perfectly safe to declare it as you have done.
-
I wonder if it would be treated as a QWORD on a 64-bit machine :confused:
WalderMort wrote:
I wonder if it would be treated as a QWORD on a 64-bit machin
Probably.
int
data type is environment specific which means that it's 16 bits wide in a compiler for 16-bit systems, e.g. the C-compiler for MicroChip PIC 18[^]. This is the standard, but a complier might not follow the standard.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
-
WalderMort wrote:
I wonder if it would be treated as a QWORD on a 64-bit machin
Probably.
int
data type is environment specific which means that it's 16 bits wide in a compiler for 16-bit systems, e.g. the C-compiler for MicroChip PIC 18[^]. This is the standard, but a complier might not follow the standard.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
but the AMD 64-bit[^] docs specify that an int remains 32-bits in size. So I guess it really depends on how the compiler decides to interpret the code.
-
but the AMD 64-bit[^] docs specify that an int remains 32-bits in size. So I guess it really depends on how the compiler decides to interpret the code.
WalderMort wrote:
but the AMD 64-bit[^] docs specify that an int remains 32-bits in size. So I guess it really depends on how the compiler decides to interpret the code.
Yep, very true.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
-
I have the following code :
class Test
{
public:
static const foo = 1;
static const bar = 2;
};int _tmain(int argc, _TCHAR* argv[])
{
cout << Test::foo << endl << Test::bar << endl;return 0;
}
- and absolutely no idea why it is working. I left the type declaration by accident and realized later that it shouldn't work - at least in my book. But it does. Can anyone explain that to me ? Is there some kind of default data-type for such situations ? If so, what is it ? wbr Brainley
As others have said, the compiler assumes you mean
int
if you don't specify a type. This was a holdover from C and is illegal in newer versions of the C++ spec.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?