5
Suppose I want to create a library, where an object should receive another object added by the user and store it 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 saving it would be interesting to pass this object by reference. But, considering that references should be referenced in the initialization, it seems to me that it would not be possible to store the reference in the object, right ? Using pointers to do the crossing, I would have no assurance that the variable would still be there when I went to use it. Can you tell me the most appropriate way to solve this problem ?