I am implementing a LinkedList in my project and this library has an example called ClassList on how to use the library. Now, I am programming for couple of years now, but I am fairly new to C++, so I would be grateful if I would just get some simple explanations on couple of questions that I have on why the example was written the way it was. The original code of the example is like this:
#include <LinkedList.h>
// Let's define a new class
class Animal {
public:
char *name;
bool isMammal;
};
char catname[]="kitty";
char dogname[]="doggie";
char emuname[]="emu";
LinkedList<Animal*> myAnimalList = LinkedList<Animal*>();
void setup()
{
Serial.begin(9600);
Serial.println("Hello!" );
// Create a Cat
Animal *cat = new Animal();
cat->name = catname;
cat->isMammal = true;
// Create a dog
Animal *dog = new Animal();
dog->name = dogname;
dog->isMammal = true;
// Create a emu
Animal *emu = new Animal();
emu->name = emuname;
emu->isMammal = false; // just an example; no offense to pig lovers
// Add animals to list
myAnimalList.add(cat);
myAnimalList.add(emu);
myAnimalList.add(dog);
}
void loop() {
Serial.print("There are ");
Serial.print(myAnimalList.size());
Serial.print(" animals in the list. The mammals are: ");
int current = 0;
Animal *animal;
for(int i = 0; i < myAnimalList.size(); i++){
// Get animal from list
animal = myAnimalList.get(i);
// If its a mammal, then print it's name
if(animal->isMammal){
// Avoid printing spacer on the first element
if(current++)
Serial.print(", ");
// Print animal name
Serial.print(animal->name);
}
}
Serial.println(".");
while (true); // nothing else to do, loop forever
}
And then I tried to modify the code without using (so much) pointers:
#include <LinkedList.h>
// Let's define a new class
class Animal {
public:
char *name;
bool isMammal;
};
char catname[]="kitty";
char dogname[]="doggie";
char emuname[]="emu";
LinkedList<Animal> myAnimalList = LinkedList<Animal>();
void setup()
{
Serial.begin(9600);
Serial.println("Hello!" );
// Create a Cat
Animal cat;
cat.name = catname;
cat.isMammal = true;
// Create a dog
Animal dog;
dog.name = dogname;
dog.isMammal = true;
// Create a emu
Animal emu;
emu.name = emuname;
emu.isMammal = false; // just an example; no offense to pig lovers
// Add animals to list
myAnimalList.add(cat);
myAnimalList.add(emu);
myAnimalList.add(dog);
}
void loop() {
Serial.print("There are ");
Serial.print(myAnimalList.size());
Serial.print(" animals in the list. The mammals are: ");
int current = 0;
Animal *animal;
for(int i = 0; i < myAnimalList.size(); i++){
// Get animal from list
animal = &myAnimalList.get(i);
// If its a mammal, then print it's name
if(animal->isMammal){
// Avoid printing spacer on the first element
if(current++)
Serial.print(", ");
// Print animal name
Serial.print(animal->name);
}
}
Serial.println(".");
while (true); // nothing else to do, loop forever
}
Both versions compile and work just fine. So my question is, why would there be used pointers to the objects in the original example, instead of the objects themselves as in the second example? Also, what is the "right" way to approach this matter? I do understand the concept of pointers, the question is more about why as about how.
Then the second question that I have is, why is there in the example provided also a pointer as a member variable "*name" used, and not the "actual" member variable?
And the last is, I went through some videos about classes in C++ and how to instantiate the objects, and I noticed that there are couple of ways, how people are doing it, and all of them work, as for example:
LinkedList<Animal> myAnimalList = LinkedList<Animal>();
// or
LinkedList<Animal> myAnimalList();
// or
LinkedList<Animal> myAnimalList;
Animal cat = new Animal();
// or
Animal cat();
// or
Animal cat;
I understand that it might differ with the parenthasis, if the constructor actually needs parameters to be fed to it, but if we put that aside, is there any difference between these three examples?
Thanks in advance for your time.