F
Fred Mangusta
Hi,
I have an object, Model, that creates another object, Trainer.
Trainer, given some data, fills some structures with processed data.
My problem is the following: I need to pass those structures back UP to
Model, because I want to use Model's save method to save them into a
file. I could save them through Trainer directly, but I would like to
concentrate all file operation in Model, and not scatter them all around.
I'm not really sure about the correct way to achieve this.
I managed to write some code that does this, but I have concerns about
the lifetime of the objects in question. This is my way of doing this:
I created the following public method in the Trainer class:
bool Trainer::setDataSTRUCT(RandLMStruct* &struct_,
RandLMInfo* &info_,
LogQuantiser* &quantiser_){
assert(TrainerStruct != NULL);
struct_ = TrainerStruct;
assert(TrainerInfo != NULL);
info_ = TrainerInfo;
assert(TrainerQuantiser != NULL);
quantiser_ = TrainerQuantiser;
return true;
}
where
randlm::RandLMStruct* TrainerStruct;
randlm::LogQuantiser* TrainerQuantiser;
randlm::RandLMInfo* TrainerInfo;
are pointers to the objects, allocated, owned, and filled with data by
Trainer: I want to use setDataSTRUCT to pass these UP to Model.
struct_, info_ and quantiser_ instead, are in the definition of the
Model class.
Therefore in model.h, I have what follows:
class Model {
public:
Model(): info_(NULL), struct_(NULL), quantiser_(NULL){}
//public methods...
private:
//these will get data from Trainer.
randlm::RandLMInfo* info_;
randlm::RandLMStruct* struct_;
randlm::LogQuantiser* quantiser_;
};
Soon after Trainer has been called by Method to process the data,
//..
scoped_ptr<Trainer> t;
t->train(...)
//...
I would like to call it passing struct_, info_, quantiser_ by reference,
as follows:
t->setDataSTRUCT(struct_,info_,quantiser_);
Now, is this approach correct? I'm not sure regarding WHO destructs the
three objects after I will have their contents saved in the file. In
fact, if I create a destructor in Model(), and write something like
~Model() {
delete info_;
delete struct_;
delete quantiser_;
}
gcc complains: this is understandable, because I have not allocated
space for them, but just made them point to already existing objects
(TrainerStruct, TrainerInfo, TrainerQuantiser).
But then if Trainer destroys these pointers, will the data to which they
point to be still available when I will need to write it - this time
using info_, struct_, quantiser_??
Is all of the above correct? Do you suggest some better approach?
Thank you in advance for your clarifications.
F.
I have an object, Model, that creates another object, Trainer.
Trainer, given some data, fills some structures with processed data.
My problem is the following: I need to pass those structures back UP to
Model, because I want to use Model's save method to save them into a
file. I could save them through Trainer directly, but I would like to
concentrate all file operation in Model, and not scatter them all around.
I'm not really sure about the correct way to achieve this.
I managed to write some code that does this, but I have concerns about
the lifetime of the objects in question. This is my way of doing this:
I created the following public method in the Trainer class:
bool Trainer::setDataSTRUCT(RandLMStruct* &struct_,
RandLMInfo* &info_,
LogQuantiser* &quantiser_){
assert(TrainerStruct != NULL);
struct_ = TrainerStruct;
assert(TrainerInfo != NULL);
info_ = TrainerInfo;
assert(TrainerQuantiser != NULL);
quantiser_ = TrainerQuantiser;
return true;
}
where
randlm::RandLMStruct* TrainerStruct;
randlm::LogQuantiser* TrainerQuantiser;
randlm::RandLMInfo* TrainerInfo;
are pointers to the objects, allocated, owned, and filled with data by
Trainer: I want to use setDataSTRUCT to pass these UP to Model.
struct_, info_ and quantiser_ instead, are in the definition of the
Model class.
Therefore in model.h, I have what follows:
class Model {
public:
Model(): info_(NULL), struct_(NULL), quantiser_(NULL){}
//public methods...
private:
//these will get data from Trainer.
randlm::RandLMInfo* info_;
randlm::RandLMStruct* struct_;
randlm::LogQuantiser* quantiser_;
};
Soon after Trainer has been called by Method to process the data,
//..
scoped_ptr<Trainer> t;
t->train(...)
//...
I would like to call it passing struct_, info_, quantiser_ by reference,
as follows:
t->setDataSTRUCT(struct_,info_,quantiser_);
Now, is this approach correct? I'm not sure regarding WHO destructs the
three objects after I will have their contents saved in the file. In
fact, if I create a destructor in Model(), and write something like
~Model() {
delete info_;
delete struct_;
delete quantiser_;
}
gcc complains: this is understandable, because I have not allocated
space for them, but just made them point to already existing objects
(TrainerStruct, TrainerInfo, TrainerQuantiser).
But then if Trainer destroys these pointers, will the data to which they
point to be still available when I will need to write it - this time
using info_, struct_, quantiser_??
Is all of the above correct? Do you suggest some better approach?
Thank you in advance for your clarifications.
F.