Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I'm trying to pass user input into an overloaded function, but one functions needs a string and the other needs an int, is there a way to just pass in the user input without having to do ifs to check if its a string or int and then having to different call lines?
There is two ways to apporach your problem
1: using the template typename
template <typename T>
void print(T input){
cout<<input<<endl;
}
2: You can use the overloaded functions
void print(int input){
cout<<input<<endl;
}
void print(double input){
cout<<input<<endl;
}
Good Luck
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
how do i add a user input to my code? I want to create a simple while loop program that will increment the value input by the user by 2.
#include <iostream>
using namespace std;
int main()
{
int a=2;
while(a<=100)
{
cout<< "Value of a: "<<a<<endl;
a=a+2;
}
return 0;
}
Not sure I completely understand your question, but the syntax for user input would be cin >> variable here;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a container class that contains a char array and an enum/integer indicating what the char array should be cast to. I use it in a callback as so:
void callback(Foo& foo){
switch(foo.type()){
case(1): do( (Bar1*) foo.stuff() );
case(2): do( (Bar2*) foo.stuff() );
case(3): do( (Bar3*) foo.stuff() );
...
}
}
Is there a way to store a mapping from the integer to the type (1, Bar1), (2, Bar2), etc. so that I can clean this switch statement up since it's getting long? Or otherwise, are there any template metaprogramming idioms that can be used in this case?
Store your objects in a std::variant instead of rolling your own using an array of char. Along with std::visit, it supports exactly the use-case you need:
void callback(std::variant<Foo1, Foo2, Foo3>& foo) {
std::visit([](auto& f) { doit(f); }, foo);
}
If you can't use std::variant then a switch is likely the simplest solution given a single known set of types.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Couldn't find answer to this. When I have pointer on pointers
char **buffer;
and I want to pass it to a function
void some_func(char **buffer) {}
so after this function call buffer will contain data from this function how should I call this function please ?
in your example it is correct but the fact is that you are passing this pointer to pointer by value so pass it by pointer:
// initialize it
char** buffer;
void some_func(char ***buffer) {} // by reference
and in function call:
some_funct(&buffer);
some_func(&buffer);
That's the call you need.
And the function should be...
void some_func(char ***buffer) {}
To manipulate buffer...
*buffer = something;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If the code is like the below,
void func(std::string str)
{
...
}
void main()
{
std::string p1 = "abcd";
char p2[SOME_LENGTH] = "abcd";
func(p1); // (1)
func(p2); // (2)
}
which way is efficient between (1) and (2)?
They are equally efficient/inefficient. Both involves copying the string and using the copy as the value of the argument 'str'. A better way would be declaring func as
void func(const std::string &str) {
}
This can avoid copying of the string.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to use just a plain, arbitrary keyword as a C function argument or a C++ template argument?
For example:
func(Random);
or
Class<Random>();
In this example, Random is not a predefined variable name, type name or Macro value.
Essentially, these values will be converted into a string but for the my purposes, I don't want to pass it as a string.
Is this at all possible?
You can, if you want, define some macro
#define KW(x) #x
and then you are free to use
void func(const char *p);
func(KW(oi));
or
void func(std::string s);
func(KW(ciao));
... another option would be defining func as a macro, like:
#define func(a, x, b) realFunc(a, #x, b)
and then
void realFunc(int a, std::string s, double b);
func(3, arigato, 4.1);
...