0

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.
  • 1
    Arduino OOP and [this] keyword - it's C++. So whatever works in C++ works in Arduino. – Nick Gammon Apr 11 '16 at 9:57
  • StringChanger change(**this**); this doesn't make any sense at all, sorry. – Nick Gammon Apr 11 '16 at 9:58
  • I thought stack over flow would bold the string sorry. I meant change(this); and I understand Arduino has less of c++ especially the heavy complex parts because of the speed of micro controllers . I don't think if it works in C++ it's works in Arduino – Nigel Tiany Apr 11 '16 at 15:44
  • It's nothing to do with speed. Exceptions are disabled by a command-line option. The rest is just pure C++. – Nick Gammon Apr 11 '16 at 21:09
1

A sketch isn't an object. It is merely a collection of variables, functions and, of course, classes.

Your example seems to be trying to make things incredibly complex when they don't need to be.

There are a number of ways of referencing things in one file from another - whether or not they are in a class. The simplest is to use the extern keyword. This is used in a file where you want to reference a variable or function that is defined in the global scope of another file. It says "This variable is global, but it's defined somewhere else."

// Sketch
String myString = "foo";

// Library
extern String myString;
// ... later on ...
myString = "bar";

Of course, that means that the name of the variable is fixed and cannot change. So you can pass the variable to whatever it is you want to use that variable in. That could be as simple as passing it to your "changer" function:

// Sketch
String myString = "foo";
StringChanger change;
// ... later ...
change.changeString(myString);

// Library
class StringChanger {
    public:
        void changeString(String &stringToChange) {
            stringToChange = "bar";
        }
};

If you want to "tie" the variable to the class so you don't have to pass it each time you could pass it to the constructor and have the class remember it:

// Sketch
String myString = "foo";
StringChanger change(myString);
// ... later ...
change.changeString();

// Library
class StringChanger {
    private:
        String *_string;
    public:
        StringChanger(String &string) {
            _string = &string;
        }
        void changeString() {
            *_string = "bar";
        }
}

And there's a myriad of other ways of doing it.

  • First part of your answer is what I want to do because I simply want that calling the constructor changes the string that is finds that extern string named myString and changes it to another. I don't want to use the function that accepts a string reference. Thanks @Majenko – Nigel Tiany Apr 11 '16 at 16:21
  • Why not just make a function that changes an external string? I don't see why you would make a class just to change some external thing, which is not in the class, and doesn't do anything else. – Nick Gammon Apr 11 '16 at 21:10
  • @NickGammon Maybe that's just an example and he's really doing something completely different. Who knows with these kids? Either way, what he is doing is most likely not the right way of doing things and using getters and setters is more likely the right solution for managing his class. – Majenko Apr 11 '16 at 21:12
  • Sounds like one of the better examples of the X-Y Problem that I have seen in a while. – Nick Gammon Apr 11 '16 at 21:17
  • 1
    Yes, we need a close reason: "this is the wrong question". :) – Nick Gammon Apr 11 '16 at 21:22

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.