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

Recommended Answers

All 2 Replies

allocate it

class MyClass
{
   // blabla
};

int main()
{
    MyClass* pClass = new MyClass;
}

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';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.