So, lets say I have a vector, vector<Item*> vItem
and an Item class with a member function Item::setValue();
. Lets also say I populated that vector by using vItem.push_back(new Item)
.
Now, to access that member function, through the pointer in the vector, would vItem[0]->setValue("key");
. be valid? I assumed it was, however when I cout
the member variables using a getValue()
function, nothing new is returned, only the default values from my constructor.
Any ideas? I feel like it's a stupid mistake in my logic, but I'm not sure. Any help would be greatly appreciated.
EDITS:
setValue code:
void Item::setValue(string sValue)
{
m_sValue = sValue;
}
with the private variable:
string m_sValue;
getValue code:
string Item::getValue()
{
return m_sValue;
}
Simple version of the code:
void func2(vector<Item*>& vItem)
{
vItem.push_back(new Item);
vItem[0]->setValue("key");
}
int main(int argc, char * argv[])
{
vector<Item*> vItem;
func2(vItem);
cout << vItem[0]->getValue() << endl;
return 0;
}
I should also note: everything compiles just fine (g++ using c++0x standard) however, the setter just doesn't set.