Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Vectors with own classes

Reply
Thread Tools

Vectors with own classes

 
 
Ben Wheare
Guest
Posts: n/a
 
      01-03-2007
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
 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      01-03-2007
Ben Wheare <z_news*R3M0V3*@bwgames*r3mov3*.net> wrote in news:459bbe88$0
$32020$(E-Mail Removed):

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


 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      01-04-2007
"Ben Wheare" <z_news*R3M0V3*@bwgames*r3mov3*.net> wrote in message
news:459bbe88$0$32020$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using own classloader inside J2EE to load and unload own classes. Stefan Siegl Java 1 07-02-2013 05:05 AM
c++ primer statement about vectors containing vectors pauldepstein@att.net C++ 3 03-26-2008 06:22 PM
Using Vectors in classes - public access andrewmorrey@aol.com C++ 5 05-11-2007 07:05 PM
Ruby, SWIG and C++: how to properly wrap vector of vectors of doubles (2D vectors)? Ruby 0 09-14-2005 05:47 PM
classes, pointers, vectors, and memory allocation mosfets@gmail.com C++ 2 02-06-2005 07:51 PM



Advertisments