Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.
share|improve this question
1  
Arduino OOP and [this] keyword - it's C++. So whatever works in C++ works in Arduino. – Nick Gammon Apr 11 at 9:57
    
StringChanger change(**this**); this doesn't make any sense at all, sorry. – Nick Gammon Apr 11 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 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 at 21:09
up vote 1 down vote accepted

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.

share|improve this answer
    
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 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 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 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 at 21:17
1  
Yes, we need a close reason: "this is the wrong question". :) – Nick Gammon Apr 11 at 21:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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