Jump to content

How to get from A to B (actually, from type "A" to type "B")


Cambridge Ray

Recommended Posts

I am new to the forum. Hello all...

 

I was flabbergasted the first time I used an IDE that had the "period completion" feature (or the similar, "left parenthesis"). It made me considering leaving old trusted "vi".

 

The problem with the period is that it only goes one step deep: more steps are needed! Additionally, sometimes the period is not the answer to your travails, when a non-member function is the one that will convert from type "A" to type "B".

 

When I was learning Java, a large number of questions (and time wasted) that I posted were of the form:

 

"How do I convert [some type] to [some other type]?"

 

The text conversions alone occupy an unordinate amount of time to programmers (just try Google to see what I mean).

 

Some C++ examples:

 

I had been using for a long time this (from Boost::Filesystem):

 

 string somestring = "abc/de";
 path p = path(somestring);

 

Only to realize, accidentally, that the conversion is done automatically. The IDE should help you in those cases:

 

path p = somestring;

This one made me kick myself. I used this many, many times:

const char* sometext = somestring.string().c_str();

 

Well, it turns out that this one is just as good:

 

const char* sometext = somestring.c_str();

 

My question is about research done in this particular field. I tried Google but the word "type" is too ambiguous.

 

This problem is very similar to the resolution of Rubik's Cube. Your expression is in some "scrambled" state and you need the computer to tell you -not only any path! mind you- but the shortest path (known as God's algorithm) to the desired type.

 

Exhaustive, aka brute force search can be used: after all, we are not talking Rubik type of steps, most of the time we are 1 or 2 steps away from the conversion.

 

Regards,

 

-RFH

Link to comment
Share on other sites

Just build up a truth table of all of the different conversions and create a case statement that provides the correct translation for each conversion.

 

You will also have to be careful where you place this statement in your code as you may not get a public variable from the declaration.

 

The shortest path is really up to you.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.