Solved

function that returns the basetype

Posted on 2002-03-19
12
255 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

776 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