Hello all,
I think the problem I'm facing is hard so I wonder if someone faced it
before and if there is a simple solution to it. I am creating a
program that basically takes all the required information at runtime
after reading a file. After reading the file, several objects of a
templated ClassA are created. I thought that the way to do this is to
create an abstract class AbstractClassA and have all templated classes
inherit from the Abstract class. Doing this I can work with pointers
to the base class. In code:
AbstractClassA {
...
...
virtual ~AbstracClassA = 0;
};
template<class B, class ATraits>
class classA : puclic AbstractClassA, public ATraits {
// overide base class member functions
...
// add other functions specific to the class
...
// some functions that depend on the template parameter
B someFun1(B&) {
...
}
B someFun2() {
...
}
};
Now, the problem is that I cannot call an additional function of
classA that is not in the interface of the abstract class because the
compiler complains (as it should). For example, I cannot call
typedef classA<int,IntT raitscaint;
AbstractClassA* pa = new caint();
int i1 = 10;
pa->someFun1(i1) ; // error, someFun1 is not defined in AbstractClassA
Of course, I cannot declare this function in the abstract class,
because the function depends on a template parameter that doesn't even
exist at that point.
One dirty solution to this is to depend on the RTTI and use
dynamic_cast against every single instantiation of ClassA. However,
imagine that I have 100 of these different instances! That's why I
templetized the class in the first place, I don't want to do any
dynamic_cast on it.
So I guess my question is if my logic right or I'm missing something
here? if it is right, is my design right? or there is a better
approach to do what I want to do?
Thank you all,
a² 4 2029
aaragon wrote:
Hello all,
I think the problem I'm facing is hard so I wonder if someone faced it
before and if there is a simple solution to it. I am creating a
program that basically takes all the required information at runtime
after reading a file. After reading the file, several objects of a
templated ClassA are created. I thought that the way to do this is to
create an abstract class AbstractClassA and have all templated classes
inherit from the Abstract class. Doing this I can work with pointers
to the base class. In code:
AbstractClassA {
...
...
virtual ~AbstracClassA = 0;
};
Your base class AbstractClassA is pretty useless this way. Unless _this_ class
contains some pure virtual method declarations, there is no point in using it.
template<class B, class ATraits>
class classA : puclic AbstractClassA, public ATraits {
// overide base class member functions
...
// add other functions specific to the class
...
// some functions that depend on the template parameter
B someFun1(B&) {
...
}
B someFun2() {
...
}
};
Now, the problem is that I cannot call an additional function of
classA that is not in the interface of the abstract class because the
compiler complains (as it should). For example, I cannot call
Right. You can only call methods declared in AbstractClassA.
You are mixing two design concepts of C++ that almost are mutual exclusive.
Virtual inheritance is used to device at run-time which functionality should be
used. In contrast to this, templates are used when you know at compile-time
which functionality you want but don't want to code a similiar algorithm for
various data types (this is the most probable scenario for using templates, of
course there are cases where templates depend on other parameters than types).
You cannot mix these two concepts!
In your case it looks as if the class that should contain the virtual methods
should be the class that you pass as template parameter B to the template
classA. We can only give you a proper assessment of your design if you tell us
what you are trying to do (unless this is confidental :-)
Regards,
Stuart
On Nov 21, 2:28 pm, aaragon <alejandro.ara. ..@gmail.comwro te:
Hello all,
I think the problem I'm facing is hard so I wonder if someone faced it
before and if there is a simple solution to it. I am creating a
program that basically takes all the required information at runtime
after reading a file. After reading the file, several objects of a
templated ClassA are created. I thought that the way to do this is to
create an abstract class AbstractClassA and have all templated classes
inherit from the Abstract class. Doing this I can work with pointers
to the base class. In code:
AbstractClassA {
...
...
virtual ~AbstracClassA = 0;
virtual sometypethatdoe sntneedtemplate s
someFun1(somety pethatdoesntnee dtemplates&);
>
};
template<class B, class ATraits>
class classA : puclic AbstractClassA, public ATraits {
// overide base class member functions
...
// add other functions specific to the class
...
// some functions that depend on the template parameter
You can't get these from an AbstractClassA pointer:
B someFun1(B&) {
...
}
B someFun2() {
...
}
};
Now, the problem is that I cannot call an additional function of
classA that is not in the interface of the abstract class because the
compiler complains (as it should). For example, I cannot call
typedef classA<int,IntT raitscaint;
AbstractClassA* pa = new caint();
int i1 = 10;
pa->someFun1(i1) ; // error, someFun1 is not defined in AbstractClassA
Of course, I cannot declare this function in the abstract class,
because the function depends on a template parameter that doesn't even
exist at that point.
One dirty solution to this is to depend on the RTTI and use
dynamic_cast against every single instantiation of ClassA. However,
imagine that I have 100 of these different instances! That's why I
templetized the class in the first place, I don't want to do any
dynamic_cast on it.
So I guess my question is if my logic right or I'm missing something
here? if it is right, is my design right? or there is a better
approach to do what I want to do?
Thank you all,
a²
On Nov 21, 6:28 am, aaragon <alejandro.ara. ..@gmail.comwro te:
I think the problem I'm facing is hard so I wonder if someone faced it
before and if there is a simple solution to it.
I had a similar problem but only because I wanted to put the different
templated classes in same datastructure.
In code:
AbstractClassA {
...
...
virtual ~AbstracClassA = 0;
};
template<class B, class ATraits>
class classA : puclic AbstractClassA, public ATraits {
// overide base class member functions
...
// add other functions specific to the class
...
// some functions that depend on the template parameter
B someFun1(B&) {
...
}
B someFun2() {
...
}
};
Now, the problem is that I cannot call an additional function of
classA that is not in the interface of the abstract class because the
compiler complains (as it should).
For my particular problem, I found the visitor design pattern helped.
Saul
On Nov 22, 8:10 am, shaz...@gmail.c om wrote:
On Nov 21, 6:28 am, aaragon <alejandro.ara. ..@gmail.comwro te:
I think the problem I'm facing is hard so I wonder if someone faced it
before and if there is a simple solution to it.
I had a similar problem but only because I wanted to put the different
templated classes in same datastructure.
In code:
AbstractClassA {
...
...
virtual ~AbstracClassA = 0;
};
template<class B, class ATraits>
class classA : puclic AbstractClassA, public ATraits {
// overide base class member functions
...
// add other functions specific to the class
...
// some functions that depend on the template parameter
B someFun1(B&) {
...
}
B someFun2() {
...
}
};
Now, the problem is that I cannot call an additional function of
classA that is not in the interface of the abstract class because the
compiler complains (as it should).
For my particular problem, I found the visitor design pattern helped.
Saul
That is exactly what I'm using now. I can use an abstract base class
for the common part of the classes, and then use a visitor to work
with specific functions of the derived template clases. It is actually
very cool stuff. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mike Meng |
last post by:
hi all,
I'm a newbie Python programmer with a C++ brain inside. I have a
lightweight framework in which I design a base class and expect user to
extend. In other part of the framework, I heavily use the instance of
this base class (or its children class). How can I ensure the instance
IS-A base class instance, since Python is a fully dynamic typing
language?
I searched and found several different ways to do this:
|
by: Stuart Golodetz |
last post by:
Hi,
I've got a minor casting issue which I need to check about (marked // <--).
I was trying to do a static_cast on it (which didn't work, though I'm not
sure why this should be the case?) I also tried reinterpret_cast (which is
clearly an exceedingly dodgy thing to do; it worked, but I'm not sure
whether it should have worked, or whether (the more likely scenario) it was
just coincidence?) Finally, after a bit of trawling through the...
|
by: Mike Frayn |
last post by:
Hi there. I'm wondering how to achieve a particular
functionality using virtual functions and class
inheritance... I don't know if its possible, but I figured
that I would give it a shot.
What I want is to have a base class with particular member
data and a virtual function that will be overloaded by the
inheriting class.
class CBaseClass
|
by: JKop |
last post by:
I'm starting to think that whenever you derive one class from another, that
you should use virtual inheritance *all* the time, unless you have an
explicit reason not to. I'm even thinking that there shouldn't have been a
"virtual" keyword for this purpose, but instead, a "nonvirtual" keyword!
In teaching inheritance, you see the common example:
class Vehicle {}
|
by: Scott |
last post by:
Hi All,
Am I correct in assuming that there is no way to have a base pointer to an
object that uses multiple inheritance?
For example,
class A { /* ... */ };
class B { /* ... */ };
| |
by: bsruth |
last post by:
I tried for an hour to find some reference to concrete information on
why this particular inheritance implementation is a bad idea, but
couldn't. So I'm sorry if this has been answered before. Here's the
scenario:
We have a base class with all virtual functions. We'll call this the
Animal class. We then make two classes Fish and Bird that both inherit
from Animal. In the program, we have a single array of Animal pointers
that will...
|
by: axel22 |
last post by:
Please observe this simple model of multiple inheritance:
void main() {
class A {
public:
virtual void print() { cout << "A" << endl;
};
class Support1 : virtual public A {
|
by: mijobee |
last post by:
I'm very new to c++ and just writing some code to learn. I've run into
a problem, with a javaish design, and want to know if there is any
possible solution without modifying the design. I've read up on
virtual inheritance and from my understanding this should work fine but
I haven't found any docs that use such a tangled example. The gcc
output containing the errrors:
Example.cpp: In member function 'virtual IObject*...
|
by: Dave Rahardja |
last post by:
Since C++ is missing the "interface" concept present in Java, I've been using
the following pattern to simulate its behavior:
class Interface0
{
public:
virtual void fn0() = 0;
};
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |