Constructor - member variable problem

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
 
A

Alf P. Steinbach

* 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

Use 'double', unless you really need to conserve memory.


Use 'std::vector', it's much more safe and convenient.

Furthermore, I have two constructors of which the first is
calling the second one...

As of C++98, a class T constructor can't call another class T
constructor on the same object, i.e., current C++ does not support
constructor forwarding.

Here an easy example (so don't worry about the semantics ;) )

You should always worry about semantics, and in fact the semantics are
the problem you're dealing with here.

Member variables:
float* pFeatVec;
int dimension;

This is not a C++ class definition.

FeatureVector::FeatureVector(ConfigReader* crd2) {

Pass by reference unless you want to allow a nullpointer as argument.

pFeatVec = NULL;
dimension = 4;

Use an initializer list rather than assignment.
FeatureVector(dimension);

This constructs a temporary FeatureVector.


See the FAQ item "Can one constructor of a class call another
constructor of the same class to initialize the this object?" currently
at <url: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.3>.
 
P

Puppet_Sock

silversurfer2025 said:
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...

See the comments in the following. But I think you've misunderstood
what you are doing in your ctor.
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);

What do you think that line does? You don't think it calls the
ctor for the current object, do you?

Basically, that line is creating a temporary un-named object
of type FeatureVector. Then, when it goes out of scope (at the
end of the ctor) it gets dtor-ed. It is not doing construction on
the current object, but creating a brand new (temporary) one.

If you want to have construction in this fashion, you have to name
the construction functions something other than the ctor name.
(The "named ctor" idiom.)

As so. (Code typed in at terminal, lots of chance of syntax error.)

FeatureVector::FeatureVector(ConfigReader* crd2)
{
// yada yada
DoFeatureVectorConstruction(dim);
}

void FeatureVector::DoFeatureVectorConstruction(int dim)
{
// yada yada
}

You don't want one ctor to be calling another ctor.
Socks
 
S

silversurfer2025

Well the example was just meant to illustrate my problem, so don't be
too pickt whether something is a float or double ;)

Thanks for the info, I did not know C++ could not do it (is there a
good reason why?).

Greetings
Tim

PS: Why should one only use references and not pointers?
 
P

Pete Becker

silversurfer2025 said:
Furthermore, I have two constructors of which the first is
calling the second one...

C++ doesn't allow that. The call to the second constructor creates a
temporary object, then destroys it.
 
D

Daniel T.

"silversurfer2025 said:
Well the example was just meant to illustrate my problem, so don't be
too pickt whether something is a float or double ;)

Thanks for the info, I did not know C++ could not do it (is there a
good reason why?).

Is there a good reason to do otherwise? Before the constructor exists,
the object is not constructed, so what would it mean to "call a
constructor" inside a constructor? Would the object be constructed
(because the inner constructor call exited) or would it not be
constructed (because the outer constructor is still executing?)

In languages that *do* allow constructors to be used like that, the
object is considered constructed before the constructor is entered.

PS: don't top-post.
 
S

silversurfer2025

In languages that *do* allow constructors to be used like that, the
object is considered constructed before the constructor is entered.
Yes, I knew constructor-chains from java and was a little confused to
find that they were not supported in C++.
PS: don't top-post.
OK, I promise to better myself! Thanks @all for your help, I now
created an init() method which is called by the constructors..

Thanks again
Tim
 
D

Daniel T.

In languages that *do* allow constructors to be used like that, the
object is considered constructed before the constructor is entered.
Yes, I knew constructor-chains from java and was a little confused to
find that they were not supported in C++.
PS: don't top-post.
OK, I promise to better myself! Thanks @all for your help, I now
created an init() method which is called by the constructors..[/QUOTE]

Just don't try to make that init() method virtual. The virtual mechanism
doesn't work in constructors either.
 
S

silversurfer2025

Daniel said:
Yes, I knew constructor-chains from java and was a little confused to
find that they were not supported in C++.

OK, I promise to better myself! Thanks @all for your help, I now
created an init() method which is called by the constructors..

Just don't try to make that init() method virtual. The virtual mechanism
doesn't work in constructors either.[/QUOTE]

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top