473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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(int& 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(XyzType& x); // type T is XyzType here
void func1(int& a);
void func2(int& b);
// ...
};

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

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

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

template <typename T>
void Target<T>::bFunc(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 2379
cm********@yahoo.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(int& 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(XyzType& x); // type T is XyzType here
void func1(int& a);
void func2(int& b);
// ...
};

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

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

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

template <typename T>
void Target<T>::bFunc(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...@comAcast.netwrote:
cmonthe...@yahoo.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(int& 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(XyzType& x); // type T is XyzType here
void func1(int& a);
void func2(int& b);
// ...
};
template <typename T>
class Target
{
void aFunc(Base<T*ptr);
void bFunc(Base<T*ptr);
// ...
};
template <typename T>
void Target<T>::aFunc(Base<T*ptr)
{
T aT;
ptr->func1(T& aT); // great, works fine
};
template <typename T>
void Target<T>::bFunc(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
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...
4
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...
11
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...
11
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...
5
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...
15
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
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;"...
14
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...
2
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 =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
agi2029
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,...

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.