Select Call
Select Call

Reputation: 45

Function , array and pointers

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.

Upvotes: 0

Views: 106

Answers (2)

Sid
Sid

Reputation: 7631

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

Upvotes: 1

Alex Z
Alex Z

Reputation: 2590

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.

Upvotes: 1

Related Questions