473,372 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Casting type int to const void*

Greetings,

I finally got around to trying to learn C++ in the last week. I have a good grasp on some of the fundamentals, but I am having trouble in some code I am writing.

I have a two functions, one that returns an int, and one that takes a const void* as an argument. I need to cast the int from the first function so that I can use it as an argument for the second function.

I have looked around and can't seem to find any information on how to do this. Any help with this is appreciated.
Feb 13 '08 #1
8 7000
Banfa
9,065 Expert Mod 8TB
The fact that you have got into a situation of having to cast an int to a const void * suggests poor program design (or possibly direct access to memory mapped peripherals as you might find in an embedded project).

In fact any use of void * in C++ tends to suggest poor design or that you are using the C like aspects of C++ rather than the full range of C++ programming techniques.

However if you must cast int to const void * then look up reinterpret_cast<...>(...) in you C++ book.
Feb 13 '08 #2
Well, as I said, I am new to C++ (just started last week),,,, however I really didn't see a choice in this instance. Although that could be just lack of experience.

The situation is, I am writing a plugin for a game compiler (DarkBASIC Professional). The functions have to be listed in a string table along with what types they accept as arguments and what type they return... but it is limited in the case where your function needs to be able to accept any type.

Overloading is a real pain to get to work in this instance because I need to be able to accept 1 to 10 arguments of any allowed type in any combination. That would be literally thousands of overloaded functions. However, they do have a string table indicator for const void*... so that was my choice, and it works.

Thanks for the tip... I am going to look up your suggestion now.
Feb 14 '08 #3
oler1s
671 Expert 512MB
but it is limited in the case where your function needs to be able to accept any type.
Bad design. Your function should be not be having to deal with "any possible kind of input". If you can't create functions that are fixed on certain types of arguments, it's because you didn't design your system properly.
Feb 14 '08 #4
Ok... I intend this non-sarcastically: How do you you suggest I structure a function that is to be provided as a command in another programming language that lets someone send a function call with arguments.... but you don't know the types of the arguments that they are going to have to send in advance? The could send an int, a string, a float, etc. And my function has to be able to accept it no matter what... up to 10 arguments like that in any order and in any number (up to 10).

Seriously... I would like a better design on this, but I can't make 10,000 different overloaded functions to handle all the possible combinations.

What I settlled on was 10 overloaded functions with 1 to 10 const void* arguments as the parameters. Since I am simply passing them off later to another function (usually unmodified) this has worked just fine, but I am open to suggestions.
Feb 14 '08 #5
oler1s
671 Expert 512MB
There is a better way. I wasn't mocking you when I told you your design is flawed. Exponential growth in variations is a sign of bad design.

How do you you suggest I structure a function that is to be provided as a command in another programming language that lets someone send a function call with arguments.... but you don't know the types of the arguments that they are going to have to send in advance?
First ask yourself, even though they technically could send any type of argument, what types does your programming logic encompass? For example, a function that takes a base to a power, and is open to user input, could technically "take any form of input", but the only meaningful type of input is a numerical value. Furthermore, this could be constrained to floating point types.

Since you're struggling with the design, why don't you describe what your function logically does with the arguments?

In any case, the best way to deal with user input might be to accept everything as a giant string, or a series of string arguments. Then attempt to tokenize or parse into appropriate data types as appropriate.

Again, I want to steer you away from the "any input type possible" situation, and I ask that you make an effort to avoid it as well.

But if all fails, I would advise you to look at getopt or boost.program_options. They are used to work with program command line arguments, which means any type of input that must eventually be sorted into appropriate data types like strings, integers, and floating point.
Feb 14 '08 #6
Well in this case I am making an ad-hoc event raising/handling system for a BASIC style language.... I take the arguments they want to pass with their event, store it in a queue, then at the DoEvents() command I pass them off to a command in yet another person's DLL that sends the arguments to the appropriate function in the BASIC code.

The arguments can literally be of any type. If they have a function that they need a string as a value for, they send a string with their event... if their handler function needs 2 strings and 3 integers they are going to send that.

The job of my function is just to organize everything and pass it along at the appropriate time without worrying about the types involved.

Since I don't have control over, or access to, the code for the BASIC compiler or the plugin DLL that I have to hand the values off to, I am a bit limited in how I can deal with them.

My code does the above flawlessly. The hang-up came when I wanted to introduce some hard coded events... like an OnMouseMove where I send the X and Y of the mouse position with the event... those are sent back by the function I use as type int.... since my event queue is geared towards storing values of type const void*, I was needing to convert the values for storage in the event queue.
Feb 14 '08 #7
Laharl
849 Expert 512MB
You might want to look at variadic functions, which can have any input of any type in any amount. They're a little tricky to work with, but they might help you.
Feb 14 '08 #8
Thanks, that sounds REALLY interesting. Always willing to pick up a new trick as long as I have something to search for. :-)
Feb 14 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Enrico `Trippo' Porreca | last post by:
Given: typedef struct Node Node; struct Node { void *obj; Node *next; }; typedef struct Stack Stack; struct Stack {
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
0
by: Greg | last post by:
Not sure if this is best place for this problem, but here it is. I have a project that is simply a C# class that interfaces with an IFilter. This is so I can retreive the text from Word docs. ...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
5
by: brekehan | last post by:
I've always been a little sketchy on the differences between static, dynamic, and reinterpret casting. I am looking to clean up the following block by using C++ casting instead of the C style...
14
by: Jonas.Holmsten | last post by:
Hello I'm porting some C++ stuff to C and having problem to get it through gcc. Here is a condensed version of the problem: void foo(const int * const * const ptr) {} main()
2
by: keith | last post by:
Hello, Could someone please explain why the (GCC, ancient 2.95.3 version) compiler won't let me static_cast<a char* to a unsigned char* or vice versa? It also won't accept dynamic_cast<for...
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
6
by: meLlamanJefe | last post by:
I am writing a queue object that will take different types of values. My approach is to have the std::queue take objects of a base class type while the user interacts with the queue using objects...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.