Skip to main content
GameDev.net gamedev.net
🔒 Locked

RAII Idiom

Started by Peter_APIIT Jul 3, 2009 at 4:34 AM 3 replies 1.4k views
Original Post
Peter_APIIT
Peter_APIIT
Hello to all, as you all know that RAII use to keep track of dynamic memory allocation in different hierarchy. For instance, i have class objectCreator and objectBase. Code: class objectCreator { private: objecrBase* pObject; }; class objectBase { }; Main.cpp Code: ObjectCreator obc; ObjectBase* ob = obc.Create(name); When obc goes out of scope, then the object created which pointed by ob also will get destroyed. I create objectCreator in main as object, then if objectCreator goes out of scope, then objectBase also will get delete in destructor of objectCreator. My question is i have class hierarchy such as usernameType(Base class) AdminUsernameType(Derived from usernameType) HRUsernameType(Derived from usernameType) Based on the username of login i dynamic create a derived type. Code: class usernameType { protected: login loginSessionObject; public: usernameType(); usernameType(const login&); ~usernameType(); static usernameType* FactoryMethodCreate(const login&); class AdministratorType : public usernameType { public: AdministratorType(); ~AdministratorType(); void ForwardCreator(); void setInstance(Human* ); }; Definition: Code: usernameType* usernameType::FactoryMethodCreate(const login& theLoginObject) { switch (theLoginObject.getUsername().at(0)) { case 'A': { return new AdministratorType(); break; } case 'H': { return new HumanResourceType(); break; } case 'S': { return new StaffType(); break; } default: { break; } } return new AdministratorType(); } How can i keep track which derived class get created and delete them accordingly when theAccountUser goes out of scope. Main.cpp Code: usernameType theAccountUser(loginSession); usernameType* theAccountType = theAccountUser.FactoryMethodCreate(loginSession); Thanks for your help. I really appreciated any advice. I won't ignore your advice. Is it possible for usernameType base class contains pointer to derived class which is create using new(). Thanks again.
bubu LV
bubu LV
You are creating something very strange.

RAII in C++ usually is implemented using constructor and destructor.
You have some generic class, that will delete pointer in destructor:
template <class T>class pointer{public:  pointer(T* p) : p(p) {}  ~pointer() { delete p; }  T* operator -> () { return p; }  // additional methods (copy constructor, assignement operator, ...)  // ..private:  T* p;};


And then you use it as wrapper for your pointers:
pointer<ObjectBase> ob = obc.Create(name);ob->SomeObjectBaseMethod();

And end of scope ob destructor will delete ObjectBase stored in it automatically.
No need to store ObjectBase pointer in obc object.

For full feature pointer class I showed, look into boost::shared_ptr or std::tr1::shared_ptr.
mrbastard
mrbastard
If I understand your question correctly, then simply making all the destructors in the hierarchy virtual will ensure that the correct destructor is called when you delete a base pointer.
[size="1"]
loufoque
loufoque
Quote:
Hello to all, as you all know that RAII use to keep track of dynamic memory allocation in different hierarchy.

Not at all, on the contrary.
RAII is scope-bound resource management.
Matt_D
Matt_D
I think your a little confused.

what you really need to go look at is reference counting, and resource management, not RAII. Boost (oh noes!) provides some simple reference counting code, reference counting, and resource management are pretty wide topics, with a number of different approaches.

RAII is typically used for scope based resource management, rather than actual reference counting resource management. (a little different to resource management).

for example, your game might have 4 references to a single texture, and you might want to delete the texture when no one references it anymore. this sort of thing isnt possible with simple RAII.

RAII is better suited to situations where you need something for a period of time, such as, spawn a thread for the duration of a function to do processing, or spawn a database connection for the duration of a function call.

RAII is certainly not a silver bullet :)

also be aware, that creating an array of objects will call the default constructor of the type of object in the array. so having an array of RAII objets will most likely cause a very large drop in performance upon allocation.
your never as good as they say you were, never as bad as they say you was.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.