B
Ben Wheare
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
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