Go Down

Topic: Please explain: scope resolution (Read 710 times) previous topic - next topic

Stan09

Please explain in a very simple words these lines from some code example:
( http://arduino.cc/forum/index.php/topic,72739.msg546928.html#msg546928 )
Code: [Select]
#include <SD.h>
....
void Setup() {
....
SdFile::dateTimeCallback(dateTime);
}


this is what is confusing me:

1. What is 'SdFile' here and how is it related to SD library?
The first line includes SD library; Is SDFile some member or class or whatnot  from that library?

2. What exactly means a syntax like 'SdFile::<something>'?
can it be replaced with, f.e. 'SDFile.<something>'?

I see this used when creating a library, f.e.
Code: [Select]
void Morse::dot()
and it just means that dot() is a part of Morse class. But as I understand, this is used only when making library code; when we are using this library in a sketch, we use 'dot' notation, like
Code: [Select]
#include <Morse.h>
Morse morse(13);
void setup()
{
}
void loop()
{
  morse.dot();
}

So I probably understand that  the code above passes  callback function dateTime to dateTimeCallback(function?), defined within (SD? SDFile?)

So what exactly is the need of using SdFile::dateTimeCallback instead of
SdFile.dateTimeCallback?

pylon

Quote
1. What is 'SdFile' here and how is it related to SD library?


The SdFile class is part of the SD library.

Quote
2. What exactly means a syntax like 'SdFile::<something>'?


It means calling a class method of the SdFile class.

Quote
can it be replaced with, f.e. 'SDFile.<something>'?


No, because that would call an object method and for that you have to create an instantiation of the class (an object) first.


Class methods are used for changing values that exist only once, object methods are used to change values (or act otherwise on it) of the specific object, of which many can exist. So class variables are shared among all objects of that class but object values are kind of private property of that object.

PaulS

Quote
It means calling a class method of the SdFile class.

Specifically, it means calling a static method of the class.

A static method is not unique to an instance of the class. Other methods are.

Writing to a file, for instance, is something that depends on which file (which instance of the SdFile class) you are trying to write to.

Setting up a callback, so the SdFile class knows what time it is, is not something that one instance of the SdFile class wants to do different from any other instance. So, the method is static (shared by all instances).

Therefore, the scope resolution operator is needed, to tell the class, not an instance of the class, to do something.

Go Up