0

So say I had:

public: static void print(string message) { }

Is there a way I could make it accept other data types (eg. int, double etc) in the same function? So I could call it like print(7) and it would work?

Thanks everyone for the answers; in the title I wanted it as the same function because I didn't realise functions could have the same names as each other (like variables). Hence, I thought the only way you could do it was through the same function. As I am quite new to C++, I preferred the method of just using the same function name and doing it with different parameters (the overloading method).

4
  • 3
    It is called overloading. Commented Dec 29, 2014 at 11:16
  • 1
    Just use overloaded methods as it ensures type safety etc Commented Dec 29, 2014 at 11:16
  • @user2672165 Overloading is for reusing the name, not the "same function". I presume OP meant the same code, to avoid copying statements, that would make sense. Though, on the second thought I might be wrong. Commented Dec 29, 2014 at 11:22
  • 1
    @luk32 Overloading seems fine to me. I didn't think that you could have functions with the same name (like variables) so I thought the only option would be to have it in the same function. Commented Dec 29, 2014 at 11:52

4 Answers 4

1

This is what the templates are for e.g:

template<typename Type>
void print_something(Type something) {
  std::cout << something << '\n';
}

The compiler is smart enough to deduce type from parameter. So you can call it like this:

print_something("test");
print_something(42);

It also works for member functions. What compiler does is it substitutes the Type with a concrete type (like int, or char*, or std::string) and produces an overload.

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

Comments

0

just create functions with the same name but with different parameters, this is called function overload, for example:

void print(char *message) { printf("%s", message); }
void print(int val) { printf("%d", val); }
void print(char ch) { printf("%c", ch); }

All above functions are overloaded, see examples of calling:

print("Hello World!");
print(10);
print('P');

this is only available in C++ (not in C)

1 Comment

Thanks, didn't realise functions could have the same name.
0

You can use C++ templates, Template provide generic data types in c++. see this for more details.

http://www.tutorialspoint.com/cplusplus/cpp_templates.htm http://www.cprogramming.com/tutorial/templates.html

Comments

0

You can easily do that by using templates. like this:

template <class T>
class CTest{
    .
    .
    .
}
template <class T>
static void print::CTest<T>(T message){...}

and then specify your variable type while using in your main function.

Comments

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.