Constructor Question

S

- Steve -

I need to have a constructor that is passed a integer number that defines
the size of an array in the private area of the Class. Is that possible?

For example in main() the user is asked for the number of lanes. Then when
the object is declared after that it is passed that integer, which then
creates an array of pointers.
 
G

Gianni Mariani

- Steve - said:
I need to have a constructor that is passed a integer number that defines
the size of an array in the private area of the Class. Is that possible?

For example in main() the user is asked for the number of lanes. Then when
the object is declared after that it is passed that integer, which then
creates an array of pointers.

you could use a std::vector


i.e.

class Lanes
{

std::vector< Lane * > m_lane_array;

public:
Lanes( int size )
: m_lane_array( size )
{
}
};
 
T

TR

- Steve - said:
I need to have a constructor that is passed a integer number that defines
the size of an array in the private area of the Class. Is that possible?

Yes, constructors can take parameters like normal functions. Use an
std::vector in your private area and call .resize() in your constructor to
set the array size. In fact you don't even need to resize it, as it'll grow
as data is added. Get hold of an elementary C++ reference to learn more.
 
D

dwrayment

vectors are fine if you like std, personally i use std sparingly

all ya have to do is use new

Object *_array = new Objects[t];

or

typedef Object *PObject;
Object **_array = new PObject[t];


depending on what exactly your looking to do
 
J

Josephine Schafer

- Steve - said:
I need to have a constructor that is passed a integer number that defines
the size of an array in the private area of the Class. Is that possible?

For example in main() the user is asked for the number of lanes. Then when
the object is declared after that it is passed that integer, which then
creates an array of pointers.

You should declare your constructor to be explicit because the parameter
passed to the
constructor determines the object's configuration. This way you prevent any
implicit
conversions that may otherwise occur inadvertently.

With best wishes,
J.Schafer
 
J

John Harrison

TR said:
possible?

Yes, constructors can take parameters like normal functions. Use an
std::vector in your private area and call .resize() in your constructor to
set the array size. In fact you don't even need to resize it, as it'll grow
as data is added. Get hold of an elementary C++ reference to learn more.

Err, you can't resize an array. I think you are thinking of a vector.

john
 
J

John Harrison

- Steve - said:
I need to have a constructor that is passed a integer number that defines
the size of an array in the private area of the Class. Is that possible?

For example in main() the user is asked for the number of lanes. Then when
the object is declared after that it is passed that integer, which then
creates an array of pointers.

Don't use an array, use a vector. Something like this

#include <vector>

class LaneVector
{
public:
LaneVector(int num_lanes) : lanes(num_lanes)
{
}
private:
std::vector<Lane> lanes;
};

You should really learn about vectors, make your life much easier.

john
 
K

Karl Heinz Buchegger

dwrayment said:
vectors are fine if you like std, personally i use std sparingly

all ya have to do is use new

Object *_array = new Objects[t];

.... and by thus have opened the 'Hey why use something
simple when there is a more complicate way' season.

By doing the memory management by yourself, your class
suddenly also needs: a destructor, an assignment operator,
a copy constructor. And all of them have, of course, to be
written correctly!

So why would you want to do this, when vector is available
and saves you from all this possible pitfalls?
 
J

John Harrison

Peter Gregory said:
That's why s/he said quite clearly "Use an STD::vector...".

pete

Hmm, shortly followed by "call .resize() in your constructor to set the
array size", but maybe I should learn to read.

Also the statement "In fact you don't even need to resize it, as it'll grow
as data is added." is misleading at best.

john
 
D

dwrayment

if you think thats complicated im sorry for you. to me its
not that complicated to add copy costructors and a destructor.
if fact its simpler and less bloated than having to include std.
but thats just me.

it gives me piece of mind knowing that im not using some other persons code.
ive even made my own classes for strings and several other data types, for
the same reason and because to otfen std does not have a function that id
like, or has functions i could care less about.

to me std is a lazy mans way to program. you wont become a better
programmer that way. while there are some parts of std that i do find
useful <algotyhms.h> for example. i tend to stay away from using the
containers because they post no advantage to me. i can write a more
efficient, more effective class than std with a little bit of effort.

To each his own. Im not gonna ask you why you choose to use stl thats your
choice, but mine however is not (most of the time).



Karl Heinz Buchegger said:
vectors are fine if you like std, personally i use std sparingly

all ya have to do is use new

Object *_array = new Objects[t];

... and by thus have opened the 'Hey why use something
simple when there is a more complicate way' season.

By doing the memory management by yourself, your class
suddenly also needs: a destructor, an assignment operator,
a copy constructor. And all of them have, of course, to be
written correctly!

So why would you want to do this, when vector is available
and saves you from all this possible pitfalls?
 
J

John Harrison

dwrayment said:
if you think thats complicated im sorry for you. to me its
not that complicated to add copy costructors and a destructor.
if fact its simpler and less bloated than having to include std.
but thats just me.

It's amazing how difficult it can be to write exception safe code. Its not a
skill I would think more than 1% of programmers have (I think that is being
generous). Maybe you are in that 1%, or maybe you don't care about exception
safety.

john
 
T

TR

John Harrison said:
Hmm, shortly followed by "call .resize() in your constructor to set the
array size", but maybe I should learn to read.

Also the statement "In fact you don't even need to resize it, as it'll grow
as data is added." is misleading at best.

Stop it.
 
K

Karl Heinz Buchegger

dwrayment said:
if you think thats complicated im sorry for you.

????

I quote you:
vectors are fine if you like std, personally i use std sparingly

all ya have to do is use new

Object *_array = new Objects[t];

That's completely misleading. Especially for newbies.
It is not true, that 'all you have to do is ...'
You also have to do, and you also have to do ...
to me its
not that complicated to add copy costructors and a destructor.

Fine for you.
if fact its simpler and less bloated than having to include std.
but thats just me.

You forgot about the assignment operator :)
Also: Can you write a completely correct copy constructor and/or
assignment operator which can also handle exceptions correctly?

It's not so complicated, but I have to admit I need a few minutes
to write them, and I have written lots of them in the past.
it gives me piece of mind knowing that im not using some other persons code.

So you don't use printf or the stream objects and instead write one
on your own. Oh, and of course you don't use string objects. I wonder
if you buy pens in a shop or do you make your own ink and use goose feathers.


If you want to do memory management on your own, this is your thing.
I won't comment on that. But if you start recommending this to newbies,
I will comment on that.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,985
Messages
2,570,198
Members
46,766
Latest member
rignpype

Latest Threads

Top