Solved

function that returns the basetype

Posted on 2002-03-19
12
272 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

751 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