Let's say I have the classes:
class Rect {
int w, l;
Rect(width, length) : w(width), l(length) {}
}
class Box {
Rect &r; int h;
Box(int width, int length, int height);
}
For the sake of argument, I do not offer default values for width an height. Mainly because this is meant as a simplified example of what I'm actually doing.
Is there any where I can use the constructor of multiple arguments Rect(width,height)
to explicitly initialize Rect &r
? I know what I would do if the constructor had a single argument, I would simply go:
Box(int width, int length, int height) : r(loneArgument), h(height)
But now, I need to explicitly initialize with multiple arguments. However:
Box(int width, int length, int height) : r(width, height), h(height)
results in a compiler error. While we're at it, so does
Box(int width, int length, int height) : r(Rect(width, height)), h(height)
with the error
error: non-const lvalue reference to type 'Rect' cannot bind to a temporary of type 'Rect'
Any idea how I can get around this? Or at least any good reasons why this may be bad practice?
I understand that I can easily give default values for width and height to solve my issue, but I'm using a simpler code and in my actual code, default values wouldn't make any sense, since the actual object I want to define requires using a multiple input constructor.
So any ways to resolve this?