Reputation: 13
I think the title of question is little bit confusing, so I'll explain that.
Let's see this very simple code.
#include <iostream>
class C {
public:
int val;
C(){
val = 2;
}
void changeVal(int i){
val = i;
}
};
void printout(int val){
std::cout << "int val : " << val << std::endl;
}
void printout(C c){
std::cout << "class val : " << c.val << std::endl;
}
int main()
{
C c;
printout(1);
printout(c);
//printout(C()); // ok, I can understand it.
//printout(C().changeVal(0)); /// ?????
return 0;
}
As you can see, function 'printout' is for printing out the input argument. My question is, when I use the int value(default datatype), then I just put in the function real number '1', however, when I use my class instance('class C'), then I have to declare my class instance before the function.
So, is there any way to make this kind of non-default datatype for function argument in 1-line?
The actual situation is, I have to use the 4x4 matrix as argument for some function. For this reason, I have to make some matrix, and initialize(make zero) that, and use it. But if I can do this same job with just 1 line, my source code will be more clear than now.
That's my question. I hope you could understand my question. Thank you.
Upvotes: 0
Views: 94
Reputation: 227418
You can pass a temporary:
printout(C());
Note that, since you don't need a copy of the C
parameter, it would make more sense to pass it by const reference:
void printout(const C& c) { ... }
Upvotes: 2