Creating objects during program.

Hi there,
i'm creating my simlpe program about cells in petri-dish.
I have class CELL with data memeber called energy a and some member functions.
I can define object from class CELL creature1.

Problem is, when cell wants to multiply because his energy is too high. So i want create another object CELL creature2 from class CELL but it
must be created automaticly in running program, and other object CELL creature3 etc...

Or how to create object with different values of data members during program ?

Could you please point me in right directions, because i'm lost. yesterday i learned object can't be made from variables.

i dont have actual code.


THX for help
yesterday i learned object can't be made from variables

That is not exactly true -- depending on semantics. The important concept is constructor.

However, this does it all:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <vector>
#include <iostream>

struct Foo {
  bool state_ = true;
  Foo( bool state ) : state_( state ) {}
  bool happy() const { return state_; }
  Foo split() { state_ = false; return Foo( false ); } 
};

int main() {
  std::vector<Foo> dish;
  dish.reserve( 4 );
  dish.emplace_back( true );
  dish.emplace_back( false );

  const auto old = dish.size();
  for ( decltype(dish.size()) cell = 0; cell < old; ++cell ) {
    if ( dish[ cell ].happy() ) dish.push_back( dish[ cell ].split() );
  }

  for ( const auto & x : dish ) {
    std::cout << x.happy() << '\n';
  }
  return 0;
}

Or does it? In order to understand the code above you probably have to read quite some tutorials and reference documentation. That is exactly what you should do anyway.
Topic archived. No new replies allowed.