By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,906 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,906 IT Pros & Developers. It's quick & easy.

How to instantiate a class by a string?

P: 14
There are three files in this project.
Expand|Select|Wrap|Line Numbers
  1. // hello.cpp content
  2. main()
  3. {
  4.   // read a txt file to get the class name;
  5.    char* name = read("classlist.txt");
  6. // I need call car.drive() and ship.drive() here, but I don't know the 
  7. // class names are car and ship or any other name
  8. // what can I do here?   
  9. // If I know the class is car, I can instantiate it,
  10.  
  11. car  car1;
  12. car1.drive();
  13. ship ship1;
  14. ship1.drive();
  15.  
  16. }
  17.  
  18. // classlist.txt content
  19. // main() does not know how many classes inside this file, but all 
  20. // classes implement the same drive() function
  21. car
  22. ship
  23.  
  24. // implement.cpp content
  25. class car{
  26. int drive();
  27.  
  28. class ship{
  29. int drive();
  30. }
May 30 '07 #1
Share this Question
Share on Google+
8 Replies


ilikepython
Expert 100+
P: 844
There are three files in this project.
// hello.cpp content
main()
{
// read a txt file to get the class name;
char* name = read("classlist.txt");
// I need call car.drive() and ship.drive() here, but I don't know the
// class names are car and ship or any other name
// what can I do here?
// If I know the class is car, I can instantiate it,

car car1;
car1.drive();
ship ship1;
ship1.drive();

}

// classlist.txt content
// main() does not know how many classes inside this file, but all
// classes implement the same drive() function
car
ship

// implement.cpp content
class car{
int drive();
}

class ship{
int drive();
}
I'm just wondering, why would you want to do that? You can have a base class with name member and then use that, if that's what you need:
Expand|Select|Wrap|Line Numbers
  1. class Thing{
  2.     string name;
  3.     public:
  4.         Thing(string);
  5.         int drive();
  6. };
  7. Thing::Thing(string passedName){
  8.     name = passedName;}
  9.  
  10. int main(){
  11.     thing car("car");
  12.     thing ship("ship");
  13.     car.drive();
  14.     ship.drive();
  15. }
  16.  
Does that help you?
May 30 '07 #2

P: 14
I'm just wondering, why would you want to do that? You can have a base class with name member and then use that, if that's what you need:
Expand|Select|Wrap|Line Numbers
  1. class Thing{
  2.     string name;
  3.     public:
  4.         Thing(string);
  5.         int drive();
  6. };
  7. Thing::Thing(string passedName){
  8.     name = passedName;}
  9.  
  10. int main(){
  11.     thing car("car");
  12.     thing ship("ship");
  13.     car.drive();
  14.     ship.drive();
  15. }
  16.  
Does that help you?
This is not I want. I want other one can add their class name in the classlist.txt and implement their drive() function. I don't know the class name is mycar or yourcar. I only need to call the drive() of that class. I found in the C# reflection can implement this, I don't know if C++ can implement this.
I need implement the main() that don't care about the class name, but can call the class function drive() in C++. Is that possible?
May 30 '07 #3

Savage
Expert 100+
P: 1,764
Maybe,for example:

If u have for units in ur product,u could read class names from file and then in unit containing class instances edit class's names so they match thos from file.
After this run ur main unit which will recompile unit that contains class instances.

Savage
May 30 '07 #4

P: 14
Maybe,for example:

If u have for units in ur product,u could read class names from file and then in unit containing class instances edit class's names so they match thos from file.
After this run ur main unit which will recompile unit that contains class instances.

Savage
You suggested that before I compile my project I need run it to generate some code according to the class names. How to do that?
May 31 '07 #5

Savage
Expert 100+
P: 1,764
You suggested that before I compile my project I need run it to generate some code according to the class names. How to do that?
U can do this from command line.

Which compiler are u using?

Savage
May 31 '07 #6

P: 14
U can do this from command line.

Which compiler are u using?

Savage
Integrity 5.0.7

cxintppc.exe
Jun 1 '07 #7

Savage
Expert 100+
P: 1,764
Integrity 5.0.7

cxintppc.exe
Mostly this is how u call compiler from command line.

e.g

Borland c++

bcc32 [options] [PATH(if not allready there)] project_name.cpp.

so this should be:

cxintppc -c//COMPILE Your path and project_name.

savage
Jun 1 '07 #8

AdrianH
Expert 100+
P: 1,251
This is not I want. I want other one can add their class name in the classlist.txt and implement their drive() function. I don't know the class name is mycar or yourcar. I only need to call the drive() of that class. I found in the C# reflection can implement this, I don't know if C++ can implement this.
I need implement the main() that don't care about the class name, but can call the class function drive() in C++. Is that possible?
No, I don't think this can be done in C++ since a class is a compile time object.

C# and Java have reflection alowing one to inject classes into the runtime environment. This can be cool, but there are security issues that have to be addressed. C# does a stack walk and signing I think and Java does signing and a sandbox.

C++ doesn't use those concepts, so it would be very risky to do. To do what you want would require:
  1. a compiler if you want source to run using a native binary
  2. a dll if you want to give the user the ability to extend the code with a binary (no source, or you can mix with point 1).
  3. an interpreter if you don't mind interpreting the code, either implement one yourself (good luck ;)) or get one premade (C-- comes to mind)
  4. a rudamentary parser if you want to make a framework that will make objects that can 'run' the 'code' (not necesarly C++ code)
NOTE 1 & 2 are also somewhat of a security risk too. Someone can crash your code by doing those.

One other thing about C#, how do you read in a file and invoke the object's method if you don't know the name of the object's type?

EDIT: IF you want to do this at compile time, you could create an interface using a virtual function (this is called interface inheirtance or a template pattern which is unrelated to a C++ template). How you would get that object to your main code thread without knowing the name of the type, I cannot answer.


Adrian
Jun 1 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.