Suppose I want to create a library where an object should receive another object added by the user and store for later use. Ex:
//header.h
class Caixa{
private:
Botao botoes[5];
public:
void addBotao(Botao botao){
int i;
while(botoes[i]!=NULL)
i++;
botoes[i] = botao;
}
Botao getBotao(int i){
return botoes[i];
}
}
class Botao{
private:
char nome[10];
public:
void Botao(char texto[10]){
nome = texto;
}
}
//main.cpp
void main(){
Caixa caixa1 = Caixa();
Botao botao1 = Botao("clique");
caixa1.addBotao(botao1);
}
For reasons of memory economy it would be interesting to pass this object by reference. But, considering which references should be referenced at startup, it seems to me that it would not be possible to store the reference in the object, right? Using pointers to make the passage, I would not be sure that the variable would still be there when I was to use it. Can you tell me the best way to solve this problem?