Inheriting from template class
Note that those are completely different classes, which have nothing in common except for the fact that they were generated from the same class template. Since Area is not a class, you cannot derive the class Rectangle from it.
You only can derive a class from another class or several of them. However you have to specify which classed to derive from when you define Rectangle. This is true no matter whether those classes are generated from a template or not. Two objects of the same class simply cannot have different inheritance hierarchies. It may be that you want to have a single type Rectangle so that you can pass all sorts of Rectangle to the same function which itself doesn't need to know the Area type.
However you can make use of multiple inheritance here:. If it is important that your generic Rectangle is derived from a generic Area you can do the same trick with Area too:. EDIT: Following the clarification, it seems you're actually trying to make Rectangle a template as well, in which case the following should work:.
Rectangle will have to be a template, otherwise it is just one type. It cannot be a non-template whilst its base magically is. Its base may be a template instantiation , though you seem to want to maintain the base's functionality as a template. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.
Learn more. Asked 10 years ago. Active 4 years, 1 month ago. Viewed k times. How do I specialize the inherited Area type for Rectangle objects? V -- Please remove capital As from my address when replying by mail. I am starting to think that my error may be somewhere else. W Marsh.
You are typing semicolins after those class definitions, yes? I do mean semicolins. It's partialdave's younger bronger. Yes indeed I have them, one thing I have done many times is to forget them, as I did above.
Jarrad wrote: Yes indeed I have them, one thing I have done many times is to forget them, as I did above. I think it all is covered in the FAQ 5. Follow its recommendations. This discussion thread is closed Start new discussion. Inheriting operator and templates. Inheriting Webforms in C. Inheriting a vector.
Viewed 11k times. Usually it's done the other way round. But the pattern you show, can be used for policy or interface injection. Books or articles about them have, but not them. Are you implying that every abstract question lacking an example is too broad?
Actually, I sometimes receive wrong answers to my questions because I added examples; i. I get answers targetting my examples instead of the question, so skipping an example is sometimes actually better. OK convinced, I retracted my close vote. Show 8 more comments. Active Oldest Votes. Use cases are: Policy-based design Curiously recurring template pattern Barton—Nackman trick In general you have the benefits from dynamic polymorphism, without the extra runtime costs of virtual functions.
Add a comment. It seems to be some kind of waste to both inherit and have a member of the same type, why not just call the parent member function T::f? Good point, but your example doesn't justify the inheritance.
It could be composition. Otherwise, you would have to specifically inherit the desired policies, i. Using templates, you define a single templated base class, and the code becomes more elegant and much more easier to maintain e.
0コメント