A few brief general questions about variables in a class

Just began using classes, based off of the LibraryTutorial. Some of the parts to be accomplished in my project aren't included in the tutorial, and I'm having a hard time finding the answers elsewhere:

  1. What is the difference between using a private variable in the header file, and declaring a variable outside of any functions in the .cpp file?

  2. Why is it necessary to add the size of an array when declaring one as a private variable? Asking this just in case private arrays behave differently (who knows, I'm learning new stuff every day). For example, I get an error when my array looks like this:

uint8_t myArray[] = {1, 2, 3};

but this works:

uint8_t myArray[3] = {1, 2, 3};

Still a noob. Thanks for the help.

  1. If you define a variable ONLY in the .cpp file, it is NOT a class variable. ONLY variables defined INSIDE the class definition (which typically resides in in the .h file) are members of that class. Anything else is just a regular (usually global) variable.

  2. Your two examples are precisely the same. Both will create arrays of length 3. You CANNOT define a class member array without defining the length of the array by either of the methods you show.

What error did you get with the first array declaration?

uint8_t myArray[] = {1, 2, 3};

Thanks for the replies, all.

What error did you get with the first array declaration?

This was the error that came up:

too many initializers for 'int [0]'

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.