Hello all, I'm wondering if it's valid for a templated class to use a
template parameter as a base class.
Basically I have two abstract types, one of which is derived from the
other, and I have an implementation for those types. Note however
that their implementations are practically the same, only the two
abstract types are different only in the bit that is derived from the
other:
class Closure {
public:
virtual int& operator[](size_t i) =0;
virtual int const& operator[](size_t i) const =0;
};
class KClosure: public Closure {
public:
bool reusable;
void banreuse(){
reusable = 0;
}
KClosure() : reusable(1), Closure() {}
//KClosure doesn't add any virtual functions,
//but does add a few non-virtuals
};
Now what I tried in g++ is:
template<typena me T, size_t N>
class ClosureArray : public T{
int dat[N];
public:
ClosureArray<T, N>() : T() {}
virtual int& operator[](size_t i){
return dat[i];
}
virtual int const& operator[](size_t i) const{
return dat[i];
}
}
g++ compiled the above in what seems to be what I expected it to work
(although I haven't done a very comprehensive test).
What I'd like to know is whether this is legal standard C++ and is
portable to a reasonably large number of non-g++ compilers. 3 1588
alan wrote:
>
class Closure {
public:
virtual int& operator[](size_t i) =0;
virtual int const& operator[](size_t i) const =0;
};
class KClosure: public Closure {
public:
bool reusable;
void banreuse(){
reusable = 0;
}
KClosure() : reusable(1), Closure() {}
//KClosure doesn't add any virtual functions,
//but does add a few non-virtuals
};
template<typena me T, size_t N>
class ClosureArray : public T{
int dat[N];
public:
ClosureArray<T, N>() : T() {}
virtual int& operator[](size_t i){
return dat[i];
}
virtual int const& operator[](size_t i) const{
return dat[i];
}
}
Err, anyway, the use case I tried was like this:
int main(void){
Closure* clos = new ClosureArray<Cl osure, 3>();
KClosure* kclos = new ClosureArray<KC losure, 5>();
}
Consider changing the interface of Closure so that it doesn't provide direct
access to implementation details (as it is it forces an int or collection of int
member on the implementation) .
I don't see a reasonable way to change that, it looks perfectly fine
to me... alf, what do you have in mind, just curious?
Thanks
On Jul 29, 2:33*pm, "Alf P. Steinbach" <al...@start.no wrote:
* alan:
Hello all, I'm wondering if it's valid for a templated class to use a
template parameter as a base class.
Basically I have two abstract types, one of which is derived from the
other, and I have an implementation for those types. *Note however
that their implementations are practically the same, only the two
abstract types are different only in the bit that is derived from the
other:
class Closure {
public:
* * virtual int& operator[](size_t i) =0;
* * virtual int const& operator[](size_t i) const =0;
};
class KClosure: public Closure {
public:
* * bool reusable;
* * void banreuse(){
* * * *reusable = 0;
* * }
* * KClosure() : reusable(1), Closure() {}
//KClosure doesn't add any virtual functions,
//but does add a few non-virtuals
};
Now what I tried in g++ is:
template<typena me T, size_t N>
class ClosureArray : public T{
* * int dat[N];
public:
* * ClosureArray<T, N>() : T() {}
* * virtual int& operator[](size_t i){
* * * * *return dat[i];
* * }
* * virtual int const& operator[](size_t i) const{
* * * * *return dat[i];
* * }
}
Consider changing the interface of Closure so that it doesn't provide direct
access to implementation details (as it is it forces an int or collectionof int
member on the implementation) .
Also consider using the keyword 'true' instead of numerical '1'.
Also consider ways to avoid dynamically changing "modes" of objects, suchas
KClosure::reusa ble, and anyway, not exposing such as public data members.
This is just sample code for what I was trying to explain; the array
entries are really Generic*, and Generic is the ultimate base class
for my hierarchy of types (which includes Closure as a derived class)
and they should really refer to each other using Generic*'s.
In an interface class a reference in a result type generally says "exposes
implementation, ungood".
clos[i] = something; seems shorter and more understandable to me than
clos.setElem(i, something);
For that matter, Closure is pretty much defined as a flat array of
pointers to other objects in my system.
>
g++ compiled the above in what seems to be what I expected it to work
(although I haven't done a very comprehensive test).
What I'd like to know is whether this is legal standard C++
That's a very broad question.
It's valid to derive from a class specified as a template parameter, yes.
and is
portable to a reasonably large number of non-g++ compilers.
Yes, it's the basis of several template programming idioms.
Thanks. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dave |
last post by:
Hello all,
I hope the context to my problem shown below is adequate but not
overwhelming. I tried to keep it minimal.
At the line indicated (in comment form) in the source below, the following
error is occurring (VC++ 7.1):
error C2663: 'std::_Tree<_Traits>::find' : 2 overloads have no legal
conversion for 'this' pointer
|
by: Walt Karas |
last post by:
The following gives an error in the declaration of the
member function x() of the class template Tpl, compiliing
with a recent version of GCC under Solaris:
class A { };
class B { };
template <typename Base>
class Tpl : protected Base
{
|
by: Arne Claus |
last post by:
I've got a tricky problem here.
I try to create an object-hierarchie, based on a template base class like
template <class T> class Node {
addChild(Node<T> *) =0;
// the Node-Container wil be defined in the base classes
};
The problem is, that the classes derived from Node represent different
forms of hierarchies, which all can use a different paramter T. This
|
by: Hartmut Sbosny |
last post by:
Hello NG,
I have a ordinary class `Base' with two functions `void foo()' and
`void any()':
class Base {
public:
Base() {}
void foo() {}
void any() {}
|
by: Chris |
last post by:
I am having a very strange problem involving virtual
functions in template classes. First of all, here is an
extremely simplified structure of the two classes I am
having problems with.
template<class Type> class base
{
public:
base& operator/=(const base&);
Type *image;
| |
by: Niels Dekker - no reply address |
last post by:
The following attempt to pass my template "Base" as a template template
argument was rejected by Microsoft VC++ 8.0 (2005), while it still works
on VC++ 7.1 (2003). Is it correct C++? And is there a workaround?
template <typename T> class Base
{
};
template <typename U, template <typename> class TempTemp>
class Derived;
|
by: valoh |
last post by:
Hi,
is this legal c++ code?
template <typename BaseTstruct A
{
BaseT& this_() { return *static_cast<BaseT*>(this); }
template <typename Tvoid Foo() { this_().Bar<T>(); }
template <typename Tvoid Bar() { }
|
by: wo3kie |
last post by:
#include <iostream>
#include <map>
#include <utility>
//
// Base
// / | \
// Derived1 Derived2 \
// \ | /
// Derived3
|
by: Clyde |
last post by:
Hi,
what i'm trying to do is:
/////////////// Code Start
template <class TType, int* p = 0>
class Template
{
public:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |