473,789 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static Members and Inheritance

Hi,

Say I have a abstract base class and I know that every derived class
will have a static data member (call it SharedInformati on)
representing information to be shared across all instances of that
derived class. However, this information will be differenet for each
derived class.

Now, if I define SharedInformati on as a static member of the base
class, then SharedInformati on cannot vary across the different derived
classes, because all derived classes will pointing to the same static
instance of SharedInformati on. However, if I don't define it in the
base class, then someone looking at the base class won't know that
every derived class must define a static data member
SharedInformati on.

How can I solve this problem? That is, have my base say, "All derived
classes will have a static data member called SharedInformati on" but
still be able to have different static instances of SharedInformati on
for each base class.

I know I may be looking at this problem in the wrong light. If so,
I'd appreciate any suggestions on how to re-conceptualize it.

thanks,
cpp
Jul 22 '05 #1
10 1891

CPP,

Right now, I think your problem statement is a bit too vague
to offer a solution. I can think of several approaches based upon
some programming patterns used in MS Windows programming:

1. Use a class factory to create your objects, the factory effectively
provides
the "anchor" point to connect all instances of a manufactured class

2. Provide macros which generates a member function/data definition
to insert in the header/implementation of the class, something akin to
the macros used to support serialization of objects in MSVC++/MFC.

3. Implement the class using "policy" classes, that is a templatized
multiple
inheritance approach, weave in a special class which handles the static
registration of the derived class. This will require the "deriver" of
your
class to do so in the same way, which might or might not be acceptable.

I could go on, but it might be fruitless if I don't understand the context
of the
problem.

dave

"cppaddict" <he***@hello.co m> wrote in message
news:qu******** *************** *********@4ax.c om...
Hi,

Say I have a abstract base class and I know that every derived class
will have a static data member (call it SharedInformati on)
representing information to be shared across all instances of that
derived class. However, this information will be differenet for each
derived class.

Now, if I define SharedInformati on as a static member of the base
class, then SharedInformati on cannot vary across the different derived
classes, because all derived classes will pointing to the same static
instance of SharedInformati on. However, if I don't define it in the
base class, then someone looking at the base class won't know that
every derived class must define a static data member
SharedInformati on.

How can I solve this problem? That is, have my base say, "All derived
classes will have a static data member called SharedInformati on" but
still be able to have different static instances of SharedInformati on
for each base class.

I know I may be looking at this problem in the wrong light. If so,
I'd appreciate any suggestions on how to re-conceptualize it.

thanks,
cpp

Jul 22 '05 #2
I could go on, but it might be fruitless if I don't understand the context
of the
problem.

dave


Dave,

I'll try to give you a simple example. Say my base class is called
Document. It has methods like getLineNumber(i nt), appendLine(stri ng),
etc. Perhaps I will have derived classes like Letter, ResearchPaper,
Article, etc.

Now let's say that every document must be written using a certain
Charset (a set of characters). Let's further assume that the Charset
is always the same for different instances of a specific derived class
(eg, all Letter instances use the same Charset), but that Charset is
not necessarily the same for different derived classes (eg, an Article
could use a different Charset from a Letter).

This suggests to me that:

1. Charset should be a static member of each derived class

2. The Document class should somehow state that all derived classes
must have a Charset.

Unfortunately you can't make Charset a static member of Document,
because that would force all derived classes of Document to use the
same Charset.

Does that make sense?

Thanks for any additional suggestions,
cpp
Jul 22 '05 #3

Using your method:
class VehicleClass
{

};
class CarClass : public VehicleClass
{

};

class MotorcycleClass : public VehicleClass
{
};

Let's say that the SharedInformati on for CarClass is the year in which
Henry Ford was born, let's say it's 1902.
And let's say that the SharedInformati on for MotorcycleClass is the
average length of the forks on a motorcycle, let's say it's 120. Here we go:
class VehicleClass
{
public:
unsigned long int* pSharedInformat ion;

}

class CarClass : public VehicleClass
{
private:
static unsigned long int SharedInformati on;

public:
CarClass(void)
{
pSharedInformat ion = &SharedInformat ion;
}
};

class MotorcycleClass : public VehicleClass
{
private:
static unsigned long int SharedInformati on;

public:
MotorcycleClass (void)
{
pSharedInformat ion = &SharedInformat ion;
}
};

