| | |
dynamic instantiation of c++ objects
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
0
#2 24 Days Ago
You mean something like?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> class Foo { public: std::string sayHello() { return "Hello"; } }; int main() { Foo test; std::cout << test.sayHello(); std::cin.get(); }
*Voted best profile in the world*
•
•
Join Date: Sep 2009
Posts: 306
Reputation:
Solved Threads: 33
0
#3 24 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:
I think for a finite number of cases you could use a bunch of if/else if statements:
C++ Syntax (Toggle Plain Text)
string classname; std::cin >> classname; (it's invalid but...) classname * cn = new classname();
I think for a finite number of cases you could use a bunch of if/else if statements:
C++ Syntax (Toggle Plain Text)
if(classname == "Foo") Foo * foo = new Foo(); elseif(classname == "Bar") Bar * bar = new Bar(); ...
0
#4 24 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 :
Your question is not very clear, but my guess would be that you meant
something like :
C++ Syntax (Toggle Plain Text)
class Foo { public Foo() { } }; int main() { string className = "Foo"; Foo * foo; //not instantiated but waiting in a sense to be created. if( className == "Foo") //a string variable has the class name FO foo = new Foo(); //so create a new class of that object //after foo is done being used with delete foo; }
Last edited by firstPerson; 24 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?![]() |
Similar Threads
- Dynamic array of objects within an object (C++)
- delegates and references to objects (C#)
- instantiation; and arrays & structures really objects? (C#)
- Help OOP runtime selection and instantiation of objects (C#)
- C++ Dynamic Array Of Objects Deletion (C++)
- add event listeners for dynamic list of objects (JavaScript / DHTML / AJAX)
- Creating and destroying objects (C++)
Other Threads in the C++ Forum
- Previous Thread: Help with C++ compatibility program
- Next Thread: Round Robin Completion Time
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






