By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,128 Members | 926 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,128 IT Pros & Developers. It's quick & easy.

Vectors with own classes

P: n/a
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
Jan 3 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Ben Wheare <z_news*R3M0V3*@bwgames*r3mov3*.netwrote in news:459bbe88$0
$3*************@news.zen.co.uk:
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.
Jan 3 '07 #2

P: n/a
"Ben Wheare" <z_news*R3M0V3*@bwgames*r3mov3*.netwrote in message
news:45***********************@news.zen.co.uk...
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<stringsdata_;
};
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<messageswords;
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.
Jan 4 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.