Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Craazer

Handling diffrend types in one function

This topic is 5803 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Is it anyway bossible to have function wich could take any parameter no matter is it array or just single integer? For example the func should now when load string(not the type) to char array or when to load number to int. template would be the basis of this if its bossible...?

Share this post


Link to post
Share on other sites
Advertisement
As you said, templates are the answer. I think this will do:


  
template class<T>
T Foo(T Stuff)
{
// Process stuff in here

return (Stuff);
}


Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.


My Site

Share this post


Link to post
Share on other sites
If you want different types with a typing relationship, use virtual functions and dynamic dispatch.

If you want different types with a matching relationship, use templates.

Share this post


Link to post
Share on other sites
quote:
Original post by Sand_Hawk
As you said, templates are the answer. I think this will do:



    
template class<T>
T Foo(T Stuff)
{
// Process stuff in here

return (Stuff);
}


Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.


My Site



Ok thanks but i know that, my real queston is:

How can i know am i dealing whit array or not?

example if i have char array i do this:
char var[10]="104"
and if i have integer i do this:
int var = 104;

so how do i know wich one use?




Share this post


Link to post
Share on other sites
If you want different functions for each type, you can overload the functions to work differently for each one:


  
int TestFunc(int oneInt)
{
// Single integer processing

}

int TestFunc(int* intArray)
{
// Array processing

}

int TestFunc(float oneFloat)
{
// Float processing

}


You can also use templates if you can generalize the operation, as already shown. Or if you want to combine the two, you can do template specialization:


  
template<typename T>
int TestFunc(T singleVar)
{
// Handle single object of any type

}

template<>
int TestFunc(int* intPtr)
{
// Specialized case for an integer pointer (array)

}


My syntax may be a little off, in which case someone will correct it.

There's also run time type identification (RTTI) that can be use to check types at runtime, but it's probably not the best for what you want to do.

[edited by - cgoat on December 2, 2002 3:43:47 PM]

Share this post


Link to post
Share on other sites
Keep in mind that if you pass an array to a function, the function only receives a pointer to the beginning of the array, and has no knowledge of how big the array is. That''s why many functions in the C library that take arrays as arguments also take an integer giving the size of the array. This need many make what you are trying to do impossible. If you are willing to use STL containers instead of vanilla arrays (there''s no reason not to), then the object knows its own length, and this one problem is solved.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!