Click to See Complete Forum and Search --> : Changing this


April 15th, 1999, 12:47 PM
Starting with a base class object and calling a method with a filename as an input. The idea is to find out what's inside the file and create an object of that type (the object inherits the base class). My problem is I want this of the base class object to become this of the specific class.

Michael Decker
April 15th, 1999, 02:53 PM
What about creating a static function that returns an allocated pointer to the base class? Within this function you would call new using the correct derived class after determining the file contents.

For example:

class CBase
{
public:
CBase();
static CBase *Create(const CString &strFilename);
};

CBase *CBase::Create(const CString &strFilename)
{
CBase *pRetVal = NULL;

// determine file type
...

// allocate object of correct derived class
pRetVal = new CDerived;

// return allocated pointer to object
return(pRetVal);
}

April 16th, 1999, 11:16 AM
It work !!! Thank you very much.

Michael Decker
April 16th, 1999, 11:41 AM
My pleasure!