unsigned long int CarClass::Share dInformation = 1902;

unsigned long int MotorcycleClass ::SharedInforma tion = 120;
void ServiceVehicle( VehicleClass*);

int main(void)
{
CarClass Beamer;

MotorcycleClass Harley;
ServiceVehicle( &Beamer);
ServiceVehicle( &Harley);
}
void ServiceVehicle( VehicleClass* pVehicle)
{
cout << pVehicle->*pSharedInform ation;

//Yes, I'm aware that I haven't include the
//appropriate header files!

//The above will yield 1902 for a car, and 120 for a
//motorcycle
}

That's one way of doing it. Obviously polymorphic functions with a return
type of unsigned long int would also work, they would also be easier. But
then I have my motto, easier isn't always better, take manual transmission
cars!!
Hope that helps.
-JKop
Jul 22 '05 #4

const unsigned long int* pSharedInformat ion;
const unsigned long int CarClass::Share dInformation = 1902;
const unsigned long int MotorcycleClass ::SharedInforma tion = 120;

I tend to write code for myself, not others, so I seldom use "const". If
you're really paranoid about someone changing the value of the variable
pShareInformati on in the base class, then make pSharedInformat ion private
and then stick this function into the base class:
unsigned long int GetSharedInform ation(void)
{
return *pShareInformat ion;
}
What strikes me, and probably everyone else reading this, is that you could
do away with those variables altogether and just create a polymorphic
function called GetSharedInform ation and then just define it in CarClass
and in MotorcycleClass . That's another possible method, one which *I* would
probably choose. Then again, there would be the disadvantage that you
wouldn't be able to edit the value of the variable. Maybe that's a good
thing, or then again maybe you *do* want to be able to, in which case you
can use my original example, or, alternatively, you could write another
polymorphic function called SetSharedInform ation .

The possibilities are endless-ish!
-JKop

Jul 22 '05 #5
What strikes me, and probably everyone else reading this, is that you could
do away with those variables altogether and just create a polymorphic
function called GetSharedInform ation and then just define it in CarClass
and in MotorcycleClass .


Indeed, now that you've pointed it out, it strikes ME as well. I
think that is the best solution for my problem, though your pointer
method is a good trick to know anyway.

Thanks for all the help,
cpp
Jul 22 '05 #6

"cppaddict" <he***@hello.co m> wrote in message
news:v5******** *************** *********@4ax.c om...
I could go on, but it might be fruitless if I don't understand the contextof the
problem.

dave


Dave,

I'll try to give you a simple example. Say my base class is called
Document. It has methods like getLineNumber(i nt), appendLine(stri ng),
etc. Perhaps I will have derived classes like Letter, ResearchPaper,
Article, etc.

Now let's say that every document must be written using a certain
Charset (a set of characters). Let's further assume that the Charset
is always the same for different instances of a specific derived class
(eg, all Letter instances use the same Charset), but that Charset is
not necessarily the same for different derived classes (eg, an Article
could use a different Charset from a Letter).

This suggests to me that:

1. Charset should be a static member of each derived class

2. The Document class should somehow state that all derived classes
must have a Charset.

Unfortunately you can't make Charset a static member of Document,
because that would force all derived classes of Document to use the
same Charset.

Does that make sense?

Thanks for any additional suggestions,
cpp


You are trying to capture type information (Article uses char set 1, Letter
uses char set 2) in data. A better way is to use virtual functions. For
instance

class Document
{
public:
virtual const CharSet& get_char_set() const = 0;
};

class Article : public Document
{
public:
virtual const CharSet& get_char_set() const { return article_char_se t; }
private:
static CharSet article_char_se t;
};

class Letter : public Document
{
public:
virtual const CharSet& get_char_set() const { return letter_char_set ; }
private:
static CharSet letter_char_set ;
};

If later you decide that you want Letter objects to store their own
individual char sets (say) then you can easily make letter_char_set
non-static and nothing else need change.

john
Jul 22 '05 #7
If later you decide that you want Letter objects to store their own
individual char sets (say) then you can easily make letter_char_set
non-static and nothing else need change.

john


John,

Thank you for that. That is the solution I'm going to go with.

cpp

