944,030 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 823
  • C++ RSS
Nov 2nd, 2009
0

dynamic instantiation of c++ objects

Expand Post »
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
manoj.pg is offline Offline
1 posts
since Nov 2009
Nov 2nd, 2009
0
Re: dynamic instantiation of c++ objects
You mean something like?

C++ Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 2nd, 2009
0
Re: dynamic instantiation of c++ objects
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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. if(classname == "Foo")
  2. Foo * foo = new Foo();
  3. elseif(classname == "Bar")
  4. Bar * bar = new Bar();
  5. ...
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Nov 2nd, 2009
0
Re: dynamic instantiation of c++ objects
>>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 :

C++ Syntax (Toggle Plain Text)
  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; Nov 2nd, 2009 at 2:30 pm.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with C++ compatibility program
Next Thread in C++ Forum Timeline: Round Robin Completion Time





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC