Why to store a function parameter value in a class private variable?
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++ c class
add a comment |
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++ c class
C doesn't have classes or private variables, so you can't do that in C?
– immibis
Dec 17 at 1:47
1
You should read up on constructors.
– chrylis
Dec 17 at 9:17
@immibis I know, I just considering that the common methods in C++ and the equivalent in C.
– Perch Eagle
Dec 17 at 9:27
add a comment |
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++ c class
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++ c class
c++ c class
asked Dec 16 at 20:30
Perch Eagle
558
558
C doesn't have classes or private variables, so you can't do that in C?
– immibis
Dec 17 at 1:47
1
You should read up on constructors.
– chrylis
Dec 17 at 9:17
@immibis I know, I just considering that the common methods in C++ and the equivalent in C.
– Perch Eagle
Dec 17 at 9:27
add a comment |
C doesn't have classes or private variables, so you can't do that in C?
– immibis
Dec 17 at 1:47
1
You should read up on constructors.
– chrylis
Dec 17 at 9:17
@immibis I know, I just considering that the common methods in C++ and the equivalent in C.
– Perch Eagle
Dec 17 at 9:27
C doesn't have classes or private variables, so you can't do that in C?
– immibis
Dec 17 at 1:47
C doesn't have classes or private variables, so you can't do that in C?
– immibis
Dec 17 at 1:47
1
1
You should read up on constructors.
– chrylis
Dec 17 at 9:17
You should read up on constructors.
– chrylis
Dec 17 at 9:17
@immibis I know, I just considering that the common methods in C++ and the equivalent in C.
– Perch Eagle
Dec 17 at 9:27
@immibis I know, I just considering that the common methods in C++ and the equivalent in C.
– Perch Eagle
Dec 17 at 9:27
add a comment |
3 Answers
3
active
oldest
votes
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:
- By initializing it in the class constructor (like in your example).
- Within the class itself (by any method).
- 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 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 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 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 at 22:51
add a comment |
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 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 (astruct
). 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 singlestruct
. 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 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 at 21:42
add a comment |
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).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("schematics", function () {
StackExchange.schematics.init();
});
}, "cicuitlab");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "540"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f59803%2fwhy-to-store-a-function-parameter-value-in-a-class-private-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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:
- By initializing it in the class constructor (like in your example).
- Within the class itself (by any method).
- 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 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 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 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 at 22:51
add a comment |
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:
- By initializing it in the class constructor (like in your example).
- Within the class itself (by any method).
- 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 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 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 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 at 22:51
add a comment |
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:
- By initializing it in the class constructor (like in your example).
- Within the class itself (by any method).
- 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.
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:
- By initializing it in the class constructor (like in your example).
- Within the class itself (by any method).
- 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.
edited Dec 18 at 9:55
answered Dec 16 at 20:35
Michel Keijzers
6,36841738
6,36841738
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 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 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 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 at 22:51
add a comment |
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 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 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 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 at 22:51
1
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 at 21:15
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 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 at 21:29
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 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 at 21:34
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 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 at 22:51
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 at 22:51
add a comment |
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 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 (astruct
). 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 singlestruct
. 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 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 at 21:42
add a comment |
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 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 (astruct
). 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 singlestruct
. 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 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 at 21:42
add a comment |
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.
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.
answered Dec 16 at 20:39
Edgar Bonet
23.9k22344
23.9k22344
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 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 (astruct
). 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 singlestruct
. 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 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 at 21:42
add a comment |
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 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 (astruct
). 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 singlestruct
. 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 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 at 21:42
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 at 21:11
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 at 21:11
1
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 at 21:31
@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 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 at 21:42
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 at 21:42
add a comment |
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).
add a comment |
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).
add a comment |
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).
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).
answered Dec 16 at 20:39
KIIV
3,5371617
3,5371617
add a comment |
add a comment |
Thanks for contributing an answer to Arduino Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f59803%2fwhy-to-store-a-function-parameter-value-in-a-class-private-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
C doesn't have classes or private variables, so you can't do that in C?
– immibis
Dec 17 at 1:47
1
You should read up on constructors.
– chrylis
Dec 17 at 9:17
@immibis I know, I just considering that the common methods in C++ and the equivalent in C.
– Perch Eagle
Dec 17 at 9:27