Vectors with own classes

Discussion in 'C++' started by Ben Wheare, Jan 3, 2007.

  1. Ben Wheare

    Ben Wheare Guest

    Hiya,

    I'm fairly new to C++, and trying to figure out how to do the following:

    I have a class, strings, which has a private char[8] array. To add to
    this array, I call the member function add_array(int), with an int,
    which populates the array (in this case with the binary equivalent of
    that integer).

    I want to make another class, messages, which encapsulates the strings
    class. Ideally, I'd like messages to contain a vector of 'strings', so
    that I can then use push_back(int) etc to add another word to the end
    of the messages vector.

    I can create a vector of words it seems (at least it compiles fine), but
    I'm not sure how to pass the int from the push_back vector function to
    the add_array function in the strings class.

    Related to this, I also want to be able to access the public member
    functions of each instance of the strings classes.

    I'm guessing it involves having a vector of pointers to multiple
    instances of the strings class, where push_back simply creates a pointer
    to another instance of strings class, but I'm not totally sure on how to
    generate it in terms of vectors.

    I'm thinking along the lines of
    ptr = new strings();
    vector.push_back(ptr);

    but how do I address member functions etc? This also doesn't allow me to
    simply do push_back(int), but I'm not even sure thats possible.

    Thanks,

    Ben
     
    Ben Wheare, Jan 3, 2007
    #1
    1. Advertising

  2. Ben Wheare

    Andre Kostur Guest

    Ben Wheare <z_news*R3M0V3*@bwgames*r3mov3*.net> wrote in news:459bbe88$0
    $32020$:

    > Hiya,
    >
    > I'm fairly new to C++, and trying to figure out how to do the

    following:
    >
    > I have a class, strings, which has a private char[8] array. To add to
    > this array, I call the member function add_array(int), with an int,
    > which populates the array (in this case with the binary equivalent of
    > that integer).


    As in:

    void add_array(int value)
    {
    memcpy(&privateArray[0], &value, sizeof(value));
    }

    ?

    Question: how do you _know_ that sizeof(int) <= 8? Theoretically an int
    could be 16 bytes. (Granted, I can't think of a platform where this is
    true yet... but it's not illegal.)

    >
    > I want to make another class, messages, which encapsulates the strings
    > class. Ideally, I'd like messages to contain a vector of 'strings', so
    > that I can then use push_back(int) etc to add another word to the end
    > of the messages vector.


    Sure. Provide a constructor for the messages object that takes an int as
    a parameter:

    messages::messages(int value)
    {
    memcpy(&privateArray[0], &value, sizeof(value));
    }

    > I can create a vector of words it seems (at least it compiles fine),

    but
    > I'm not sure how to pass the int from the push_back vector function to
    > the add_array function in the strings class.
    >
    > Related to this, I also want to be able to access the public member
    > functions of each instance of the strings classes.


    You would need to provide some sort of accessor functions on the
    "messages" class to obtain a reference (or perhaps pointer...) to a
    specific "strings" instance to work with.

    > I'm guessing it involves having a vector of pointers to multiple
    > instances of the strings class, where push_back simply creates a

    pointer
    > to another instance of strings class, but I'm not totally sure on how

    to
    > generate it in terms of vectors.
    >
    > I'm thinking along the lines of
    > ptr = new strings();
    > vector.push_back(ptr);


    Well, that's one way. Recall that std::vector (and all of the STL
    containers) are value-based containers. So when one puts an item into a
    std::vector, a _copy_ of the object is taken and put into the vector. So
    given the new constructor I've supplied above:

    void messages::add_message(int value)
    {
    stringsVec.push_back(value);
    }

    This should cause a temporary "strings" instance to be created, and
    passed into the push_back function.

    > but how do I address member functions etc? This also doesn't allow me

    to
    > simply do push_back(int), but I'm not even sure thats possible.
     
    Andre Kostur, Jan 3, 2007
    #2
    1. Advertising

  3. Ben Wheare

    Jim Langston Guest

    "Ben Wheare" <z_news*R3M0V3*@bwgames*r3mov3*.net> wrote in message
    news:459bbe88$0$32020$...
    > Hiya,
    >
    > I'm fairly new to C++, and trying to figure out how to do the following:
    >
    > I have a class, strings, which has a private char[8] array. To add to this
    > array, I call the member function add_array(int), with an int, which
    > populates the array (in this case with the binary equivalent of that
    > integer).


    something like: ?
    class strings
    {
    public:
    add_array(int val) { int* dp = reinterpret_cast<int*>( data_ ); *dp =
    val; }
    private:
    char data_[8];
    };

    "add to this array" is rather confusing. I'm not sure what you mean.

    > I want to make another class, messages, which encapsulates the strings
    > class. Ideally, I'd like messages to contain a vector of 'strings', so
    > that I can then use push_back(int) etc to add another word to the end of
    > the messages vector.


    Little confused here. Like this: ?
    class messages
    {
    private:
    std::vector<strings> data_;
    };

    > I can create a vector of words it seems (at least it compiles fine), but
    > I'm not sure how to pass the int from the push_back vector function to the
    > add_array function in the strings class.


    Well, you told me about strings, and you told me about messages, but where
    is words? Or is words messages? Do you mean:
    std::vector<messages> words;

    > Related to this, I also want to be able to access the public member
    > functions of each instance of the strings classes.
    >
    > I'm guessing it involves having a vector of pointers to multiple instances
    > of the strings class, where push_back simply creates a pointer to another
    > instance of strings class, but I'm not totally sure on how to generate it
    > in terms of vectors.


    I"m not sure what you're talking about here.

    > I'm thinking along the lines of
    > ptr = new strings();
    > vector.push_back(ptr);
    >
    > but how do I address member functions etc? This also doesn't allow me to
    > simply do push_back(int), but I'm not even sure thats possible.
    >
    > Thanks,
    >
    > Ben


    Your question seems to be more along the lines of, hey, once I add somethign
    to a vector, how the heck do I get back to it? One way I've done it is
    MyVector[MyVector.size() - 1]; to get the last element, the one I just
    added.

    Can you post some actual code so I can figure out what you are actually
    trying to achieve? It could be as simple as iterators, or class
    constructors, or something totally unrelated. I"m not sure I understand
    your problem.
     
    Jim Langston, Jan 4, 2007
    #3
    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. Stefan Siegl
    Replies:
    1
    Views:
    775
  2. Saverio M.
    Replies:
    0
    Views:
    519
    Saverio M.
    Jul 3, 2006
  3. Replies:
    3
    Views:
    700
    Shadowman
    Mar 26, 2008
  4. Guest
    Replies:
    0
    Views:
    444
    Guest
    Sep 14, 2005
  5. David Filmer
    Replies:
    17
    Views:
    268
    J. Romano
    Aug 18, 2004
Loading...

Share This Page