Pages: [1]   Go Down
Author Topic: Please explain private and public in library header file  (Read 740 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 138
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
     I am very confused on these fundamental questions about variables in libraries. Could someone please explain to me when library variables should be:

1) listed in a .h file

2) listed as "private" in a .h file

3) listed as "public" in a .h file
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Private means nothing outside the class can access it.
Public is the exact opposite.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1184
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Variables should never be declared in a .h file as this is bad practice. They can be declared globally by putting them in a library and the external reference in the header file.  If they are declared in the header file they will exist as a local copy everywhere the header file is included.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Variables should never be declared in a .h file as this is bad practice
Unless, of course, they are class variables, in which case, the .h is the very best place to declare them. Probably the only place.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1184
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

They are defined in the class definition but don't exist until the object is created.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Full Member
***
Karma: 0
Posts: 138
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Variables should never be declared in a .h file as this is bad practice
Unless, of course, they are class variables, in which case, the .h is the very best place to declare them. Probably the only place.

Yes, they are all class variables.
Sorry for not giving enough information.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, class variables should almost always be private.
It generally keeps things simple and clean.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Full Member
***
Karma: 0
Posts: 138
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, class variables should almost always be private.
It generally keeps things simple and clean.

Thanks.

However, lets say a variable is used in only one function of the class.  Does it even need to be listed in the .h file?

I guess the heart of my question is, when should you list class variables in the .h file?
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A class is defined in an .h file.

If the variable is to be a class variable, it is defined within the class.

Are you asking when should a variable be a class variable?  If so, that is one of the routine continuous questions of Object Oriented Design.

If you're asking whether the class variable should be public or private (or protected--but no need to go there here), that's another one but the short answer I would say is "public if you want it to be part of the interface" and "private if it's part of the implementation".  Another way to say might be

I'm the Object.  My variable will be public if I don't mind my users accessing me without me knowing it.  If I do mind, I will instead do something like this
Code:
class Me{
   private: 
     int myVariablePPM;  // e.g. stored as part-per-million
   public
     int myVariablePpm(){ return myVariable; }          // this way I "know it" when someone tries to access me
     int myVariablePct(){ return myVariable/10000; }  // or even pretend to have variables that I don't
};

But this is just nuts and bolts OO.... not sure if its what you're asking smiley

Cheers,
John
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 138
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A class is defined in an .h file.

If the variable is to be a class variable, it is defined within the class.

Are you asking when should a variable be a class variable?  If so, that is one of the routine continuous questions of Object Oriented Design.

If you're asking whether the class variable should be public or private (or protected--but no need to go there here), that's another one but the short answer I would say is "public if you want it to be part of the interface" and "private if it's part of the implementation".  Another way to say might be

I'm the Object.  My variable will be public if I don't mind my users accessing me without me knowing it.  If I do mind, I will instead do something like this
Code:
class Me{
   private: 
     int myVariablePPM;  // e.g. stored as part-per-million
   public
     int myVariablePpm(){ return myVariable; }          // this way I "know it" when someone tries to access me
     int myVariablePct(){ return myVariable/10000; }  // or even pretend to have variables that I don't
};

But this is just nuts and bolts OO.... not sure if its what you're asking smiley

Cheers,
John

John,
     Nice writeup.  Its funny that before I read your reply I did some studying on classes and objects.  I realize now that I need to understand OO better to build effective Arduino libraries.
Logged

Pages: [1]   Go Up
Jump to: