How Do I Store Objects in a Object in a Vector? (C++)

I hope this is not a stupid question. Basically I would like to access a string stored in a Class (Statement is the name I am using) in a vector of type Statement. Basically I am trying to store objects in a dynamic hierarchy of objects. Types.cpp:

#include<iostream>
#include<fstream>
#include <string>
#include <vector>
using namespace std;

class Statement{

public:
vector<string> Inner_String;
vector<Statement> Inner_Statement;
string contents;

void set_contents (string);
string get_contents(){ return contents;}
void new_string(string);
string get_string(int v){return Inner_String[v];}
void new_Inner_Statement(Statement);
Statement get_Inner_Statement(int v){return Inner_Statement[v];}
};

void Statement::set_contents(string s){
contents = s;
}

void Statement::new_string(string s){
Inner_String.push_back(s);

}
void Statement::new_Inner_Statement(Statement s){
Inner_Statement.push_back(s);
}

Main method:

#include <iostream>
#include "FileIO.h"
#include "Types.h"

using namespace std;
int main()
{
Statement test;
test.new_Inner_Statement(Statement());
Statement a = test.get_Inner_Statement(0);
a.set_contents("words");
cout << a.get_contents();
test.get_Inner_Statement(0).set_contents("string");
cout << test.get_Inner_Statement(0).get_contents();
return 0;
}

What happens is cout << a.get_contents() returns its string while cout << test.get_Inner_Statement(0).get_contents() does not.

Answers


Look at this piece of code:

test.get_Inner_Statement(0).set_contents("string");
^^^^^^^^^^^^^^^^^^^^^^^^^^^

It calls this function:

Statement get_Inner_Statement(int v)

which returns a copy object (temporary) of type statement. On this object, you calls set_contents function, at which cease to exists at the end of the call.

Then, you call:

test.get_Inner_Statement(0).get_contents();

that creates a new temporary, from the unchanged statement, and try to get its contents.


Need Your Help

HTML <script> async Attribute in Magento

javascript asynchronous prototype magento-1.9.1

I would like to try to insert the "async" attribute in Prototype JavaScript script tag in Magento 1.9.1:

Equivalent for str.join in pandas

python pandas

Is there a clean way to concatenate an arbitrary number of string series similar to the ' '.join idiom? If I know the columns I want in advance I can do