VansFannel
VansFannel

Reputation: 45921

Init a new array on instantiation

I'm developing an iOS application with latest SDK.

I have an Objective-C++ class with these two properties:

@interface MyClass : NSObject

@property (nonatomic) int* road;
@property (nonatomic) int* cars;

...

@end

And inside init method this:

    _road = new int[16] (0);
    _cars = new int[16] (0);

But I get this error: Array 'new' cannot have initialization arguments.

Reading this tutorial they said that I can initialize them that way.

How can I initialize those variables?

Upvotes: 0

Views: 423

Answers (2)

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81868

ISO C++ does not allow to specify a constructor parameter. The tutorial you linked might refer to a language extension not available in clang.

Upvotes: 1

Vitaly S.
Vitaly S.

Reputation: 2389

To fix a problem use such code:

_road = new int[16] ();
_cars = new int[16] ();

It initialises all members of arrays to default value.

Upvotes: 2

Related Questions