passing argument to user defined type

V

vivekian

Hi,

trying to compile the following piece of code

class PeerList
{
public :
....
PeerList ( int noPieces )
{
// using noPieces to dynamically allocate memory for an
array.
}
~PeerList ()
{
....
}
};

Now trying to make a list of objects of type PeerList so trying the
following :

std::list <PeerList> pl ;

But , how do i pass the argument -- 'int noPieces' to this declaration
?

thanks in advance ,
 
H

Howard

Now trying to make a list of objects of type PeerList so trying the
following :

std::list <PeerList> pl ;

But , how do i pass the argument -- 'int noPieces' to this declaration
?

thanks in advance ,

Andy gave you a couple good ways, but I wanted to clarify something for you.
Your code above defines a container (list) for a number of PeerList objects.
It does not yet contain any PeerList objects. This differs from creating a
raw array of PeerList objects, which _does_ create the objects themselves as
well as the container (if you can even call an array a "container").

To put an actual PeerList object in your list, first you create the object,
passing it any constructor parameters you need, and then add it to the list
(e.g., by calling push_back). (Andy also showed a way to do that in one
step which is pretty neat, I think.)

That's why you couldn't figure out how to pass the parameter... because you
don't! :)

Hope this helps...

-Howard
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top