You are here:

C++/vectors & adding objects???

Advertisement


Question
i have created a vector (vecorganism) that contains pointers to objects of class plant.. i was wondering how to add an object to the vector?

is this close...  vecorganism.push_back(plant)
or ..... vecorganism.push_back(new->plant)

????? THANKS ???????  

Answer
Hi Dean,
 Say the class is as follows:

class plant {
 // some stuffs
};

int main ()
{
 plant *obj = new plant;
// some thing
 vector <plant *> x;
x.push_back (obj);
}

if the vector was defined as vector <plant> then you would be writing it as x.push_back (&obj)

Regards,
D Rai

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Dharmender Rai

Expertise

I can answer general and system level C/C++ questions.

Experience

I have 5 years of experience in C++.

©2016 About.com. All rights reserved.