• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: #define works, const short does not for Ctype array in class
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: #define works, const short does not for Ctype array in class


  • Subject: Re: #define works, const short does not for Ctype array in class
  • From: Alexander Spohr <email@hidden>
  • Date: Mon, 14 Jun 2004 20:58:01 +0200

Am 14.06.2004 um 16:23 schrieb George Woodrow III:

I have the following code:

const short MaxNEvalValues = 6; // array size for evalValues[]

@interface STQualityStandards : NSObject
{
float evalVals[MaxNEvalValues];
}

If I compile this, I get an error:

error: variable-size type declared outside of any function.

If I use the #define, everything works fine, as does hard coding the 6 in the array.

For performance reasons, I need to use a c style array, not an MSMutableArray.

Using the const works in c/c++.

What do I have to do to make it work in Cocoa?

just some thoughts. i am only sure about a, didn't do C++ for a long time :)

a) you declare MaxNEvalValues in the header.
every #include/#import of that header would try to declare that variable again. this can not work.
b) const in C means that no one _should_ change the contents (but it's still is a variable), const in C++ means that no one _can_ change the content (and the variable is replaced by it's value, as java does it with finals).
c) C++ will call a constructor for float[] with MaxNEvalValues as a parameter, so it is allowed to use a variable for the size. C has no constructors for [], so a variable size is never allowed in a struct.

- if you want to define that arrays size at compile-time use #define.
- if you want to have it dynamic use float * and malloc(sizeof(float)*MaxNEvalValues) and move MaxNEvalValues to the .m file. if you want MaxNEvalValues to be visible to others use extern in the header.

atze
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >#define works, const short does not for Ctype array in class (From: George Woodrow III <email@hidden>)

  • Prev by Date: window resize problem (NSMinX(windowFrame), NSMaxY(windowFrame)
  • Next by Date: How to refresh a directory view after a movePath
  • Previous by thread: Re: #define works, const short does not for Ctype array in class
  • Next by thread: NSTextAttachment problems
  • Index(es):
    • Date
    • Thread