ParaToxic Posted September 4, 2012 Share Posted September 4, 2012 Hey ya, I want to make a little debugconsole which has the function to add some information like "The Car drives:" and a pointer to the varibale like Car_Speed and this debuginfos were shown in a listbox, so you have a nice overview to all your debug variables you want to see. My problem is that I need a std::map for that like: std::map< int, std::pair<String, (Type) > > a; . the first int is for the index that helps to manage the listing in the listbox, the String is the variable information ( "The Car drives:" ) and the third is the pointer to the variable.Now the problem is that I want to put in different types like int,float,double or vectors, so I want a dynamic Type ( like template<typename a> but that doesn't work). So what can I do here ??? Thanks Quote Link to comment Share on other sites More sharing options...
Furbolg Posted September 4, 2012 Share Posted September 4, 2012 You cant do nothing here. Because templates are not "dynamic Type", Templates are "choosen Type". For example: std::list<string> stringlist; std::list<float> floatlist; // this ok stringlist.push_back( "test" ); floatlist.push_back( 1.0f ); // this wont work stringlist.push_back ( 2.0f ); floatlist.push_back ( "wow" ); What you "can" do here right now is using interfaces and inheritance for example: #include <windows.h> #include <iostream> #include <vector> #include <string> #include <sstream> #include <list> class IConsoleOutput { public: virtual std::string getConsoleString() = 0; }; class IntOutput : public IConsoleOutput { public: IntOutput(std::string name, int& refvalue) : name(name), value(refvalue) { } std::string getConsoleString() { std::stringstream output; output << name << " = " << value << std::endl; return output.str(); } private: std::string name; int& value; }; // same for float etc. int main() { int a = 0; std::vector<IConsoleOutput*> debugstrings; IntOutput testint(std::string("simple test"), a); debugstrings.push_back( &testint ); // i normally use IntOutput *bla = new IntOutput(...) but this is simpler and works right now // output all strings for(int i = 0; i < debugstrings.size(); i++) { std::cout << debugstrings[i]->getConsoleString(); } a = 10; // output all strings for(int i = 0; i < debugstrings.size(); i++) { std::cout << debugstrings[i]->getConsoleString(); } } This is just a small example without const correctness and some other design patterns (observer, visitor, factory ....) but i hope this is an idea for you. Quote Link to comment Share on other sites More sharing options...
ParaToxic Posted September 5, 2012 Author Share Posted September 5, 2012 This is a possible way.Is it possible to create a String where you set the pointer of your value and the String is always changing if the vale changes ?? (By the way I could use void* or not ??? ) Pseudo code: String** mystring ; mystring = pointer to my value; (I have to convert this with stringstream..) So I have a string pointer to my value pointer to my value Here is a code from an other forum: #include <iostream> #include <string> #include <unordered_map> #include <utility> struct variable_printer { template <typename T> void insert(std::string const& name, T& ref) { variables_[name] = std::make_pair(&ref, &print_impl<T>); } void print(std::string const& what) const { auto iter = variables_.find(what); if(iter != variables_.end()) iter->second.second(iter->second.first); // else ... } private: typedef void const volatile variable; template <typename T> static void print_impl(variable* ptr) { std::cout << *static_cast<T*>(ptr); } std::unordered_map<std::string, std::pair<variable*, void (*) (variable*)>> variables_; }; int main() { variable_printer printer; int const volatile i = 42; printer.insert("i", i); printer.print("i"); } Quote Link to comment Share on other sites More sharing options...
Furbolg Posted September 5, 2012 Share Posted September 5, 2012 Hi Paratoxic! 1. void* (pointer) are evil (because you can pass everything to it) 2. if you use a reference (&) or pointer (*) then it will get the actual value but be careful. If the pointer points to null or an undefined value (if your variable gets deleted) you will get some strange behavior/crashes and most dangerous if you do something like this : if (myrefvalue = testvalue) You can change the value from within your "printer" method/class easily so take care and use const (the code above is not a comparisation, its an assignment which return true) The code you postet is another way (to rome) and it should work. To be honest, im an old style programmer im not such familiar with templates Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.