Reputation: 89
Can I build parameter const reference inside the method call? I know it compiles, but am not completely confident it is correct at run time.
I have the following classes:
class A{
public:
void doThings(const B& b);
}
class B{
B(int i, int j);
}
int main{
A a;
a.doThings(B(1,2)); //Is this OK?
}
Upvotes: 0
Views: 93
Reputation: 85541
The concept is fine... a temporary B
will be constructed and passed by reference. Don't store that reference in A
or use anywhere beyond the scope of doThings
.
But your code is no good. Here, I fixed it for you:
class B;
class A {
public:
void doThings(const B& b) {}
};
class B {
public:
B(int i, int j) {}
};
int main() {
A a;
a.doThings(B(1, 2)); //Is this OK?
}
Upvotes: 2