Jump to content

Conversion problem


Laurens
 Share

Recommended Posts

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 :D

 

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 :blink:

 

Thanks!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...