Hello,
I fear I want to have something which is not possible in C++.
How is it possible to define a base class so that the derived
class is forced to contain a static member variable, which can
be used by static member functions of the base class?
Something like
virtual static XClass* pXClass;
/* pXClass shall be pure virtual, so not static in base class, but
static in derived class */
The static class related part of the derivation could so provide
manager functionality for the object instances of the class.
The only solution I currently see is to have a base class
containing
XClass* pXClass; /* non static */
and related member functions.
Derive a singleton class from this, so I would have some "static"
effect. And have a separate class XClass chained by the derived class.
Best regards,
Hubert 6 3506
Hubert Fritz wrote:
Hello,
I fear I want to have something which is not possible in C++.
How is it possible to define a base class so that the derived
class is forced to contain a static member variable, which can
be used by static member functions of the base class?
You might be able to do something with templates, but what problem are
you attempting to solve?
--
Ian Collins.
Ian Collins wrote:
Hubert Fritz wrote:
>Hello,
I fear I want to have something which is not possible in C++.
How is it possible to define a base class so that the derived class is forced to contain a static member variable, which can be used by static member functions of the base class?
You might be able to do something with templates, but what problem are
you attempting to solve?
My problem is more a theoretical one - to understand a) the limitations
of the language or b) my limitated understanding of
the language.
Given that you want to manage objects of a class, e.g. create
them, find them again by several keys, delete them. So you
might have an anchor, and chained the objects in a list.
Using a single class to implement this concept, you can have
the anchor static - it will belong to the class. The link
field in the class will be non-static and belongs to the
objects. Member functions manipulating the linked list
would be static for being used independently of particular
objects. Let us name this class ClassA.
Now imagine to generalise this and try to find a base class
ClassB gathering the algorithms and derive one, or several
classes with functionality such as ClassA, we may call them
ClassD1, ... ClassDn.
The static member (anchor) of ClassA is obviously needed in
the derived classes. Algorithms shall be inherited from ClassB.
For me it is not clear if there is a way to provide
or force the definition of the static member in ClassD1
by ClassB and use it from static member functions in
ClassB.
Hubert
Hubert Fritz wrote:
>
My problem is more a theoretical one - to understand a) the limitations
of the language or b) my limitated understanding of
the language.
Given that you want to manage objects of a class, e.g. create
them, find them again by several keys, delete them. So you
might have an anchor, and chained the objects in a list.
Using a single class to implement this concept, you can have
the anchor static - it will belong to the class. The link
field in the class will be non-static and belongs to the
objects. Member functions manipulating the linked list
would be static for being used independently of particular
objects. Let us name this class ClassA.
Now imagine to generalise this and try to find a base class
ClassB gathering the algorithms and derive one, or several
classes with functionality such as ClassA, we may call them
ClassD1, ... ClassDn.
The static member (anchor) of ClassA is obviously needed in
the derived classes. Algorithms shall be inherited from ClassB.
For me it is not clear if there is a way to provide
or force the definition of the static member in ClassD1
by ClassB and use it from static member functions in
ClassB.
I'm still not 100% clear what you are trying to achieve, but would
something like this do what you want?
#include <iostream>
template <typename Derived>
struct B
{
static int doSomething()
{
return Derived::static Member;
}
};
struct D : B<D>
{
static const int staticMember = 42;
};
int main()
{
std::cout << D::doSomething( ) << std::endl;
}
--
Ian Collins.
Hello Ian,
I have to think about and try out the
reachability of staticMember by B.
The static member in D seems not to be
enforced by struct B. But this is no
severe problem.
Somehow strange that the base class is
dependent from the derived one.
Hubert Fritz
Ian Collins wrote:
I'm still not 100% clear what you are trying to achieve, but would
something like this do what you want?
#include <iostream>
template <typename Derived>
struct B
{
static int doSomething()
{
return Derived::static Member;
}
};
struct D : B<D>
{
static const int staticMember = 42;
};
int main()
{
std::cout << D::doSomething( ) << std::endl;
}
Hubert Fritz wrote:
Please try not to top post.
>
Ian Collins wrote:
>>I'm still not 100% clear what you are trying to achieve, but would something like this do what you want?
#include <iostream>
template <typename Derived> struct B { static int doSomething() { return Derived::static Member; } };
struct D : B<D> { static const int staticMember = 42; };
int main() { std::cout << D::doSomething( ) << std::endl; }
Hello Ian,
I have to think about and try out the
reachability of staticMember by B.
Either make it public, or
class D : public B<D>
{
friend class B<D>;
static const int staticMember = 42;
};
The static member in D seems not to be
enforced by struct B. But this is no
severe problem.
It is if anything tries to call doSomething().
Somehow strange that the base class is
dependent from the derived one.
Welcome to the world of C++!
--
Ian Collins.
On Jan 28, 12:35 pm, Hubert Fritz <hubertfr...@ar cor.dewrote:
Hello,
I fear I want to have something which is not possible in C++.
How is it possible to define a base class so that the derived
class is forced to contain a static member variable, which can
be used by static member functions of the base class?
The answer is simple: have the base class declare the static data
member itself - thereby ensuring that the static data member exists
and is accessible from the derived class.
After all, what difference does it make whether the base class or its
derived class is the one to declare the static data member? The
overhead in either case is exactly the same.
Greg This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Tee |
last post by:
Hi,
I have a base usercontrol with a method (blank method, no code), I have
another few usercontrols that will inherit this base usercontrol, but I want
to force all the usercontrol that inheriting this base usercontrol to
override the method with its own code. How do I do it?
I have tried to make the base usercontrol an abstract class (mustinherits +
mustoverrides), this works, but all the usercontrols that inherit it will
not able...
|
by: Ernst Murnleitner |
last post by:
Hello Readers,
Is there a way that only one class can construct a class A and its inherited
classes A2, A3 etc.?
I want to construct a class A (and the inherited classes A2, A3 etc.) from a
(factory) class Fa.
I wanted to make that only F can call
new A
|
by: Bryan Parkoff |
last post by:
I find an interesting issue that one base class has only one copy for
each derived class. It looks like that one base class will be copied into
three base classes while derived class from base class is executed. It
means that three derived classes are pointed to a separated copy of base
class. I do not allow second and third copy of base class to be created,
but it must remain only first copy of base class.
It allows three derived...
|
by: Brad Navarro |
last post by:
OK, I may be asking for the impossible, but I'll give it a shot: For a case
like this:
using System.Data.SqlClient;
abstract class Base
{
private SqlConnection DBConnect;
Base(string SqlConnectStr)
{
DBConnect = new SqlConnection(SqlConnectStr);
|
by: Bonj |
last post by:
i'm trying to create a class that will contain all the features of
instantiating a window and showing it. I've got the SetWindowLong /
GetWindowLong pair to succesfully store the 'this' pointer in the hWnd's
GWL_USERDATA, but the pointer that it's referring to always seems to point
to the base class and never the derived class. I don't know what I'm doing
wrong, this has been driving me mad for hours. Consequently, the static
WndProc...
| |
by: Tomás |
last post by:
Given the following:
static_cast<T>( expr )
This will evaluate to an l-value if T is a reference type -- otherwise it
will evaluate to an r-value.
The same goes for reinterpret_cast.
I've been trying to write an "implicit_cast", but I don't think it's
|
by: Dave Booker |
last post by:
It looks like the language is trying to prevent me from doing this sort of
thing. Nevertheless, the following compiles, and I'd like to know why it
doesn't work the way it should:
public class ComponentA
{
static string s_name = "I am the root class.";
public string Name { get {return s_name;} }
}
|
by: Mirko Puhic |
last post by:
Is there a way to properly do this?
struct Derived;
struct Base{
Derived der;
};
|
by: Naomi |
last post by:
I need to make software engineering decision to do with using a
derived data type in a container class. So for example, if I have an
Edge class, and I want to make a Edge object which contains two
NumberVertices instead of Vertices, I can either just use a non-
templated Edge class which stores pointers to my NumberVertices and
leave it up to the programmer to cast back again to NumberVertex (this
type of casting only works with...
|
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: 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: 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: 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: 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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |