473,320 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

calling cunstroctor from CString object

Hi
can i call a constructor of a class, if the name is stored in a CString
Object
e.g.

class CBaseClass
{
int x;
};

class CDerClass1: public CBaseClass
{
int y;
};

class CDerClass2: public CBaseClass
{
int z;
};

CString strClassName;
// = L"CMyClass";//using unicode charset....

//main1//i know this
CBaseClass* pPtr;
------
----
----------

if(some condition)
{
strClassName = L"CDerClass1"
pPtr = new CDerClass1;
}
else
{
strClassName = L"CDerClass2"
pPtr = new CDerClass2;
}
//for 2 classes this approach is fine but
//what if i have more than 100 classes derived from the same class
//n at runtime i want to allocate the memory for them.....

//main2//can it be done????
CBaseClass* pPtr;
------
----
----------

if(some condition)
{
strClassName = L"CDerClass1"
}
else
{
strClassName = L"CDerClass2"
}
//now i want to allocate the memory for the class whose name is stored
in the strClassName
pPtr = new "some way to call the constructor whose name is stored in
string.....";
plz help me i m tired of writting this if else statements........

Jan 11 '07 #1
1 911
mu*********@gmail.com wrote:
Hi
can i call a constructor of a class, if the name is stored in a CString
Object
e.g.
I think what you're asking is called a factory. You have to add a thin
infrastructure to your classes. First you need a function that creates a
particular instance, which I will call FactoryCreator:

typedef CBaseClass* (*FactoryCreatorPtr)();

You have to implement this function for each actual class in your hierarchy:

CBaseClass* CDerClass1Creator() { return new CDerClass1; }
CBaseClass* CDerClass2Creator() { return new CDerClass2; }

Then you have to implement a registry, where you will associate class
names with FactoryCreatorPtr pointers. For this purpose I'm going to use
an STL map:

typedef std::map<std::string, FactoryCreatorPtrFactoryRegistryMap;
FactoryRegistryMap registry;

You have to dynamically register every creator that you want to support:

registry["CDerClass1"] = CDerClass1Creator;
registry["CDerClass2"] = CDerClass2Creator;

The registration doesn't need to be centralized. The only requirement is
that it gets done before you actually create instances.

Now you're ready to create an instance by name:

CBaseClass* CreateClassByName(const FactoryRegistryMap& registry, const
std::string& classname)
{
FactoryRegistryMap::const_iterator found = registry.find(classname);
return (found == registry.end()) ? 0 : (found->second)();
}

If you call this with an unknown name, the function will return 0. If
the classname is already registered, CreateClassByName creates a new
instance, which you're ready to use:

CBaseClass* instance = CreateClassByName("CDerClass1");
instance->[...];
delete instance;

I hope this gets you started.

You can make each class auto-register itself using the following scheme
(put this in each class' .cpp file):

static bool AutoRegister()
{
registry["CDerClass1"] = CDerClass1Creator;
return true;
}
static bool auto_register = AutoRegister();

This way you just need to add the .cpp/.obj/.lib to any of your
projects, and it will auto register with your factory infrastructure,
without having to worry about manual registration. Just make sure there
is only one registry in your entire project (preferably a singleton object).

I recommend that you google for the factory pattern, and you'll find
several alternative implementations.

Tom
Jan 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Fausto Lopez | last post by:
I'm getting the following error: 'strlen' : cannot convert parameter 1 from 'class CString' to 'const char *' when I try to compile the following code: HRESULT AnsiToUnicode(CString pszA,...
5
by: Tim Wong | last post by:
All: I am trying to convert a CString value to an unsigned char array. I found some code online that will allow me to compile, but when I try to print out...i get a whole mess. /*Begin Code*/...
1
by: tings | last post by:
Writing my own CString class, how to implement a member method so that the class' other members can return a CString like: CString CString::WhateverMethod(...) { char ACharString; ... return...
0
by: Nicky | last post by:
hi,all I use a c++ (build in vc6) dll in c#, do I need to care about the calling convention? Cause it always gives me a unexpected calling! I used it in c# like this: public static extern...
7
by: Klaus Bonadt | last post by:
I have an existing VC6 application using the MFC. I am able to pass CString and other parameters from such a VC6 dll to an unmanaged MFC dll (compiled in Visual Studio .NET). Now I want to use...
4
by: Steven Livingstone | last post by:
If I was using C# and wanted to call the printf function what would I do? Specifically i want to convert a string to octal and as there is no facility for doing this in C# i figured i'd look at...
2
by: Patrik via DotNetMonster.com | last post by:
Hi, I have simple managed C++ lib library with variable declaration of type CString. Another C# windows application reference this C++ library. When I try call method in C++ library from C#,...
5
by: Igor Natachanny | last post by:
Wich way I can to take the BYTE*-pointer to a string of characters that contain into CString-object?
0
by: skysurf | last post by:
Hello, I have been placed on a new project involving development for Windows CE using Visual C++. I have to send an XML file via a SSL connection to a server followed by receiving a corresponding...
3
by: NightCrawler | last post by:
Hi can i call a constructor of a class, if the name is stored in a CString Object e.g. class CBaseClass { int x; };
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.