Re: Is this legal?

From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 01/23/04


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



Relevant Pages

  • Re: const and constant expression
    ... I understand that const is 'read-only' but I could not convince my ... All the expressions in an initializer for an object ... to const int) which he shot down saying the comparison of examples is ... Is the compiler correct after optimisation? ...
    (comp.lang.c)
  • Re: #define vs. const
    ... >I was wondering if a const variable or object took up space. ... for the array dimension, and at the same time, the compiler probably ... const int array; // int, ... Comeau C/C++ with Dinkumware's Libraries... ...
    (comp.lang.cpp)
  • Re: Is const really const?
    ... declared const can not be modified by the module it is defined in. ... Once the compiler diagnoses the problem, though, it's allowed ...
    (comp.lang.c)
  • Re: double loop optim
    ... const double p0 = 0.51; ... const int N = 100000; ... 2.cpp: remark: LOOP WAS VECTORIZED. ... You've not indicated that what the compiler produces ...
    (comp.lang.asm.x86)
  • Re: Communicating between Applications
    ... Private Declare Function CloseHandle _ ... Const SUBLANG_DEFAULT = &H1 ... Private Sub Command1_Click ... Dim BytesWritten As Long ...
    (microsoft.public.vb.general.discussion)