Calling constructor

S

Sylvain

Let's say I have the following code where a class 'pipo' has 8 instances
of foo:

class foo
{
foo ( const char * _name):
name = _name
{

}
private:
const char * name;
};

class pipo
{
foo *mf[8];
pipo()
{
for (unsigned int i = 0 ; i < 8; ++i)
{
ostringstream ost;
ost << "MY_FOO_" << i;
string name = ost.str();
mf = new foo (name.c_str());
}
}
};

Is it mandatory to do in such a way: using array of pointers on foo. Or
may I do it by using plain array of instances of foo?
In this case, I have no clue how to process to call the constructor with
the correct C like string as defined in the exemple...
Do you have any ideas how to proceed?

Something like this?

class pipo
{
foo mf[8];
pipo ():
????
{

}
};


Regards

Sylvain
 
V

Victor Bazarov

Sylvain said:
Let's say I have the following code where a class 'pipo' has 8 instances
of foo:

class foo
{
foo ( const char * _name):
name = _name
{

}
private:
const char * name;
};

class pipo
{
foo *mf[8];
pipo()
{
for (unsigned int i = 0 ; i < 8; ++i)
{
ostringstream ost;
ost << "MY_FOO_" << i;
string name = ost.str();
mf = new foo (name.c_str());
}
}
};

Is it mandatory to do in such a way: using array of pointers on foo. Or
may I do it by using plain array of instances of foo?
In this case, I have no clue how to process to call the constructor with
the correct C like string as defined in the exemple...
Do you have any ideas how to proceed?

Something like this?

class pipo
{
foo mf[8];
pipo ():
????
{

}
};


There is no way to directly initialise an array if the array is
a non-static member of a class. Even with your 'array of pointers'
solution, the pointers are uninitialised in the beginning and then
are _assigned_ values in the constructor body. It's a shortcoming
of the language, nothing you or anybody else can do about it at
this time.

Victor
 
R

Ron Natalie

Sylvain said:
Is it mandatory to do in such a way: using array of pointers on foo. Or
may I do it by using plain array of instances of foo?

You can make a plain array:
foo mf[8];
or a dynmaic array
foo* mf = new foo[8];

In this case, I have no clue how to process to call the constructor with
the correct C like string as defined in the exemple...

Unfortunately, arrays are bastard type and initializing them with something
other than the default is frequently problematic.

In your case, I'd just allocate them all with
foo mf[8];
and then provide a "SetName(const char*)" method or something to provide
the name after they are constructed.
 
C

Cy Edmunds

Sylvain said:
Let's say I have the following code where a class 'pipo' has 8 instances
of foo:

class foo
{
foo ( const char * _name):
name = _name

what is this? ITYM name(_name)
{

}
private:
const char * name;
};

If you are planning to put class instances in a container it is wise to
include a default constructor:

class foo
{
public:
foo() : name(0) {}
foo ( const char * _name) : name(_name) {}
private:
const char * name;
};

Then to make an array:

foo mf[8];
for (int j = 0; j < 8; ++j)
mf[j] = foo("abc");
 
A

Alf P. Steinbach

Let's say I have the following code where a class 'pipo' has 8 instances
of foo:

class foo
{
foo ( const char * _name):
name = _name
{

}
private:
const char * name;
};


Class 'foo' has one very big assumption built-in:


* The lifetime of the character array that is the 'name' of a 'foo'
instance must include the lifetime of the 'foo' instance.


Otherwise the 'foo' instance may be referring to some deallocated
or otherwise cleaned-up memory, no longer a meaningful 'name'.



class pipo
{
foo *mf[8];
pipo()
{
for (unsigned int i = 0 ; i < 8; ++i)
{
ostringstream ost;
ost << "MY_FOO_" << i;
string name = ost.str();
mf = new foo (name.c_str());


Here the big 'foo' assumption is violated.

The 'foo' constructor is passed a pointer to the internal memory
of a 'string' instance.

Shortly after that 'string' instance goes out of scope and deallocates
the memory now pointed to by the 'foo' instance.


}
}
};

Is it mandatory to do in such a way: using array of pointers on foo.
No.


Or may I do it by using plain array of instances of foo?

Yes.

You can even use a 'std::vector', which is a good habit to get into
(even if it buys you nothing but overhead in this particular case).

In this case, I have no clue how to process to call the constructor with
the correct C like string as defined in the exemple...

In the 'foo' constructor you should _copy_ the string.

The easiest way to do that is to use a 'std::string' as member,
instead of a 'char const*'.

When you use 'std::string' you don't have to deal with allocation and
deallocation.
 
J

John Carson

Sylvain said:
Let's say I have the following code where a class 'pipo' has 8
instances of foo:

class foo
{
foo ( const char * _name):
name = _name
{

}
private:
const char * name;
};

class pipo
{
foo *mf[8];
pipo()
{
for (unsigned int i = 0 ; i < 8; ++i)
{
ostringstream ost;
ost << "MY_FOO_" << i;
string name = ost.str();
mf = new foo (name.c_str());
}
}
};

Is it mandatory to do in such a way: using array of pointers on foo.
Or may I do it by using plain array of instances of foo?
In this case, I have no clue how to process to call the constructor
with the correct C like string as defined in the exemple...
Do you have any ideas how to proceed?

Something like this?

class pipo
{
foo mf[8];
pipo ():
????
{

}
};


Regards

Sylvain



You could construct the array outside of pipo and have pipo just store a
pointer to it, e.g.,

#include <string>
using namespace std;

class foo
{
public:
foo (const char * _name) : name(_name)
{}
private:
string name;
};

class pipo
{
public:
foo *pmf;
pipo(foo *pfoo) : pmf(pfoo)
{}
};


int main()
{
foo scratch[8] = {"MY_FOO_0", "MY_FOO_1", "MY_FOO_2", "MY_FOO_3",
"MY_FOO_4", "MY_FOO_5", "MY_FOO_6", "MY_FOO_7"};
pipo p(scratch);
return 0;
}
 

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

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top