[Gambas-user] static const?

Doriano Blengino doriano.blengino at ...1909...
Wed Apr 14 18:42:11 CEST 2010


Les Hardy ha scritto:
>    
>> The C language, in facts, does not even have CONSTs - it goes with
>> #define. So, it would be correct to forbid STATIC when declaring CONSTs.
>>      
> Surely this is not correct. ANSI C uses const, and C++ also uses the
> const keyword.
> #define (a preprocessor directive) is a relic from old C, and const is
> now recommended use instead.
>    
Surely, according to http://www.ericgiguere.com/articles/ansi-c-summary.html
> The declaration:
>      enum colours { RED, BLUE, GREEN };
>    
>
> would declare colours as an enumeration tag representing the integer 
> constants RED, BLUE and GREEN. These enumeration constants are given 
> integer values starting at 0 and increasing by 1 with each identifier.
>
> An enumeration constant may be used wherever an integer is expected. 
> The following is equivalent to the above enumerated type:
>
>      #define RED   0
>      #define BLUE  1
>      #define GREEN 2
>    

Moreover, from http://tigcc.ticalc.org/doc/keywords.html
>
>
>     _const_
>
> *Makes variable value or pointer parameter unmodifiable.*
>
> When |const| is used with a variable, it uses the following syntax:
>
> const/variable-name/  [ =/value/];
>    
>
> In this case, the |const| modifier allows you to assign an initial 
> value to a variable that cannot later be changed by the program. For 
> example,
>
> const my_age = 32;
>    
>
> Any assignments to |'my_age'| will result in a compiler error. 
> However, such declaration is quite different than using
>
> #define my_age 32
>    
> In the first case, the compiler allocates a memory for |'my_age'| and 
> stores the initial value 32 there, but it will not allow any later 
> assignment to this variable. But, in the second case, all occurences 
> of |'my_age'| are simply replaced with 32 by the preprocessor 
> <http://tigcc.ticalc.org/doc/cpp.html>, and no memory will be 
> allocated for it.

You perhaps refer to "const" modifier, which is different from declaring 
a constant, like in
> public:
>     WinEDA_VertexCtrl( wxWindow* parent, const wxString& title,
>                        wxBoxSizer* BoxSizer, int units, int 
> internal_unit );

> Also, it would be correct to use static with const, the line below would
> be correct use.
>
> static const int daysPerMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
>    
In this case - talking about C and not C++:
> |static| tells that a function or data element is only known within 
> the scope of the current compile. In addition, if you use the |static| 
> keyword with a variable that is local to a function, it allows the 
> last value of the variable to be preserved between successive calls to 
> that function. 
"const" says that data is not modifiable, so "static" would say the same 
thing. Note also the ambiguity of the keyword when applied to data 
global to a module, and data local to a function - the same keyword does 
two very different things.

>
> Having said that, The original question was not about C,  I think Fabian
> was simply asking about the scope of constants in classes (in Gambas)
>    
Uhm... is it forbidden to cite other languages to better explain a 
concept? So why you cited C++?

Anyway, you are right, the original question was about scope. In gambas 
scope is governed by PRIVATE and PUBLIC. Full stop.

If you feel that my reply is a little hurting, excuse me; it is because 
your reply seemed hurting to me. Prove to me that I am wrong and I will 
publicly apologize.

Regards,
Doriano




More information about the User mailing list