Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

function that returns the basetype

Posted on 2002-03-19
12
265 Views
Last Modified: 2010-04-02
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
Comment
Question by:rs_1915
  • 4
  • 2
  • 2
  • +3
12 Comments
 
LVL 6

Accepted Solution

by:
thienpnguyen earned 10 total points
ID: 6880728
Sorry, C++ can not have that funtion .
0
 
LVL 6

Expert Comment

by:thienpnguyen
ID: 6880730
Moreover, nobody can write that function .
0
 
LVL 2

Expert Comment

by:LoungeLizard
ID: 6880847
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
LVL 6

Expert Comment

by:thienpnguyen
ID: 6880871
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
 
LVL 2

Expert Comment

by:LoungeLizard
ID: 6881718
Ah. The numerical base. Should have figured that out :)
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 6882155
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
 
LVL 6

Expert Comment

by:thienpnguyen
ID: 6883840
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
 
LVL 8

Expert Comment

by:fl0yd
ID: 6884050
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
 
LVL 11

Expert Comment

by:griessh
ID: 6955436
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
 
LVL 1

Author Comment

by:rs_1915
ID: 6955478
griessh
Sorry, I really forgot about this. I want to split between  floyd and tienpnguyen. How do i split the points?
0
 
LVL 11

Expert Comment

by:griessh
ID: 6955987
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
 
LVL 6

Expert Comment

by:Mindphaser
ID: 6979112
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question