473,544 Members | 911 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

virtual base classes, templates, and static functions

Hello,
I searched for an answer to my question and found similar posts,
but none that quite addressed the issue I am trying to resolve.
Essentially, it seems like I need something like a virtual static
function (which I know is illegal), but, is there a way to provide
something similar? The class that is the target of my inquiry is a
template class that interfaces to one of several derived classes
through a pointer to a base class. The specific derived class that is
interfaced to depends on the template parameter provided. However, in
a few cases, I need to call a static (class) function associated with
the derived class when an objects of that class may not exist. Two
possibilities I have come up with are to 1) pass the static function
of the dervied class to the target class as a callback function or 2)
add the interface to the abstract base class, remove the static
designation of the function in the derived class, and use a half-
initialized object (ughh!) to access the function through a pointer to
the base class. Is there a better, more elegant way to do this?

Thanks,
Carl

Code example below.
template <typename T>
class Base
{
public:
// illegal to declare a virtual static function here
virtual void func1(T& a) = 0;
virtual void func2(T& b) = 0;
// ...
};

class Derived1 : public Base<int>
{
public:
static void Derived1Func(in t& x); // type T is int in this case
void func1(int& a);
void func2(int& b);
// ...
};

class XyzType
{
XyzType() {}
~XyzType() {}
};

class Derived2 : public Base<XyzType>
{
public:
static void Derived2Func(Xy zType& x); // type T is XyzType here
void func1(int& a);
void func2(int& b);
// ...
};

template <typename T>
class Target
{
void aFunc(Base<T*pt r);
void bFunc(Base<T*pt r);
// ...
};

template <typename T>
void Target<T>::aFun c(Base<T*ptr)
{
T aT;

ptr->func1(T& aT); // great, works fine
};

template <typename T>
void Target<T>::bFun c(Base<T*ptr)
{

// now here I need to get a handle on Derived1Func or Derived2Func
// ... or DerivedNFunc depending on the T parameter, or some other
parameter
// if necessary. But I cannot call any of them directly because this
class
// primarily uses an interface to reference the specific derived
class
// (and does not know or care which derived class it is)
};
Dec 13 '07 #1
2 2394
cm********@yaho o.com wrote:
Hello,
I searched for an answer to my question and found similar posts,
but none that quite addressed the issue I am trying to resolve.
Essentially, it seems like I need something like a virtual static
function (which I know is illegal), but, is there a way to provide
something similar? The class that is the target of my inquiry is a
template class that interfaces to one of several derived classes
through a pointer to a base class.
That's what's happening, but it doesn't seem to be the design intent.
It interfaces with the base class object via a pointer to it. Whoever
is hiding behind that pointer should be immaterial to your "target"
class, if everything is designed correctly.
The specific derived class that is
interfaced to depends on the template parameter provided. However, in
a few cases, I need to call a static (class) function associated with
the derived class when an objects of that class may not exist.
Here the design breaks. If your "target" class has to know what the
derived class is, then you cannot claim that it interfaces through
the base class.
Two
possibilities I have come up with are to 1) pass the static function
of the dervied class to the target class as a callback function or 2)
add the interface to the abstract base class, remove the static
designation of the function in the derived class, and use a half-
initialized object (ughh!) to access the function through a pointer to
the base class.
Sound like in the latter case you're concerned that 'Target::aFunc'
or 'Target::bFunc' can be called for partially constructed object.
That's a major limitation.

The function pointer in the case of partially constructed object
might be just as bad an idea as a virtual function. What is the
role of the static 'DerivedNFunc'?

Another question: can more than one of DerivedN classes inherit from
the same Base specialisation?

I guess I am still struggling with the fact that you need to have
and call the 'DerivedNFunc' in the 'Target' that isn't supposed to
know anything about the derived class (since all of its interface
is designed to use Base, at least so far)...
Is there a better, more elegant way to do this?

Thanks,
Carl

Code example below.
template <typename T>
class Base
{
public:
// illegal to declare a virtual static function here
virtual void func1(T& a) = 0;
virtual void func2(T& b) = 0;
// ...
};

class Derived1 : public Base<int>
{
public:
static void Derived1Func(in t& x); // type T is int in this case
void func1(int& a);
void func2(int& b);
// ...
};

class XyzType
{
XyzType() {}
~XyzType() {}
};

class Derived2 : public Base<XyzType>
{
public:
static void Derived2Func(Xy zType& x); // type T is XyzType here
void func1(int& a);
void func2(int& b);
// ...
};

template <typename T>
class Target
{
void aFunc(Base<T*pt r);
void bFunc(Base<T*pt r);
// ...
};

template <typename T>
void Target<T>::aFun c(Base<T*ptr)
{
T aT;

ptr->func1(T& aT); // great, works fine
};

template <typename T>
void Target<T>::bFun c(Base<T*ptr)
{

// now here I need to get a handle on Derived1Func or Derived2Func
// ... or DerivedNFunc depending on the T parameter, or some other
parameter
// if necessary. But I cannot call any of them directly because this
class
// primarily uses an interface to reference the specific derived
class
// (and does not know or care which derived class it is)
};
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 13 '07 #2
On Dec 13, 10:16 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
cmonthe...@yaho o.com wrote:
Hello,
I searched for an answer to my question and found similar posts,
but none that quite addressed the issue I am trying to resolve.
Essentially, it seems like I need something like a virtual static
function (which I know is illegal), but, is there a way to provide
something similar? The class that is the target of my inquiry is a
template class that interfaces to one of several derived classes
through a pointer to a base class.

