Vectors with own classes

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
 
A

Andre Kostur

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.
 
J

Jim Langston

Ben Wheare said:
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:
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 said:
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.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top