| | |
Deciding return type of a function at runtime
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2007
Posts: 49
Reputation:
Solved Threads: 0
Hi..
My code is as follows :-
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
My code is as follows :-
c++ Syntax (Toggle Plain Text)
class Class1 { }; class class2 { }; main() { .... ..... if(var==true) class1 c1=createandmanipulate(file); else class2 c1=createandmanipulate(file); ...... ..... ..... }
Last edited by stymiee; Sep 11th, 2007 at 11:14 am. Reason: Please use code tags
I think this might work.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class Class1 { }; class Class2 { }; // Specialize two type traits for picking class 1 and class 2. template <bool> class ClassType { }; template <> class ClassType<false> { }; // Overload createandmanipulate to use the type traits. Class1 createandmanipulate( ClassType<true> ) { cout<<"Returning Class1\n"; return Class1(); } Class2 createandmanipulate( ClassType<false> ) { cout<<"Returning Class2\n"; return Class2(); } // It all works! :) int main() { Class1 c1 = createandmanipulate( ClassType<true>() ); Class2 c2 = createandmanipulate( ClassType<false>() ); return 0; }
The truth does not change according to our ability to stomach it.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
>> ... 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.
use an if/else with dynamic cast only if virtual functions cannot meet your requirements.
an option would be to use run-time polymorphism.
C++ Syntax (Toggle Plain Text)
class base { virtual ~base() = 0 {} }; class Class1 : public base {} ; class class2 : public base {} ; base* createandmanipulate( ifstream& file ); int main() { //.... //..... base* object = createandmanipulate(file); if( var==true ) { class1* c1 = dynamic_cast<class1*>(object) ; assert(c1) ; // use c1 } else { class2* c2 = dynamic_cast<class2*>(object) ; assert(c2) ; // use c2 } //.... //..... delete base ; }
>>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.
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.
![]() |
Similar Threads
- QBASIC under Windows XP (Visual Basic 4 / 5 / 6)
- Trouble writing code for arithmetic calculations of complex numbers (C++)
- Open In New Window Php (PHP)
- Program works... now I need to create a function, HELP! (C)
- Having trouble passing bool type to function....need help (C++)
- using functions (C#)
- This ought to be simple - extra spaces (PHP)
- Need Help counting Array Length (C++)
- Need direction on how start this program (C++)
Other Threads in the C++ Forum
- Previous Thread: how to get handle to window
- Next Thread: error checking help
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