Jul 22 '05 #8
cppaddict posted:
What strikes me, and probably everyone else reading this, is that you
could do away with those variables altogether and just create a
polymorphic function called GetSharedInform ation and then just define
it in CarClass and in MotorcycleClass .


Indeed, now that you've pointed it out, it strikes ME as well. I
think that is the best solution for my problem, though your pointer
method is a good trick to know anyway.

Thanks for all the help,
cpp


If I was ever a C++ teacher, or wrote a book teaching C++, and I got to the
point where I was teaching polymorphism, I would first just show them how to
do it "manually" using function pointers, as I did with my variable
pointers. Then I would say to them, "Guess what?! C++ has a built-in thing
that does this for you, it's called polymorphism, so now you don't have to
write a big bulge of code for EVERY class you write!". The result of this is
that the student would understand polymorphism perfectly and entirely!

When *I* first came to the chapter on polymorphism, I took one look at the
word and made sure I was sitting up straight and paying full attention to
the text, to try digest the whole thing. The name, "polymorphi sm", is
stupid, it's like something out of biology - it implies that the concept is
much more complicated and hard to comprehend than it actually is!, when in
essence it's just a nice pointer trick!

And you've got to love my polymorphism with member variables!
-JKop
Jul 22 '05 #9

"cppaddict" <he***@hello.co m> wrote in message
news:qu******** *************** *********@4ax.c om...
Hi,

Say I have a abstract base class and I know that every derived class
will have a static data member (call it SharedInformati on)
representing information to be shared across all instances of that
derived class. However, this information will be differenet for each
derived class.

Now, if I define SharedInformati on as a static member of the base
class, then SharedInformati on cannot vary across the different derived
classes, because all derived classes will pointing to the same static
instance of SharedInformati on. However, if I don't define it in the
base class, then someone looking at the base class won't know that
every derived class must define a static data member
SharedInformati on.

How can I solve this problem? That is, have my base say, "All derived
classes will have a static data member called SharedInformati on" but
still be able to have different static instances of SharedInformati on
for each base class.


I don't think you need anything static. Simply have a function that returns
the value you want. Then override this function for any subclasses that
return a different value. You're asking for a "static" solution to a
"dynamic" problem. This is what OO is for.
Jul 22 '05 #10

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

Similar topics

10
2515
by: porneL | last post by:
How do I use static functions/properties with inheritance? I've found it very problematic. class Foo { static function A() {/* ??? */::B(); } static function B() {echo 'Foo';} }; class Bar extends Foo
8
2041
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the constness of static members. Is there a way to do that? Also, is there a way for a static member function to guarantee constness of static members? TIA Regards,
7
1879
by: Sunny | last post by:
Hi all, According C# Language Specification : 10.11 Static constructors: The static constructor for a class executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain: - An instance of the class is created.
3
1754
by: Aaron Watters | last post by:
A C# question about constructors/static methods and inheritance: Please help me make my code simpler! For fun and as an exercise I wrote somewhat classical B-tree implementation in C# which I later ported to java and Python for comparison. http://bplusdotnet.sourceforge.net/ for full details and code ]
3
11832
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to add items to this list, but have the base class hold the data and expose the property. Problem is, however, that the derived class' static constructor doesn't get called when DerivedClass.MyList is accessed, so the list has no members. If
6
4411
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the same invocation from a DistantlyRelated class. What actually happened was that the compiler produced: error C2247: 'A::function' not accessible because 'CloselyRelated' uses 'private' to inherit from 'A' I'm guessing that the above compiler...
11
3846
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
8
2889
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
13
20775
by: learning | last post by:
Hi I have a static class written by other team which encapsulates a database instance. but I need to extend it to incldue other things. I know that C# static class is sealed and can;t be inherited & I don't want to copy + paste code. How can I inherit those member variables? Thanks
2
2002
by: Tim Van Wassenhove | last post by:
Hello, When i read the CLI spec, 8.10.2 Method inheritance i read the following: "A derived object type inherits all of the instance and virtual methods of its base object type. It does not inherit constructors or static methods...." In the C# spec, 17.2.1 Inheritance i read the following:
0
9666
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10200
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
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,...
0
9020
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, 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...
1
7529
isladogs
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...
0
6769
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();...
0
5418
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...
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.