Lets say I have a sketch called mysketch.ino with these contents:
String globalString = "ThisisGlobal";
void setup(){}
void loop(){}
How can I pass THIS sketch mysketch.ino
as an object or its reference to a .h
file named
StringChanger.h
with the following contents, so that StringChanger.h
can change somevariables belonging to mysketch.ino
.
class StringChanger{
template <typename T> // or type of mySketch.ino i don't know what i'm doing
void changeString(T t) { //a Reference Or Object Or Whatever
t.globalString = "GotYouAndChangedYou";
}
}
I wan't to have simple usage in the mySketch.ino
.
For instance:
#include "StringChanger.h"
StringChanger change(**this**); // perhaps default empty constructor here
String globalString = "ThisisGlobal";
void setup(){
change.changeString(); // perhaps pass reference to **this** here
}
void loop(){}
How can I accomplish this (give a class an object so that it can change some value of that object) in general and not neccesarily in this context. Errors I currently get:
invalid use of 'this' in non-member function
invalid use of 'this' at top level.
Arduino OOP and [this] keyword
- it's C++. So whatever works in C++ works in Arduino. – Nick Gammon♦ Apr 11 at 9:57StringChanger change(**this**);
this doesn't make any sense at all, sorry. – Nick Gammon♦ Apr 11 at 9:58