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:

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

class Thing{

string name;

public:

Thing(string);

int drive();

};

Thing::Thing(string passedName){

name = passedName;}

int main(){

thing car("car");

thing ship("ship");

car.drive();

ship.drive();

}

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?

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.

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?

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:

a compiler if you want source to run using a native binary

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).

an interpreter if you don't mind interpreting the code, either implement one yourself (good luck ;)) or get one premade (C-- comes to mind)

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.