passing argument to user defined type

Discussion in 'C++' started by vivekian, Mar 28, 2006.

  1. vivekian

    vivekian Guest

    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 ,
     
    vivekian, Mar 28, 2006
    #1
    1. Advertising

  2. vivekian

    Howard Guest

    "vivekian" <> wrote in message
    news:...

    >
    > 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
     
    Howard, Mar 28, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oodini
    Replies:
    1
    Views:
    1,779
    Keith Thompson
    Sep 27, 2005
  2. vivekian
    Replies:
    3
    Views:
    292
    Marcus Kwok
    Mar 28, 2006
  3. Replies:
    2
    Views:
    428
  4. Replies:
    1
    Views:
    452
    Sylvester Hesp
    May 16, 2007
  5. Ryan Dupuis
    Replies:
    8
    Views:
    1,261
    Eric Sosman
    Oct 2, 2013
Loading...

Share This Page