## Class objects in vectors tutorial?

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Stucky101
Posts: 141
Joined: Tue Mar 27, 2007 4:17 am UTC
Location: Park City, Utah, USA, North America, Earth, Sol, Orion Arm, The Milky Way, Universe A

### Class objects in vectors tutorial?

I was wondering if someone could link me to a good tutorial on how to store class objects into vectors with C++. I have only been able to track down one, and I'm having trouble with it for some reason. Thanks in advance.
"The earth is the cradle of humankind, but one cannot live in the cradle forever."
~Konstantin Tsiolkovsky, 1895

jaap
Posts: 2094
Joined: Fri Jul 06, 2007 7:06 am UTC
Contact:

### Re: Class objects in vectors tutorial?

The main pitfall to be aware of is slicing. If you have a container for instances of class A, and you insert an instance of type B, derived from A, then it will be sliced back to an instance of A. You have to remember that the container only stores sizeof A bytes for each object, so any member data defined in class B but not A will get sliced away and be lost.
If you are in this situation, you are better off using a container of pointers to A, so that the container doesn't handle the storage of the objects themselves.

Stucky101
Posts: 141
Joined: Tue Mar 27, 2007 4:17 am UTC
Location: Park City, Utah, USA, North America, Earth, Sol, Orion Arm, The Milky Way, Universe A

### Re: Class objects in vectors tutorial?

jaap wrote:The main pitfall to be aware of is slicing. If you have a container for instances of class A, and you insert an instance of type B, derived from A, then it will be sliced back to an instance of A. You have to remember that the container only stores sizeof A bytes for each object, so any member data defined in class B but not A will get sliced away and be lost.
If you are in this situation, you are better off using a container of pointers to A, so that the container doesn't handle the storage of the objects themselves.

I don't think that'll be a problem for my current project, but thanks for the advice. If I did have a need for a vector of a subclass I think I would just use another vector separate from the superclass's vector anyways.
"The earth is the cradle of humankind, but one cannot live in the cradle forever."

~Konstantin Tsiolkovsky, 1895

LuminaryJanitor
Posts: 24
Joined: Tue Dec 15, 2009 12:23 pm UTC

### Re: Class objects in vectors tutorial?

I didn't think vectors of objects were that different from vectors of primitive types. What exactly are you having problems with?
An apple every eight hours will keep three doctors away

Stucky101
Posts: 141
Joined: Tue Mar 27, 2007 4:17 am UTC
Location: Park City, Utah, USA, North America, Earth, Sol, Orion Arm, The Milky Way, Universe A

### Re: Class objects in vectors tutorial?

LuminaryJanitor wrote:I didn't think vectors of objects were that different from vectors of primitive types. What exactly are you having problems with?

Well there are two main problems I'm having. For one thing the class that I'm using with the vector has a constructor with parameters and I don't understand how to set those parameters when I'm creating an object of the class using the .push_back() function. I mean, since the data type is a class its not like there is a single value/character that I could place in .push_back's parameters like I would usually do using the .push_back() function with int or char.

The second question is how would I call member functions of the class for a particular object? I'm thinking I would do something like this but I wanted to make sure.

Code: Select all

`vecExample[4].function();`

Where vecExample is the vector and function is the member function of the class.

Also, would it be possible to call the same function for every object within a vector by doing this?

Code: Select all

`vecExample.function();`
"The earth is the cradle of humankind, but one cannot live in the cradle forever."

~Konstantin Tsiolkovsky, 1895

Ephphatha
Posts: 625
Joined: Sat Sep 02, 2006 9:03 am UTC
Location: Bathurst, NSW, Australia

### Re: Class objects in vectors tutorial?

Stucky101 wrote:
LuminaryJanitor wrote:I didn't think vectors of objects were that different from vectors of primitive types. What exactly are you having problems with?

Well there are two main problems I'm having. For one thing the class that I'm using with the vector has a constructor with parameters and I don't understand how to set those parameters when I'm creating an object of the class using the .push_back() function. I mean, since the data type is a class its not like there is a single value/character that I could place in .push_back's parameters like I would usually do using the .push_back() function with int or char.

If you define a copy constructor for your class you can call:

Code: Select all

`vecExample.push_back(MyClass(param1, param2, param3));`

The copy constructor would look something like this:

Code: Select all

`MyClass{public:  MyClass(const MyClass &rhs);}MyClass::MyClass(const MyClass &rhs){  this->member1 = rhs.member1;  this->member2 = rhs.member2;  this->anothermember = rhs.anothermember;  //And you get the gist...}`

Stucky101 wrote:The second question is how would I call member functions of the class for a particular object? I'm thinking I would do something like this but I wanted to make sure.

Code: Select all

`vecExample[4].function();`

Where vecExample is the vector and function is the member function of the class.

Yes.

Stucky101 wrote:Also, would it be possible to call the same function for every object within a vector by doing this?

Code: Select all

`vecExample.function();`

No. However, you can loop through the vector (either using a for loop or an iterator) and it's pretty simple. (what you're trying to do there is call the function() method of the vector class.)
I'm not lazy, I'm just getting in early for Christmas is all...

LuminaryJanitor
Posts: 24
Joined: Tue Dec 15, 2009 12:23 pm UTC

### Re: Class objects in vectors tutorial?

Ephphatha wrote:
Stucky101 wrote:
LuminaryJanitor wrote:If you define a copy constructor for your class you can call:

Code: Select all

`vecExample.push_back(MyClass(param1, param2, param3));`

The copy constructor would look something like this:

Code: Select all

`MyClass{public:  MyClass(const MyClass &rhs);}MyClass::MyClass(const MyClass &rhs){  this->member1 = rhs.member1;  this->member2 = rhs.member2;  this->anothermember = rhs.anothermember;  //And you get the gist...}`

Though if your class is that simple (i.e. no dynamic memory, resource handles, or anything else that needs to be freed / finalised in the destructor) then you should be fine with the automatically generated shallow copy assignment.

Out of interest, what is the scope of the anonymous instance created by the caller?
An apple every eight hours will keep three doctors away

Stucky101
Posts: 141
Joined: Tue Mar 27, 2007 4:17 am UTC
Location: Park City, Utah, USA, North America, Earth, Sol, Orion Arm, The Milky Way, Universe A

### Re: Class objects in vectors tutorial?

I think I've figured out my problem. I'm just using a temporary object to store data in the vector and I think it's working properly. Thanks for the help guys.
"The earth is the cradle of humankind, but one cannot live in the cradle forever."

~Konstantin Tsiolkovsky, 1895

MHD
Posts: 630
Joined: Fri Mar 20, 2009 8:21 pm UTC
Location: Denmark

### Re: Class objects in vectors tutorial?

LuminaryJanitor wrote:Out of interest, what is the scope of the anonymous instance created by the caller?

I presume you mean:

Code: Select all

`cout << string("Hello"); // The scoping of, as an example, this tempoary string object?`

It's scope is until the next semi colon.
EvanED wrote:be aware that when most people say "regular expression" they really mean "something that is almost, but not quite, entirely unlike a regular expression"

Axidos
Posts: 167
Joined: Tue Jan 20, 2009 12:02 pm UTC
Location: trapped in a profile factory please send help

### Re: Class objects in vectors tutorial?

MHD wrote:
LuminaryJanitor wrote:Out of interest, what is the scope of the anonymous instance created by the caller?

I presume you mean:

Code: Select all

`cout << string("Hello"); // The scoping of, as an example, this tempoary string object?`

It's scope is until the next semi colon.

Next, in that case, being the one at the end of that very line?

Return to “Coding”

### Who is online

Users browsing this forum: No registered users and 5 guests