Accessing elements of an array defined in a class (C++)
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.)
精彩评论