Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What's the best way to do this...? (templates/inheritance problem)

Reply
Thread Tools

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

 
 
fungus
Guest
Posts: n/a
 
      03-19-2008
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-lit....html#faq-23.9


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-19-2008
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-lit....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


 
Reply With Quote
 
 
 
 
fungus
Guest
Posts: n/a
 
      03-20-2008
On Mar 19, 6:23 pm, "Victor Bazarov" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      03-20-2008
fungus wrote:

> On Mar 19, 6:23 pm, "Victor Bazarov" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best way to loop through ArrayList and remove elements on the way? Kevin Java 16 01-30-2008 08:54 PM
way way way OT: MCNGP Announcement Neil MCSE 174 04-17-2006 05:55 PM
Any way/Best way to simulate 3:2 mode on an S2 IS? Joe Digital Photography 0 03-09-2006 05:35 PM
AMD Opteron: 1-way, 2-way, ... Up to 8-way. John John Windows 64bit 12 12-27-2005 08:17 AM
What is the best way to make dual-way xml databinding? Stan ASP .Net 3 05-05-2005 02:07 PM



Advertisments