Deciding return type of a function at runtime

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

Join Date: Aug 2007
Posts: 49
Reputation: tonyaim83 is an unknown quantity at this point 
Solved Threads: 0
tonyaim83 tonyaim83 is offline Offline
Light Poster

Deciding return type of a function at runtime

 
0
  #1
Sep 11th, 2007
Hi..
My code is as follows :-

  1. class Class1
  2. {
  3. };
  4. class class2
  5. {
  6. };
  7.  
  8. main()
  9. {
  10. ....
  11. .....
  12. if(var==true)
  13. class1 c1=createandmanipulate(file);
  14. else
  15. class2 c1=createandmanipulate(file);
  16. ......
  17. .....
  18. .....
  19. }
How should i declare the createandmanipulate() function so that it can return the respective object i.e either of Class1 or Class2 depending on the value of variable "var"at runtime. Also i know it can be done using template but how. ?? I have no idea.. Plz help
Last edited by stymiee; Sep 11th, 2007 at 11:14 am. Reason: Please use code tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Deciding return type of a function at runtime

 
0
  #2
Sep 11th, 2007
I think this might work.
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Class1 { };
  6. class Class2 { };
  7.  
  8. // Specialize two type traits for picking class 1 and class 2.
  9. template <bool>
  10. class ClassType { };
  11.  
  12. template <>
  13. class ClassType<false> { };
  14.  
  15. // Overload createandmanipulate to use the type traits.
  16. Class1 createandmanipulate( ClassType<true> ) {
  17. cout<<"Returning Class1\n";
  18. return Class1();
  19. }
  20.  
  21. Class2 createandmanipulate( ClassType<false> ) {
  22. cout<<"Returning Class2\n";
  23. return Class2();
  24. }
  25.  
  26. // It all works! :)
  27. int main() {
  28. Class1 c1 = createandmanipulate( ClassType<true>() );
  29. Class2 c2 = createandmanipulate( ClassType<false>() );
  30.  
  31. return 0;
  32. }
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Deciding return type of a function at runtime

 
0
  #3
Sep 11th, 2007
>> ... depending on the value of variable "var"at runtime. Also i know it can be done using template ...

an option would be to use run-time polymorphism.
  1. class base { virtual ~base() = 0 {} };
  2.  
  3. class Class1 : public base {} ;
  4.  
  5. class class2 : public base {} ;
  6.  
  7. base* createandmanipulate( ifstream& file );
  8.  
  9. int main()
  10. {
  11. //....
  12. //.....
  13. base* object = createandmanipulate(file);
  14. if( var==true )
  15. {
  16. class1* c1 = dynamic_cast<class1*>(object) ;
  17. assert(c1) ;
  18. // use c1
  19. }
  20. else
  21. {
  22. class2* c2 = dynamic_cast<class2*>(object) ;
  23. assert(c2) ;
  24. // use c2
  25. }
  26. //....
  27. //.....
  28. delete base ;
  29. }
use an if/else with dynamic cast only if virtual functions cannot meet your requirements.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Deciding return type of a function at runtime

 
0
  #4
Sep 11th, 2007
>>an option would be to use run-time polymorphism.
I thought about that too but dismissed it because it may not work. createandmanipulate() still has to know whether to allocate an object of type Class1 or Class2 before returning. If it allocates an object of only base then the caller will not get what it expects.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Deciding return type of a function at runtime

 
0
  #5
Sep 11th, 2007
>> If it allocates an object of only base
base
has a pure virtual destructor, the function will not be able to instantiate (a standalone) object of that type.
however, it could return 0; perhaps indicating an error of some kind.
Last edited by vijayan121; Sep 11th, 2007 at 11:07 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC