0

I'm reading data from a file in JSON format, and I want to put the values in the objects password_c and apiKeyValue members:

class Configuration {
private:
    char password_c[32];
    char apiKeyValue_c[20];
public:
    String get_ApiKeyValue();
    void set_ApiKeyValue(char* apiKeyValue);

    String get_Password();
    void set_Password(char* password);
};

String Configuration::get_Password() {
    return password_c;
}

void Configuration::set_Password(char* password ) {
    strcpy(password_c, password); 
}  

String Configuration::get_ApiKeyValue() {
    return  apiKeyValue_c;
}

void Configuration::set_ApiKeyValue(char* apiKeyValue) {
    strcpy(apiKeyValue_c, apiKeyValue); 
}

bool Configuracao::loadFile() {

    if (!SPIFFS.begin()) {
        Serial.println("Failed to mount file system");
        while(true);
    }
  
    StaticJsonDocument<700> doc; 
    File file = SPIFFS.open(("/config.json"), "r");
    if (!file){
        Serial.println("Failed to open file for reading");
        //return;
    }

    DeserializationError error = deserializeJson(doc, file);
    if (error) {
        Serial.print(F("deserializeJson() failed: "));
        Serial.println(error.f_str());
        return -1;
    } 

    strcpy(password_c, doc["password"]); // I belive that it can't work, but it work. password_c is private
    strcpy(apiKeyValue_c, doc["apiKeyValue"]);

    // - Test
    Serial.println(get_Password()); //value ok!
    Serial.println(password_c);     //value ok! 
    file.close();
}

The commands work inside the method that reads the JSON file, but I thought this would not be possible since both members are private.

I can print the result of password_c and apiKeyValue_c right after strcpy().

Serial.println(get_Password()); // using the method
Serial.println(password_c); // direct (I don't know why this way works, as they are private.

I imagined that I would have to implement set methods to access these objects.

Can someone enlighten me?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Mateus
  • 1
  • the fields are private to the methods, loadFIle is a method of the class – pm100 Mar 09 '23 at 23:52
  • On an unrelated note, using `std::string` is (almost always) better than `char []` for storing strings. – Ken Y-N Mar 09 '23 at 23:53
  • The code, as shown, will not compile, since there is no class `Configuracao` defined, and the `Configuration` class that is shown has no `loadFile()` method declared. Please don't make up code, copy/paste your *real* code. – Remy Lebeau Mar 10 '23 at 00:03

1 Answers1

0

Code that is outside of a class, and is not declared to be a friend of that class, cannot access that class's private members. But, code that in inside of the class's own methods can freely access the class's private members. A class would be fairly useless if it couldn't access its own data.

So, in this case, assuming you meant for Configuracao to be Configuration instead, and that you meant to declare a loadFile() method in the Configuration class, then Configuration::loadFile() can access Configuration's private members without restriction. But, any code that is using a Configuration object (and is not a friend of Configuration) would not be able to access the private members at all, and would have to use its public getters/setters instead.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770