Re: Constructor inheritance
On 7/31/2014 1:47 PM, Paavo Helde wrote:
Lynn McGuire <lmc@winsim.com> wrote in news:lrdqvc$o7a$1@dont-email.me:
On 7/31/2014 10:17 AM, Paavo Helde wrote:
I think in my current design I am a bit stuck with the fact that my
compiler does not yet support the C++11 feature of using base class
constructors. So is there any workaround for it? I can use any kind
of template trickery.
What I need is to define a bunch of related lightweight classes (no
virtual functions) which all have a collection of common constructors
(at least 6) and one or two specific constructors. So the goal is to
have all constructors present in the same class somehow. I guess
there is no other way than to spell out those 6 common constructors
in each leaf class, correct?
TIA
Paavo
I think that the answer is yes. But, I need to
see some very simple code. Very, very simple.
The most simple code exhibiting my situation I can think of:
#include <string>
#include <vector>
class A {};
template<int k>
class B: private A {
public:
B() {}
B(const A&) {}
B(A&&) {}
B(const B&) {}
B(B&&) {}
template<int k> B(const B<k>&) {}
template<int k> B(B<k>&&) {}
Won't compile, probably. You probably meant to use a different template
argument name here, as in
template<int j> B(const B<j>&) {}
, no? Meaning that you want it constructible from a different
instantiation of the template.
};
class C1: public B<1> {
public:
// specific constructor:
C1(const std::string&) {}
// common constructors:
C1() {}
C1(const C1& c): B<1>(c) {}
C1(C1&& c): B<1>(c) {}
C1(const ::A& a): B<1>(a) {}
C1(::A&& a): B<1>(a) {}
template<int m> C1(const B<m>& b): B<1>(b) {}
template<int m> C1(B<m>&& b): B<1>(b) {}
};
class C2: public B<2> {
public:
// specific constructor:
template<typename T> C2(const std::vector<T>&) {}
// common constructors:
C2() {}
C2(const C2& c): B<2>(c) {}
C2(C2&& c): B<2>(c) {}
C2(const ::A& a): B<2>(a) {}
C2(::A&& a): B<2>(a) {}
template<int m> C2(const B<m>& b): B<2>(b) {}
template<int m> C2(B<m>&& b): B<2>(b) {}
};
// etc, k = 3,4,...
int main() {
A a;
C1 c;
C1 cc(a);
C1 ccc(c);
C2 d;
C2 dd(a);
C2 ddd(d);
C2 dddd(c);
}
I would probably use a macro to duplicate all that code. You can easily
conditionally define that macro for a newer version of the compiler and
not even bother changing the code that uses it...
V
--
I do not respond to top-posted replies, please don't ask