开发者

Accessing elements of an array defined in a class (C++)

开发者 https://www.devze.com 2022-12-26 04:12 出处:网络
Assume that int array arrayName is a member of class className, How can I access its element in my main pro开发者_JAVA技巧gram??className.arrayName[0] doesn\'t seem to workIf arrayName is static insid

Assume that int array arrayName is a member of class className, How can I access its element in my main pro开发者_JAVA技巧gram?? className.arrayName[0] doesn't seem to work


If arrayName is static inside class className, then you can access it like that:

//Declaration
class className{
public: 
  static int arrayName[5];
};

//Access
className::arrayName[index];

If it is not static, you must create an instance of your class first.

//Declaration
class className{
public: 
  int arrayName[5];
};

//Access
className a;
a.arrayName[index];


It should be objectName.arrayName[index], where objectName is an instance of your class. Don't forget to declare your arrayName public.

(Assuming that your arrayName is not static.)

0

精彩评论

暂无评论...
验证码 换一张
取 消