RAII Idiom
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.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]