473,386 Members | 1,796 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,386 software developers and data experts.

How to instantiate a class by a string?

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
8 4675
ilikepython
844 Expert 512MB
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
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
1,764 Expert 1GB
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
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
1,764 Expert 1GB
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
U can do this from command line.

Which compiler are u using?

Savage
Integrity 5.0.7

cxintppc.exe
Jun 1 '07 #7
Savage
1,764 Expert 1GB
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
1,251 Expert 1GB
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

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

Similar topics

6
by: Frank Millman | last post by:
Hi all I have a question regarding inheritance. I have come up with a solution, but it is not very elegant - I am sure there is a more pythonic approach. Assume the following class definitions....
8
by: Carel Lotz | last post by:
H We have ported our VB 6 application into VB .NET but are still integrating with a few COM + applications written in VB6 running on our application server (Win 2000 Server). We have the proxies...
5
by: Glenn Serpas | last post by:
I have Class A and Class B .. Class B has a private member that is a pointer to a Class A object. private: B *mypointer ; I instantiate the A object A* myobject new = A();
2
by: Prashant | last post by:
Hi , Need some help ! . I am storing Classnames in an XML file and depending on the user's choice of process want to : 1) Read the classname of that process from the XML file 2) Instantiate...
1
by: Andreas Poller | last post by:
Hello, I have the following problem: I have a class deriving from ICustomTypeDescriptor: public __gc class TPropertyBag : public ICustomTypeDescriptor { private: ...
2
by: ghobley | last post by:
Hello, I have a problem where by wish to instantiate an object say NewReport to a specific class, all inherited from the base class MyReport, I have the classname as a string and I don't want to...
2
by: Merk | last post by:
I'm wanting to know if/how it would be possible to load a form based on a string that contains the name of the form (class). For example, instead of doing this: myForm f = new myForm();...
2
by: Pyenos | last post by:
class model:pass class view: model() class controller: model() I can instantiate clsss model from inside class view but I can't instantiate class model from inside controller, due to the...
4
by: Tomas | last post by:
A newbie question: How can I instantiate objects dynamically in VB.NET. E.g. I have the object 'Player' and I would like to instantiate it with the several instances (James, Gunner, etc.), without...
1
by: learning | last post by:
Hi how can I instaltiate a class and call its method. the class has non default constructor. all examples i see only with class of defatul constructor. I am trying to pull the unit test out from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.