473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Vectors with own classes

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
2 1574
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jay | last post by:
Hi, I'm a C++="C with classes" kind of guy and was wondering if anyone felt like making the case that STL Vectors are in any way better than my home-brewed, memmove()-heavy dynamic array class...
3
by: ataru | last post by:
My boss has some beef or other with vectors - he says that they've given him trouble over the years, and so he uses some template classes he wrote 15 years ago. My question is, is the standard...
10
by: Michael Aramini | last post by:
I need to represent 1D and 2D arrays of numeric or bool types in a C++ program. The sizes of the arrays in my intended application are dynamic in the sense that they are not known at compile time,...
2
by: mosfets | last post by:
Hi, I'm having a little trouble figuring out the difference in terms of memory allocation between: class person_info; class A { private:
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
5
by: pallav | last post by:
I have a map like this: typedef boost::shared_ptr<NodeNodePtr; typedef std::vector<NodePtrNodeVecPtr; typedef std::map<std::string, NodePtrNodeMap; typedef std::map<std:string,...
5
by: andrewmorrey | last post by:
Hello, I've got a VC++ project containing multiple classes and a main function. In one of the class functions, it reads from a text file and places the data into a vector; //...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
10
by: Ruben | last post by:
Hello I'm working out an exercise with vectors and classes. I have a vector defined in a constructor which is just failing to compile with a systax error complaint by gcc: The code is here: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.