S
silversurfer2025
Hello everybody,
I had this problem several times now and did not yet get the reasoning
behind it. I have a class with a pointer as member variable, lets say a
float array. Furthermore, I have two constructors of which the first is
calling the second one...
Here an easy example (so don't worry about the semantics )
Member variables:
float* pFeatVec;
int dimension;
FeatureVector::FeatureVector(ConfigReader* crd2) {
pFeatVec = NULL;
dimension = 4;
FeatureVector(dimension);
}
FeatureVector::FeatureVector(int dim) {
dimension = dim;
pFeatVec = new float[dimension];
for(int i=0;i<dimension;i++) pFeatVec=0;
}
If I create a new object e.g. with FeatureVector* test = new
FeatureVector(&confReader); the program behaves as if there would have
never been the array creation in the second constructor. In other
words: Whatever variable-assignment I do in the second constructor, it
is not seen in the resulting object... Thus, I would have to write:
FeatureVector::FeatureVector(ConfigReader* crd2) {
pFeatVec = NULL;
dimension = 4;
pFeatVec = new float[dimension];
FeatureVector(dimension);
}
FeatureVector::FeatureVector(int dim) {
dimension = dim;
pFeatVec = new float[dimension];
for(int i=0;i<dimension;i++) pFeatVec=0;
}
in order to let the system work, but apparently this is kind of
shitty...
Btw: The same counts e.g. for dimension. If I only assign a value to it
within the second constructor, it is as well not known in the
outcome-object..
Greetings and please put an end to my non-knowing...
Thanks
Tim
I had this problem several times now and did not yet get the reasoning
behind it. I have a class with a pointer as member variable, lets say a
float array. Furthermore, I have two constructors of which the first is
calling the second one...
Here an easy example (so don't worry about the semantics )
Member variables:
float* pFeatVec;
int dimension;
FeatureVector::FeatureVector(ConfigReader* crd2) {
pFeatVec = NULL;
dimension = 4;
FeatureVector(dimension);
}
FeatureVector::FeatureVector(int dim) {
dimension = dim;
pFeatVec = new float[dimension];
for(int i=0;i<dimension;i++) pFeatVec=0;
}
If I create a new object e.g. with FeatureVector* test = new
FeatureVector(&confReader); the program behaves as if there would have
never been the array creation in the second constructor. In other
words: Whatever variable-assignment I do in the second constructor, it
is not seen in the resulting object... Thus, I would have to write:
FeatureVector::FeatureVector(ConfigReader* crd2) {
pFeatVec = NULL;
dimension = 4;
pFeatVec = new float[dimension];
FeatureVector(dimension);
}
FeatureVector::FeatureVector(int dim) {
dimension = dim;
pFeatVec = new float[dimension];
for(int i=0;i<dimension;i++) pFeatVec=0;
}
in order to let the system work, but apparently this is kind of
shitty...
Btw: The same counts e.g. for dimension. If I only assign a value to it
within the second constructor, it is as well not known in the
outcome-object..
Greetings and please put an end to my non-knowing...
Thanks
Tim