That's what's happening, but it doesn't seem to be the design intent.
It interfaces with the base class object via a pointer to it. Whoever
is hiding behind that pointer should be immaterial to your "target"
class, if everything is designed correctly.
No, it is clearly the intent of the design to interface with any
object derived from the abstract base class through the base class'
interface.

The specific derived class that is
interfaced to depends on the template parameter provided. However, in
a few cases, I need to call a static (class) function associated with
the derived class when an objects of that class may not exist.

Here the design breaks. If your "target" class has to know what the
derived class is, then you cannot claim that it interfaces through
the base class.
Well, the design does not break per se. It is not complete. It is the
reason for my post. I need to find a way to call a static function
associated with the derived class I am interfacing to through the
abstract base class interface. It would be ideal (?) if that call
could take place through an additional function in the interface, but
it does not seem possible for a static function.

Two
possibilities I have come up with are to 1) pass the static function
of the dervied class to the target class as a callback function or 2)
add the interface to the abstract base class, remove the static
designation of the function in the derived class, and use a half-
initialized object (ughh!) to access the function through a pointer to
the base class.

Sound like in the latter case you're concerned that 'Target::aFunc'
or 'Target::bFunc' can be called for partially constructed object.
That's a major limitation.
I do not like option #2 and am looking for a better solution.
The function pointer in the case of partially constructed object
might be just as bad an idea as a virtual function. What is the
role of the static 'DerivedNFunc'?

No, it is not so bad. The static function is a hash function that
takes a key (the T parameter)
as a parameter and returns a hash value. The hash value is then used
to retrieve an item from a hash table and, if found, is returned to
the caller. The hash function only operates on the key and therefore
does not require the derived class to be instantiated and, as I
indicated, in some cases it will not be instatiated. The derived
classes represent different items that can be stored in different
instantiations of the "target" class.

Another question: can more than one of DerivedN classes inherit from
the same Base specialisation?
The abstract base class represents the minimal interface needed by the
"target" class to interface to a particular item. Each of the n
derived (item) classes inherits from the same abstract base class.

I guess I am still struggling with the fact that you need to have
and call the 'DerivedNFunc' in the 'Target' that isn't supposed to
know anything about the derived class (since all of its interface
is designed to use Base, at least so far)...
Well, I'm struggling too :-). I am trying to come up with a good
solution to this issue, but so far passing in a callback function that
can be invoked at any point (without requiring a derived object to be
instantiated) seems to be the "best" solution that I can think of.
However, it seems to be me that a more clever solution might be found.
If you have any ideas, I would be interested in hearing them.

>
Is there a better, more elegant way to do this?
Thanks,
Carl
Code example below.
template <typename T>
class Base
{
public:
// illegal to declare a virtual static function here
virtual void func1(T& a) = 0;
virtual void func2(T& b) = 0;
// ...
};
class Derived1 : public Base<int>
{
public:
static void Derived1Func(in t& x); // type T is int in this case
void func1(int& a);
void func2(int& b);
// ...
};
class XyzType
{
XyzType() {}
~XyzType() {}
};
class Derived2 : public Base<XyzType>
{
public:
static void Derived2Func(Xy zType& x); // type T is XyzType here
void func1(int& a);
void func2(int& b);
// ...
};
template <typename T>
class Target
{
void aFunc(Base<T*pt r);
void bFunc(Base<T*pt r);
// ...
};
template <typename T>
void Target<T>::aFun c(Base<T*ptr)
{
T aT;
ptr->func1(T& aT); // great, works fine
};
template <typename T>
void Target<T>::bFun c(Base<T*ptr)
{
// now here I need to get a handle on Derived1Func or Derived2Func
// ... or DerivedNFunc depending on the T parameter, or some other
parameter
// if necessary. But I cannot call any of them directly because this
class
// primarily uses an interface to reference the specific derived
class
// (and does not know or care which derived class it is)
};

V
Thanks,
Carl
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -
Dec 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
5381
by: Stijn Oude Brunink | last post by:
Hello, I have the following trade off to make: A base class with 2 virtual functions would be realy helpfull for the problem I'm working on. Still though the functions that my program will use a lot are the ones that are virtual and thus will slow down the calculation, at least that is what what I have read on several places on the...
4
1830
by: Martin | last post by:
Greetings I want to have virtual member functionality, but without my member functions being virtual:-) As of yet this is all just in my head cause I can't see a nice solution yet so lets start with some simple code.
11
4333
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining it inline. Like this.
11
3410
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and exception neutral/ I got a reply from Bux with his code for a smart pointer with far fewer lines of code and more cleaner design, not over engineered...
5
11336
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget to pass all the necessary object files to the linker. Neither of those are my problem. Please bear with me as the question I ask is rather long and...
15
3468
by: Philipp | last post by:
Hello I don't exactly understand why there are no static virtual functions. I would have liked something like this: class Base{ static virtual std::string getName(){ return "Base"; } }
17
3511
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
14
2320
by: Hunk | last post by:
Hi I ws wondering if there is a way to implement operator+ in case of virtual classes. Here's the problem. I have to have a base string class from which two classes (normal char string and a hash string class ) are derived. The two derived classes are template classes specifying the sizes. The base class is a non-template class so that it...
2
2301
by: Markus Dehmann | last post by:
I need a simple object serialization, where loading an object from file looks like this: Foo* foo1 = FooFactory::create("./saved/foo1.a321f23d"); Foo* foo2 = FooFactory::create("./saved/foo2.eb287ac8"); Now, Foo is an abstract base class, and FooFactory contains a static function which again calls static create functions on Foo1 or Foo2...
0
7429
marktang
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...
0
7368
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...
0
7777
jinu1996
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...
1
7384
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...
0
7713
tracyyun
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...
0
4922
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...
0
3420
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...
0
3414
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
671
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.