0

I have a situation where a function can only take "int" (can't change this) and I need it in a different situation. let me directly write the code

bool foo(int dev) 
{
         ...
         ...
      return true/false;
}

I need to pass :

  1. mClassPointer->dev()
  2. mClassPointer[index]->dev()
  3. dev() //(function)
  4. and obviously dev //(variable)

mClassPointer is pointer to class. dev() is a member function of a class , return an Integer.

4
  • 4
    It is very unclear as to what you are trying to acheive. Commented Mar 2, 2012 at 3:21
  • Thank You, Als for editing I am new here no much familiar with the syntax Commented Mar 2, 2012 at 3:24
  • 1
    You do not want to return true/false;. Division by zero is undefined. Commented Mar 2, 2012 at 3:24
  • James , that's just to refer ture or false :) Commented Mar 2, 2012 at 3:25

2 Answers 2

1

If you have a function that needs to handle different datatypes in different situations (as is vaguely implied in your question), then perhaps you need to look into templates.

Sign up to request clarification or add additional context in comments.

8 Comments

+1 for how to make the function really generic. I don't really know if that's what he meant or if there's some weird restriction that only allows him to accept integers.
Please elaborate I have a situation where I can take only "int" value
@SelectCall Could you elaborate as to your question? We're not sure what you want to do or why.
@SelectCall What other types do you want the function to take? Your question isn't clear enough there. Theoretically using a template can allow any type to be used... provided it doesn't break the code the template is used in
I have couple of device which I need to support , to validate the device I have been calling function at different places which takes above listed arguments . till now It has only "int" now as I am modifying further requires it to handle all I wanna make function generic
|
1

You may be able to do that by changing the argument to a void*.

Be very careful with this and read this thread carefully, espc. the post by Loki Astari:

error: cast from 'void*' to 'int' loses precision

If the function only accepts an int then I don't know if this is possible. Read a discussion in this thread if you are thinking of casting your pointers to int and passing. May not work on certain platforms: Converting a pointer into an integer

6 Comments

But he says, 'function can only take "int"'. I'm totally confused about what he's asking.
He wants to be able to pass multiple types to a function that takes an int?
Hmm, not sure if that is possible without issues. Read this stackoverflow.com/questions/153065/…
Oh, that seems issue if it does't then need to care anything else not disturb if I change the type?
@SelectCall not sure what you mean?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.