943,668 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1988
  • C++ RSS
Sep 11th, 2007
0

Deciding return type of a function at runtime

Expand Post »
Hi..
My code is as follows :-

c++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 8
Solved Threads: 0
Light Poster
tonyaim83 is offline Offline
49 posts
since Aug 2007
Sep 11th, 2007
0

Re: Deciding return type of a function at runtime

I think this might work.
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Sep 11th, 2007
0

Re: Deciding return type of a function at runtime

>> ... 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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Sep 11th, 2007
0

Re: Deciding return type of a function at runtime

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005
Sep 11th, 2007
0

Re: Deciding return type of a function at runtime

>> 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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

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: how to get handle to window
Next Thread in C++ Forum Timeline: error checking help





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


Follow us on Twitter


© 2011 DaniWeb® LLC