inherit constructor

I've searched google and looks around and found that it's possible to inherit a constructor from base class. But I simply don't know the syntax. I think the pattern is something like this code bellow.

1
2
3
4
5
6
7
8
9
10
11
class B {
public:
	B(){
	}
private:
	int haha;
};

class I : public B {
	using B::B;
};


but this code produce error in my compiler

error: 'B:B' name constructor

FYI, My compiler is codeblocks 10.05 no patch to the gcc and using C++0x

So what's the correct syntax to inherit class constructor ?

Maybe you think about calling the base constructor:
1
2
3
4
5
6
7
8
9
10
11
12
13
class B {
public:
	B(){
	}
private:
	int haha;
};

class I : public B {
public:
	I(): B() {
	}
};

http://stackoverflow.com/questions/4417797/why-is-inheriting-constructors-not-supported-by-all-of-the-existing-c-compiler

The compiler you're using is likely not to have full support yet. In fact I do not know of any compiler will full 100% C++11 support yet.
I think that solves the problem but if there is a parameter in the constructor
Hmmm something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class B{
public:
    B( int a, int b, int c ){ 
           d = a; 
           e = b;
           f = c;
    }
private:
    int d,e,f;
}

class I {
    I( int a, int b, int c ) : B( a,b,c ) {} 
}


is there better ways to do this ?
because if the base class constructor parameter change then all the derived class inheriting the class consturctor this way must change and it's an extra work in every work.
You could just forward everything

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class B{
public:
    B( int a, int b, int c )
    : d(a), e(b), f(c) { }
private:
    int d,e,f;
};

class I : public B {
public:
    template<typename ...A>
    I(A&& ...a)
    : B(std::forward<A>(a)...) {}
};
g++ - Delegating constructors GCC 4.7, Inheriting constructors GCC 4.8
http://gcc.gnu.org/projects/cxx0x.html

clang - Delegating constructors Clang 3.0, Inheriting constructors Not implemented
http://clang.llvm.org/cxx_status.html
What's with the C++11...
Doesn't this inheriting consturctor exist before C++0x ?
and yes my compiler is capable of doing it...
So don't mind the version

1
2
3
4
5
class I : public B {
public:
	I(): B() {
	}
};


the code above can run without problem

I just need the solution for inheriting the consturctor without knowing the parameter to the consturctor

and I don't know about std::forward. Never heard of it and have to search about it...

is the forward version like this ?
1
2
3
4
5
6
class I : public B {
public:
    template<typename X, typename Y, typename Z>
    I(X&& a, Y&& b, Z&& c)
    : B(std::forward<X>(a),std::forward<Y>(b),std::forward<Z>(c)) {}
};



templating and inheriting is the things I don't want to combine. They produces obsecure error message if there is one and I hate it but somehow have to deal with it.

> Doesn't this inheriting consturctor exist before C++0x ?

http://www.stroustrup.com/C++11FAQ.html#inheriting


> templating and inheriting is the things I don't want to combine.
> They produces obsecure error message if there is one

They can also cause unexpected or unintuitive behaviour unless one is careful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct B
{
    B( int a, int b, int c ) : d(a), e(b), f(c) {}
    private: int d,e,f;
};

struct I : B
{
    template< typename... A >
        I( A&& ...a ) : B(std::forward<A>(a)...) {}

    I( double a, int b, char c ) : B( int( std::ceil(a) + 0.1 ), b, c ) {}
    I( const std::string& n, int a, int b, int c ) : B(a,b,c), name(n) {}

    const std::string name = "anonymous" ;
};

int main()
{
    I a( 2.6f, 1, 2 ) ; // un-intuitive

    I b( std::string("a name"), 1, 2, 3 ) ; // *** error
}

Thanks for the helps...
I am sorry, I am not too well informed...

This part give error message
1
2
    template< typename... A >
        I( A&& ...a ) : B(std::forward<A>(a)...) {}

Maybe my compiler is just not new enough

Maybe copy paste produces less stress for my brain than inheriting.
Topic archived. No new replies allowed.