0

how do i create a new instance of a class at runtime?

ok say i have a window open and every time i click inside the windows it puts a small image ( which is defined in the class ) at the location of the mouse pointer. But how can i make each image a new instance of the class. I can do it easy at compile time
class A;
class B;
class C;

but this only works if i know how many instance's that im going to have

any help will be greatly appreciated

3
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by Narue
0

Since you don't know how many objects there will be, a dynamically sized collection is probably the way to go:

#include <ctime>
#include <ios>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

int main()
{
  std::vector<std::string> v;
  char ch;

  while (std::cin.get(ch)) {
    std::time_t now = time(0);

    v.push_back(std::ctime(&now));

    if (ch != '\n')
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  }

  std::vector<std::string>::const_iterator it = v.begin();
  std::vector<std::string>::const_iterator end = v.end();

  while (it != end)
    std::cout<< *it++ <<'\n';
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.