I am trying to pass a pointer to a const char * from a child to its parent, but I am clearly not understanding how to do this correctly. Here is the code which contains three classes: 1) parent 2) Alpha (child) 3_ Beta (child)
#pragma once
class Parent {
protected:
char *childName;
public:
Parent ( char* _childName ) {
childName = _childName;
}
char *getChildName () {
return childName;
}
};
class Alpha: public Parent {
protected:
const char* alphaName = "ALPHA";
public:
Alpha (): Parent ( alphaName ) {
Serial.print ( F ( "My name is " ) );
Serial.println ( getChildName () );
}
};
class Beta: public Parent {
protected:
const char* betaName = "BETA";
public:
Beta (): Parent ( betaName ) {
Serial.print ( F ( "My name is " ) );
Serial.println ( getChildName () );
}
};
Alpha *alpha;
Beta *beta;
void setup () {
Serial.begin ( 115200 );
while (!Serial.availableForWrite ()) {}
alpha = new Alpha ();
beta = new Beta ();
}
void loop() {}
And here is the output I get: My name is �
My name is �˵
const
to have it compile, and I get the expected output: "My name is ALPHA\r\nMy name is BETA\r\n".const char betaName[] = "BETA";
unfortunately I can't remember the precise details but it was to do with the way the memory was allocated. HOWEVER this was back in the 90's so hopefully the compilers are better now.