Laurens Posted September 5, 2010 Share Posted September 5, 2010 Hi everyone, At the risk of making a fool of myself I am asking here because I can't figure it out for the life of me I have an EntityResource class that has a map containing properties that represent the base settings for an entity. Such properties can include "CollisionHull", containing the path to the entity's .phy file, "Orientation", containing the entity's matrix and so on. I have a templated Read method to read a value and return any type I want. The cast fails on every type though and I have no clue why. Here is the abbreviated class with the offending method: #ifndef ENTITYRESOURCE_H #define ENTITYRESOURCE_H #include <map> #include <string> using namespace std; class EntityResource { map<string, string> properties; public: template <typename T> const T Read(string property) const { map<string, string>::const_iterator i = properties.find(property); if (i != properties.end()) { return reinterpret_cast<const T>((*i).second); } return NULL; }; }; #endif Calling: body = LoadBody(("abstract::" + resource.Read<string>("CollisionHull") + ".phy").c_str()); will result in: error C2440: 'reinterpret_cast' : cannot convert from 'const std::string' to 'const std::string' Any push in the right direction appreciated Thanks! Quote Link to comment Share on other sites More sharing options...
Canardia Posted September 5, 2010 Share Posted September 5, 2010 body = LoadBody(string("abstract::" + resource.Read<string>("CollisionHull") + ".phy").c_str()); Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Laurens Posted September 5, 2010 Author Share Posted September 5, 2010 That doesn't work Still stuck on the same error. Thanks for the fast reply though EDIT: Thinking I might have to resort to creating seperate ReadString, ReadInt and ReadFloat methods Quote Link to comment Share on other sites More sharing options...
Canardia Posted September 5, 2010 Share Posted September 5, 2010 The error comes from your line with "reinterpret_cast" anyway, so just remove that. Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Laurens Posted September 5, 2010 Author Share Posted September 5, 2010 That would work for reading strings. The whole point of templating the method was to convert properties when reading them. I won't be able to read int's now for instance using resource.Read<int>("MaxSpeed"); which is stored in the map as a string. I always thought that reinterpret_cast was the C++ equivalent of a C-style cast (a C-style cast does work, in fact, but I'd rather not use it). I suppose this is not entirely the case then, correct? Quote Link to comment Share on other sites More sharing options...
Canardia Posted September 5, 2010 Share Posted September 5, 2010 You can have a int EntityResource::Read() and string EntityResource::Read(). Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Laurens Posted September 5, 2010 Author Share Posted September 5, 2010 You mean overriding by return type? I have never tried it because I assumed it was not possible to do so from past experience in C# and Java. To work around it for now I just have one Read method returning a string. Quote Link to comment Share on other sites More sharing options...
Canardia Posted September 5, 2010 Share Posted September 5, 2010 Of course it's possible in C++ since it's a real programming language Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
L B Posted September 5, 2010 Share Posted September 5, 2010 class A { int Test() { ... } } class B : A { new string Test() { ... } } I guess C# now is a real programming language as well. Quote Link to comment Share on other sites More sharing options...
Laurens Posted September 5, 2010 Author Share Posted September 5, 2010 Oh wow, never knew that was even possible in C# *just waiting for someone to come by now and show me this is possible in Java as well * Quote Link to comment Share on other sites More sharing options...
Rick Posted September 5, 2010 Share Posted September 5, 2010 Did that work for you in C++? I'm pretty sure you can't overload a function with just the return type being different. Here is a trick that I found: struct func { operator string() { return "1";} operator int() { return 2; } }; int main( ) { int x = func(); // calls int version string y = func(); // calls string version double d = func(); // calls int version cout << func() << endl; // calls int version func(); // calls neither } Lazlo that might work, but does it work if they are all in the same class? That seems to be what he's trying to do. Quote Link to comment Share on other sites More sharing options...
L B Posted September 5, 2010 Share Posted September 5, 2010 I don't have MSVC# right now, but I think this would be valid: class Foo { int Test() { ... } new string Test() { ... } } Quote Link to comment Share on other sites More sharing options...
Canardia Posted September 6, 2010 Share Posted September 6, 2010 Did that work for you in C++? I'm pretty sure you can't overload a function with just the return type being different. I tried it and it works. Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Laurens Posted September 6, 2010 Author Share Posted September 6, 2010 I have not tried it yet but the documentation I was able to google was sketchy at best. The general consensus appears to be that return type and signature need to be the same. I will try sometime later this week though. Quote Link to comment Share on other sites More sharing options...
Laurens Posted September 6, 2010 Author Share Posted September 6, 2010 I have been trying to get it working but no success so far. Some more googling got me this: Once declared virtual, a member function can be overridden in any level of derivation, as long as the overriding version has the identical signature of the original declaration. This is quite straightforward, but sometimes a virtual has to return an object of type that differs from the return type declared in its base class. The C++ Standard allows that only when the return type is replaced with a class publicly derived from the original return type. from http://www.devx.com/tips/Tip/12476. Have you got an actual working piece of code Lumooja? I would love to see this work. Cheers! Quote Link to comment Share on other sites More sharing options...
Canardia Posted September 6, 2010 Share Posted September 6, 2010 It doesn't work with my Express version, I have to check why it worked with my Professional version tomorrow. Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ 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.