Solved

function that returns the basetype

Posted on 2002-03-19
12
239 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now