dynamic instantiation of c++ objects

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2009
Posts: 1
Reputation: manoj.pg is an unknown quantity at this point 
Solved Threads: 0
manoj.pg manoj.pg is offline Offline
Newbie Poster

dynamic instantiation of c++ objects

 
0
  #1
25 Days Ago
If I have the class name in a string varibale, how can I instantiate an object of that class?

I am just thinking of COM and CORBA implementations where C++ object is instantiated from the data in the stream. How can I implement it?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert
 
0
  #2
25 Days Ago
You mean something like?

  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Foo
  5. {
  6. public:
  7. std::string sayHello()
  8. {
  9. return "Hello";
  10. }
  11. };
  12.  
  13. int main()
  14. {
  15. Foo test;
  16. std::cout << test.sayHello();
  17.  
  18. std::cin.get();
  19. }
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 311
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 33
jonsca jonsca is online now Online
Posting Whiz
 
0
  #3
25 Days Ago
I think he might mean a situation where you could take in a class name from the user and instantiate a class, so in your example above:
  1. string classname;
  2. std::cin >> classname;
  3. (it's invalid but...) classname * cn = new classname();
  4.  

I think for a finite number of cases you could use a bunch of if/else if statements:
  1. if(classname == "Foo")
  2. Foo * foo = new Foo();
  3. elseif(classname == "Bar")
  4. Bar * bar = new Bar();
  5. ...
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,151
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 145
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster
 
0
  #4
25 Days Ago
>>If I have the class name in a string varibale, how can I instantiate an object of that class?

Your question is not very clear, but my guess would be that you meant
something like :

  1. class Foo
  2. {
  3. public Foo() { }
  4. };
  5.  
  6. int main()
  7. {
  8. string className = "Foo";
  9. Foo * foo; //not instantiated but waiting in a sense to be created.
  10.  
  11. if( className == "Foo") //a string variable has the class name FO
  12. foo = new Foo(); //so create a new class of that object
  13.  
  14. //after foo is done being used with
  15. delete foo;
  16. }
Last edited by firstPerson; 25 Days Ago at 2:30 pm.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC