• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

function that returns the basetype

Is there any C/C++ function that returns the base type of given parameter. For example int GetBaseType( 24), returns 10, GetBaseType( 0x000B) returns 16.
0
rs_1915
Asked:
rs_1915
  • 4
  • 2
  • 2
  • +3
1 Solution
 
thienpnguyenCommented:
Sorry, C++ can not have that funtion .
0
 
thienpnguyenCommented:
Moreover, nobody can write that function .
0
 
LoungeLizardCommented:
What do you mean with "base type"? If you mean the type of class, then you can use RTTI to (sort of) achieve that:

int GetBaseType(char const * className)
{
  if (strcmp(className, "int"))
  {
    return 1;
  }

  if (strcmp(className, "float"))
  {
    return 2;
  }

  // Add all the class types you need here
}

Then you simply use this:

int c;

GetMyType(typeid(c).name())

Unless I'm missing the actual question here ...


0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
thienpnguyenCommented:
He means
 
     1234   is a decimal numbe r ==> return 10
     0234   is a  octal number      ==>  return 8
     0x123  is  a hex number      ==> return 16
0
 
LoungeLizardCommented:
Ah. The numerical base. Should have figured that out :)
0
 
fl0ydCommented:
Sorry, thienpnguyen, you're not right. I could come up with a function -- or a macro at least. Something like:
#define GET_BASE( n ) \
    { std::string str( #n ); \
      check for first two chars in str...\
      and return appropriate base \
    };

Sorry, it's still too early to come up with real code, but the key is to use the 'charizing operator' (#).

Hope that helps...
.f
0
 
thienpnguyenCommented:
fl0yd ,

Well, we see problem with different point of views . I think  the require for GetBase's argument
is a number .  In that case, we can not write a function as  rs_1915 's require .
However, you think the argument of GetBase is a string, in that case, it is easy to write .

By the way, the macro that you suggests does not work with variable name or macro ...
For example, what will happend when someone call GET_BASE( CONST_NUMBER )  with
#define CONST_NUMBER 0x100


Full code

#include <iostream>
using namespace std;

#define  GET_BASE( n )  GetBaseFromString(#n )
#define  CONST_NUMBER      0x123

int GetBaseFromString(char *str )
{
    cout << "Argument : " << str << endl;
    return 10; // dummy code

}

void main(int argc, char **argv)
{
    GET_BASE( CONST_NUMBER );

}



the output will be

Argument : CONST_NUMBER

not 0x123
0
 
fl0ydCommented:
Like I stated before, it's way too early to come up with a rock solid solution. However, there *IS* a way to do it -- I didn't suggest using it, just tried to prove a point.
Btw. what about returning 'NaN' for your call to GET_BASE( CONST_NUMBER )?

.f
0
 
griesshCommented:
Dear rs_1915

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to split between

     "tienpnguyen and fl0yd"

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
rs_1915Author Commented:
griessh
Sorry, I really forgot about this. I want to split between  floyd and tienpnguyen. How do i split the points?
0
 
griesshCommented:
rs_1915

I appreciate your feedback.
To split points, you usually post a 0 point question in Community Support at http://www.experts-exchange.com/commspt . Call it somethinmg like "Moderator please help" and tell them that you have a open question (include the URL, like http://www.experts-exchange.com/cplusprog/Q.20278910.html for this question) and ask them to split the points between expert A and expert B. One of our nice moderators will handle the splitting for you.

======
Werner

0
 
MindphaserCommented:
Force accepted

fl0yd
There will be a separate 10 points question for your help.

** Mindphaser - Community Support Moderator **
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now