Re: Is this legal?
From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 01/23/04
- Next message: Norbert Riedlin: "Re: [C, C++] "(a=b) = c;" Illegal or Undefined?"
- Previous message: Richard Heathfield: "Re: [C] strcat() question (ongoing)"
- In reply to: jeffc: "Re: Is this legal?"
- Next in thread: Andrey Tarasevich: "Re: Is this legal?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 23 Jan 2004 23:40:57 +1100
"jeffc" <nobody@nowhere.com> wrote in message news:400ff25e_1@news1.prserv.net...
|
| "Chris ( Val )" <chrisval@bigpond.com.au> wrote in message
| news:bulsrc$it892$1@ID-110726.news.uni-berlin.de...
| >
| > "jeffc" <nobody@nowhere.com> wrote in message
| news:400d47d6_2@news1.prserv.net...
| > |
| > | "Chris Val" <chrisval@bigpond.com.au> wrote in message
| > | news:118880b0.0401192048.4f7f7beb@posting.google.com...
| > | > "jeffc" <nobody@nowhere.com> wrote in message
| > | news:<400c4e18_1@news1.prserv.net>...
| > | > > "Joec" <joec@annuna.com> wrote in message
| > | > > news:mRXOb.19622$zj7.18392@newsread1.news.pas.earthlink.net...
| > | > > > I am trying to create and object and my compiler won't accept it?
[snip]
| > class Base
| > {
| > private:
| > const int N;
| > public:
| > Base() : N( 100 ) {}
| > int Print() const { return N; }
| > };
| >
| >
| > int main()
| > {
| > std::cout << "N == " << Base().Print() << std::endl;
| >
| > return 0;
| > }
| >
| > ...is legal, and compiles fine for me.
| >
| > Note: That it is the only way to initialise an 'const'
| > data member in the class.
| >
| > Is that what you meant by: "I already knew that" ?
|
| No, I meant that I already knew that the compiler I'm currently using
| doesn't get this right. I'm still confused. I put 2 examples into the
| online Comeau snippet compiler.
| http://www.comeaucomputing.com/tryitout/
I'm still confused as to why you're confused :-).
| First I compiled this:
| class grid
| {
| private:
| const int num = 30;
| int grd[num][num];
| };
|
| And I got this error:
| error: nonstandard member constant declaration (standard form is a static
| const integral member)
| const int num = 30;
That is correct.
As I stated earlier, initialising num(non-static const data
member) in the example above, can only be achieved via an
initialiser list.
grid() : num( 30 ) {} // Ok...
However, this is still not enough to provide an 'const'
expression for use in the array subscript, therefore,
even though the const data member can be initialised,
the following still won't work:
grid() : num( 30 ) {}
int grd[num][num];
...because the object hasn't been fully constructed as yet,
'num' is not seen as an compile time constant.
| Then I compiled this
| class grid
| {
| private:
| static const int num = 30;
| int grd[num][num];
| };
|
|
| And I got this success message:
| In strict mode, with -tused, Compile succeeded (but remember, the Comeau
| online compiler does not link).
|
| Your comments on that?
Yes, the latter one is fine according to the language rules,
however, I believe the rules state that it applies to only
integral based data members.
For example:
// Ok...
private:
static const int num = 30;
// Not Ok...
private:
static const char* num = "30";
static const std::string S = "X";
Make sense ?
Cheers.
Chris Val
- Next message: Norbert Riedlin: "Re: [C, C++] "(a=b) = c;" Illegal or Undefined?"
- Previous message: Richard Heathfield: "Re: [C] strcat() question (ongoing)"
- In reply to: jeffc: "Re: Is this legal?"
- Next in thread: Andrey Tarasevich: "Re: Is this legal?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|