i am having difficulty understanding how this concept of creating new objects on the fly in a vector container seem to work, as seen in link at time 18:30. It seems the author constructs a vector of classes like so
class a;
typedef vector<a> b;
.
.
.
vector<b> c;
.
.
.
for (unsigned x=0;x<num_of_b_obj.size();x++){
c.push_back(b); //seems to be a way to dynamically create an array
//of objects of b in vector container c
}
does anyone know how this works, and is there any good documentation of this concept ?
The typical way of constructing an object is to instantiate the class, followed by the object name, like class_name object_name1
and class_name object_name2
, then to use the object members freely like object_name1.function_a
. The issue with using the vector container to construct the object seems to be, then how would one use, member functions of different object, if there are no visible ways of assigning a name to the constructed object ?
below is the real code ... showing the "push_back()" method seemingly creating new objects in a loop
class Neuron(){};
typedef vector<Neuron> Layer;
class Net {
public:
Net(vector<unsigned> &topology) // class constructor
void feedForward(const vector<double> &inputVals) {}; // passing
inputVals by reference rather than by value, because inputVals will be too
HUGE to pass by copying the value.
void backProp(const vector<double> &targetVals) {};
void getResults(vector<double> &resultVals) const {};
private:
vector<Layer> m_layers; // m_layer[layerNum][neuronNum]
};
Net::Net(vector<unsigned> &topology)
{
unsigned numLayers = topology.size();
for (unsigned layerNum=0;layerNum < numLayers;++layerNum){
m_layers.push_back(Layer()); // create new empty layer, to fill with the i'th neuron
for(unsigned neuronNum=0; neuronNum <= topology[layerNum];++neuronNum){
m_layers.back().push_back(Neuron()); // ".back()" to access the newly created layer, ".push_back()" to append the new neuron in the new layer
cout<< "Made a neuron foo !!" << endl;
}
}
}