What's the best way to do this...? (templates/inheritance problem)

Discussion in 'C++' started by fungus, Mar 19, 2008.

  1. fungus

    fungus Guest

    I've got a big template class "A" and I want to write another template
    "B" which is exactly the same as A except for one method (let's call
    it "foo()").

    Is there a way to do this without complete copy/paste of "A"? I'd like
    to do it with only one copy of the common code.

    nb. foo() can't be static, it needs access to class members....

    template <class T> class A {
    void foo();
    };

    template <class T> class B {
    void foo(); // I want a different "foo()" here...
    };



    On a related note, something which came up while I was messing around
    with this is that when I inherit from a class ("A"), all the
    constructors in "A" are hidden.

    class A {
    public:
    A(int);
    };

    class B : public A {
    public:
    // Compiler writes a constructor for me, hiding A(int)...
    };

    main() {
    // Doesn't work...compiler says 'int' can't be converted to 'const
    B&'
    B b(42);
    }

    How can I make A's constructors visible? I read the faq and tried
    adding
    a "using A::A", but it makes no difference...

    class B : public A {
    public:
    using A::A; // Makes no difference...
    };


    faq: http://www.parashift.com/c -faq-lite/strange-inheritance.html#faq-23.9
     
    fungus, Mar 19, 2008
    #1
    1. Advertising

  2. fungus wrote:
    > I've got a big template class "A" and I want to write another template
    > "B" which is exactly the same as A except for one method (let's call
    > it "foo()").
    >
    > Is there a way to do this without complete copy/paste of "A"? I'd like
    > to do it with only one copy of the common code.


    Is that a trick question? The first rule of the class hierarchy:
    if two classes are very similar except for some minor differences,
    extract the common portion into a separate class and make your two
    classes inherit that.

    It goes the same for class templates. The common functionality has
    to be extracted into another template. Both class templates inherit
    from that [base] template.

    >
    > nb. foo() can't be static, it needs access to class members....
    >
    > template <class T> class A {
    > void foo();
    > };
    >
    > template <class T> class B {
    > void foo(); // I want a different "foo()" here...
    > };
    >
    >
    >
    > On a related note, something which came up while I was messing around
    > with this is that when I inherit from a class ("A"), all the
    > constructors in "A" are hidden.


    Not hidden. The constructors are special function that are never
    _inherited_ .

    >
    > class A {
    > public:
    > A(int);
    > };
    >
    > class B : public A {
    > public:
    > // Compiler writes a constructor for me, hiding A(int)...
    > };
    >
    > main() {
    > // Doesn't work...compiler says 'int' can't be converted to 'const
    > B&'
    > B b(42);
    > }
    >
    > How can I make A's constructors visible?


    You cannot. You can only create a constructor in 'B' that will
    take one argument and simply forward it to the base class' c-tor.
    See "initialisation of base classes" in your favourite C++ book.

    > I read the faq and tried
    > adding
    > a "using A::A", but it makes no difference...
    >
    > class B : public A {
    > public:
    > using A::A; // Makes no difference...
    > };
    >
    >
    > faq:
    > http://www.parashift.com/c -faq-lite/strange-inheritance.html#faq-23.9


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 19, 2008
    #2
    1. Advertising

  3. fungus

    fungus Guest

    Re: What's the best way to do this...? (templates/inheritanceproblem)

    On Mar 19, 6:23 pm, "Victor Bazarov" <> wrote:
    > > B is exactly the same as A except for one method.
    > > Is there a way to do this without complete copy/paste of "A"? I'd like
    > > to do it with only one copy of the common code.

    >
    > Is that a trick question?


    Not really, see below...

    > > when I inherit from a class "A", all the
    > > constructors in A are hidden.

    >
    > Not hidden. The constructors are special function that are never
    > _inherited_ .
    >


    Aha! So the correct answer to the first question is
    really "no, you can't just change a single function".

    To make it work I have to change the function *and*
    rewrite all the constructors in my derived classes.
     
    fungus, Mar 20, 2008
    #3
  4. fungus

    Kai-Uwe Bux Guest

    fungus wrote:

    > On Mar 19, 6:23 pm, "Victor Bazarov" <> wrote:
    >> > B is exactly the same as A except for one method.
    >> > Is there a way to do this without complete copy/paste of "A"? I'd like
    >> > to do it with only one copy of the common code.

    >>
    >> Is that a trick question?

    >
    > Not really, see below...
    >
    >> > when I inherit from a class "A", all the
    >> > constructors in A are hidden.

    >>
    >> Not hidden. The constructors are special function that are never
    >> _inherited_ .
    >>

    >
    > Aha! So the correct answer to the first question is
    > really "no, you can't just change a single function".
    >
    > To make it work I have to change the function *and*
    > rewrite all the constructors in my derived classes.


    You can use templated constructors:

    class B : public A {

    B () : A () {}

    template < typename X >
    B ( X x ) : A ( x ) {}

    template < typename X, typename Y >
    B ( X x, Y y ) : A ( x, y ) {}

    ...

    B ( B const & other ) : A ( other ) {}


    };

    If all constructors of A take at most 2 arguments, the above would suffice.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Mar 20, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian Pilcher
    Replies:
    0
    Views:
    345
    Ian Pilcher
    Jul 23, 2005
  2. Der Andere
    Replies:
    3
    Views:
    615
    Der Andere
    Apr 20, 2004
  3. JKop
    Replies:
    3
    Views:
    492
  4. recover
    Replies:
    2
    Views:
    834
    recover
    Jul 25, 2006
  5. Jim Barrows
    Replies:
    0
    Views:
    297
    Jim Barrows
    Mar 12, 2008
Loading...

Share This Page