A dirty way to reuse a temporary in the constructor initialization is to add a dummy parameter. I'm not keen on this technique, but is the only one I know to give the temporary a name. I tend to redesign my classes to avoid this struct A { A( T1 temporary_ = T1() ) : t2( foo( temporary_ ) ) , t3( bar( temporary_ ) ) T2 const t2; T3 const t3; }.
A dirty way to reuse a temporary in the constructor initialization is to add a dummy parameter. I'm not keen on this technique, but is the only one I know to give the temporary a name. I tend to redesign my classes to avoid this.
Struct A { A( T1 temporary_ = T1() ) : t2( foo( temporary_ ) ) , t3( bar( temporary_ ) ) T2 const t2; T3 const t3; }.
Well, I can honestly say that I would've never thought of that. And if somebody really wants to pass me a T1() it wouldn't really hurt, I guess. – bitmask Nov 4 at 22:43 @bitmask: That's the main problem with it, that it changes the interface the user sees.
If it doesn't hurt your design, then its just an acceptable side-effect. – K-ballo Nov 4 at 22:44 I don't really like default function parameters in public headers (too many issues with versioning+recompiling+ODR, overrides and overloads IMHO), but it sure starts to look like one of the realistic options, given all the extra ifs and buts the OP has added... +1 – sehe Nov 4 at 22:47 @sehe: Indeed. I would love to hear about an alternative technique.
Most of the time I end up designing inner classes to hold both members and provide appropiate construction. – K-ballo Nov 4 at 22:49.
Const-references may be defaulted to a temporary, as of recently (I forget whether that's a C++03 or C++11 addition): A::A(const T & t = T()) : a(t), b(t) { } // ^^^^^^ (Maybe declare that constructor explicit, just in case. ).
3 That's valid C++03 as well. – K-ballo Nov 4 at 22:43 @K-ballo: I might actually be confusing this with default template arguments for function templates, coming to think of it. Oh well.
Maybe someone can clarify to which standard this conforms. – Kerrek SB Nov 4 at 22:45.
I'd solve this by creating a factory method // for arbitrary T1, T2, T3 T2 foo(T1 const&); T3 bar(T1 const&); struct A { T2 const t2; T3 const t3; static A createA() { return A(T1()); } private: A(const T1& t) : t2(foo(t)), t3(bar(t)) {} }.
I'm in the middle of a deep and complex class hierarchy with non-trivial constructors. – bitmask Nov 4 at 22:41.
I'd avoid this situation at all costs, but if I had to, I'd add an extra member: struct A { T1 temp_; T2 const t2; T3 const t3; A() : temp_(), t2(foo(temp_)), t3(bar(temp_)) {} }.
That's really the perversion of it, as a matter of fact I already have such a member, but I need to pass the temporary to my parent class, which I have to initialise first (I tried to make the question as abstract and simplified as possible, but it appears I cut out too much; sorry for that! ). – bitmask Nov 4 at 22:58 Then I'd go with storing the value in the parent class, and referencing it subsequently via the base type: A() : base(T1()), t2(foo(base::m_t1)), t3(bar(base::m_t1)) {}.
But, realize that you may have a problem with the lifetime of the temporary if base::base(...) takes the address of the temporary. – Rob Nov 4 at 23:04 @bitmask -- Oh, I see from your other comments that you actually don't need the same temporary object, you only need the same value. Ignore my comment about lifetime.
– Rob Nov 4 at 23:07 @bitmask: but I need to pass the temporary to my parent class - mmm I'm starting to feel your codebase has big design problems. Surely, one class hierarchy cannot suffer from all the design ailments at the same time? Conversely, if all that complexity is required, then the cost is also in order?
Use factory patterns, repository patterns, builder patterns, composite, flyweights, IoC, whatever exists. Yes it will cost, but such is life. – sehe Nov 4 at 23:14 @sehe: I understand that it looks like that, and there are some parts nobody is particularly proud of, but I think this point has a quite logical design.
And to your complexity point: factories, templates, dynamic binding, flyweights and to some extent IoC are in place for just the construction this constructor is involved in (one of the reasons btw. I left out information). – bitmask Nov 4 at 23:27.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.