Referencing in a Function

How would I properly write the following:

1
2
3
4
void instance_create(baseClass& nameOfDerivedClass) {
  nameOfDerivedClass newInstance;
  instances.push_back(nameOfDerivedClass);
}


Basically, I am trying to create an instance of the specified argument through this function.
Your trying to implement the factory pattern?
http://en.wikipedia.org/wiki/Factory_method_pattern
1
2
3
4
5
6
7
template<typename T> 
void instance_create() {
    instances.push_back( T() );
}

// ...
instance_create<Foo>();


Doesn't seem very useful... what are you trying to do?
Zaita's suggested method seems fairly simple, however, once the project reaches a large scale it may seem too large to manage.

jsmith, I am trying to write an object manager.
So to create the class that I choose it would be like so:
1
2
3
4
5
6
7
template <typename T>
void instance_create() {
    new T();
    instances.push_back( T() );
}

instance_create <anObject>();
Factories are not hard to manage at all, that's why they're a known formal design pattern.

Your object manager should only manage related objects. You may need multiple object managers depending on how you want to act on them.
I think what you may be after is the Abstract Factory pattern (rather than just the Factory Method pattern).

It's hard to see your intent without a greater context, so you may also want to check out the Singleton pattern. It provides two benefits: control of the number of instances of a class and it provides global access to the instance(s).
I am writing an object manager.

1
2
3
4
5
6
7
8
9
class ObjectManager {
  public:
    void instance_create( <name_of_argument> );
    std::vector <baseObject> instances;
};

void ObjectManager::instance_create( <name_of_object> ) {
  instances.push_back( new <name_of_object> );
}


How would I properly implement this?
You could also use a static member in your class that holds a reference to the current objects. Typically, an STL map<UNIQUE_OBJ_ID, BaseClass *> would be used. That way a static getInstance( UNIQUE_OBJ_ID id ) method could look up the appropriate object.

How do you intend to use your class? Storing them in a vector means that either you intend to operate on all of the objects at once or you are storing an index (order of creation) somewhere else for later retrieval of an object.

From the snippet that you provided, I would guess that you need to create a base class, derive your classes from that, and store base class pointers/references in the container. You might also want to restrict the creation of your derived classes so that they must always be in your container of objects. The Singleton pattern with a static create method that returns a pointer/reference might be helpful.
So I still say

1
2
3
4
5
6
7
8
9
10
11
template< typename Base >
class ObjectManager {
  private:
     std::vector< Base* > instances;

  public:
    template< typename Derived >
    void instance_create() {
         instances.push_back( new Derived() );
    }
};


I am writing an object manager that manages a base object which has derived objects that perform things differently.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class baseObject {
  public:
    virtual void Step();
};

class obj_Ball: public baseObject {
  public:
    void Step() {
      //Perform ball movement
    }
};

class obj_test: public baseObject {
  void Step() {
    //Increase a variable by one and cout it.
}


The object manager would scroll through each instance in the instances list and perform their cooresponding Step method.

1
2
3
for ( int i=0; i < instances.size(); i++ ) {
  instances[i].Step();
}


Also, upon the instance_create.... the instance would also perform their cooresponding constructors and deconstructors upon deletion.
Topic archived. No new replies allowed.