1

Assume that int array arrayName is a member of class className, How can I access its element in my main program?? className.arrayName[0] doesn't seem to work

2
  • Could you add some code to the question? Commented Apr 11, 2010 at 11:13
  • 1
    did you try objectofclassName.arrayName[0]? Commented Apr 11, 2010 at 11:18

2 Answers 2

12

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];
Sign up to request clarification or add additional context in comments.

Comments

3

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.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.