1

I'm just curious, I'm going through a C++ library for the mcu2.4 TFT display.

And this method I notice when I run through C++ libraries.

Which is when a parameter is passed through a function, it's stored in a private variable; like this one:

HCTFT::HCTFT(byte DisplayType)
{
    _Display = DisplayType;

What's the benefit of this method? What's the similar method when I want to do it in C code?

  • C doesn't have classes or private variables, so you can't do that in C? – immibis Dec 17 '18 at 1:47
  • 1
    You should read up on constructors. – chrylis Dec 17 '18 at 9:17
  • @immibis I know, I just considering that the common methods in C++ and the equivalent in C. – Perch Eagle Dec 17 '18 at 9:27
4

The reason is that _Display will be used probably after its construction.

So you pass the byte during construction, and afterwards the value is available during the object's lifetime.

In C you probably will do this by:

  • Passing the variable (byte DisplayType) in each function where the value is needed (you can use this way in C++ too but it's cumbersome in both C/C++ to pass the variable in all functions needed).
  • Storing it in a global variable (static for that .c file); this is the typically C method.

* Added explanation *

Such a variable is called class variables and are mostly made private. The reason is that other objects cannot change the value directly. The only way to change a variable is:

  1. By initializing it in the class constructor (like in your example).
  2. Within the class itself (by any method).
  3. By a typical Set method (like SetDisplayType). This method can check if the value passed is within a range, and there is only one entry point in this class to change it.

To retrieve the value, typically a GetDisplayType method should be created which is public. The variable itself is private.

  • 1
    OK, I understand, so it's a programming style. Hmm alright, I'm starting to understand C++ syntax. I'm just running through the library. Most of the time when I run through C++ libraries I encounter things; like, private, public, inline, virtual .. etc. There are also things I want to understand too, I'm just taking it step at a time to get to know C++ better, I'm thinking how C++ would be effective to me when I want to write a library. – Perch Eagle Dec 16 '18 at 21:15
  • Well in C++ (or actually an OO language) it's very normal to put information (data/variables) and functionality together, and in a class (private) variable you store data that belongs to that instance of the class. – Michel Keijzers Dec 16 '18 at 21:29
  • OK, this one is clear to me now, so the class is just a complete object which has its own functions and variables and that's it. Then also there are other specifiers which provide the property to use this class functions or variable by other classes but of course it's much more advanced topic than my current knowledge. – Perch Eagle Dec 16 '18 at 21:34
  • It's better to read a book about OO design, than you will get to learn all the reasons behind it. The main idea is: keep data/functions together, keep data private, keep functions lean and public to others. – Michel Keijzers Dec 16 '18 at 22:51
3

Michel Keijzers explained pretty well the purpose of that idiom. Just to complement his answer, you can do exactly the same in C, only with a slightly different syntax:

typedef struct {
    byte _Display;
    // ...
} HCTFT;

HCTFT_construct(HCTFT *this, byte DisplayType)
{
    this->_Display = DisplayType;
    // ...
}

Now, every function that receives a pointer to an HCTFT (a HCTFT “method”) has access to that data as a member of the struct.

  • So you mean I have to store it somewhere whether in a class private member with C++, struct variable with C or a global variable. So I don't have to pass it again to other functions, and I just can call its value from any function without passing it to that function? Am I understanding it correctly? – Perch Eagle Dec 16 '18 at 21:11
  • 1
    @PerchEagle: You don't have to: there are always many ways to do one thing. What I say is that, if you need to store several pieces of data for managing a TFT display, it is convenient to bundle them together into a single data structure (a struct). Then, instead of passing all the pieces as separate arguments to every function that deals with the display, you just pass a pointer to that single struct. The code becomes better structured and more readable. The C++ classes and methods are basically syntactic sugar over this programming pattern. – Edgar Bonet Dec 16 '18 at 21:31
  • Wonderful, I just have to dive into writing the TFT library to learn how to use these skills, I started to put the definitions in the header file. But what I'm considering now is that the process could be similar if I used my basic C skills to write a library hmmm. I found a good example like this piece of code. void HCTFT::SetFG(byte R, byte G, byte B) { _FGR = R; _FGG = G; _FGB = B; } so now the result is that the underscore variables are declared in the private section. And now by passing them to this function then they can be used right away in other functions! Nice. – Perch Eagle Dec 16 '18 at 21:42
2

That is more Object Oriented Programing question than the Arduino related.

Encapsulation is one of the fundamentals of OOP and it's basically you'll provide some interface to the object and the outside world doesn't have to know anything about it's internals (so it's not possible to change it into inconsistent state).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.