473,466 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Base class returning children's data

Hello,
I'm trying to to this: all classes of a hierarchy have a property,
which is a constant object shared between each class. I'd like to have
one function for getting that object, but each class should initialize
that object once.

For example, given the property of type Property, we have that:

class Object {
public:
const Property getP() const { return _p; }
private:
static Property _p;
}, o1, o2;

Property Object::_p = Property("Object's Prop");

class Widget : public Object {
// We don't want to redeclare all here
};
Property Widget::_p = Property("Widget's Prop");

In this way, we use the code of getP which returns always a given
static variable, but the "pointed variable" changes when accessing to
a certain class:

Object *o = new Object, *w = new Widget;
o->getP() // Object's prop
w->getP() // Wdiget's prop

The reason why I'm trying to do this is that the getP member function
may change it's way to produce the result, and it must be consistent
between all derived objects, even if it changes. So I'm trying to
reduce code localizing it in just one place.
For example, assume that we have multiple properties and that getP
returns a composition of them:
class Object {
...
float getP() const;
static int p1;
static float p2;
}

one version of getP may be:
float Object::getP() const { return p1 + p2; }

but later, it may change in:
float Object::getP() const { return p1 + 100 * p2; }

and clients shouldn't never notice this change, they just have to
handle class' properties, and never touch this function.

Actually, I'm thinking about static and virtuals, but I can't find a
proper way to do this. Maybe with templates it's also feasible. Also,
please address me to any pattern which may suit this case, I'm sorry
but I don't know many patterns.

Thanks :)
Jul 4 '08 #1
3 1295
On Jul 4, 11:31*pm, "Alf P. Steinbach" <al...@start.nowrote:
In

* * float Object::getP() const { return p1 + 100 * p2; }

should a derived class be able to override the p1 and p2?
Yes, because each property remains equal for each class.
Think about that properties as static constant, for example as would
be class "name" perperty:
Base::name == "Base"
Derived::name == "Derived"
Integer::name == "Integer"

Every class has a name, and every object would like to access to its
class name using, for example
this->myName()
which return the
static const char *name
defined for object's class.

Thanks
Jul 5 '08 #2
On Jul 5, 2:54*am, "Alf P. Steinbach" <al...@start.nowrote:
Well, then you run into problems if you allow variable data in those properties.

But putting that aside (just don't have that), it seems that what you're after
is the old concept of meta-class, like, off the cuff,
Thanks for the reply :) It gave me some ideas...

I'd prefer to avoid meta-classes, it seems a little code bloat to me
for such an easy (?) task. What about this? It seems to work, but...
Is it correct and safe?

class Base {
// This is the function I'd like to write only once
const int get() { return val() * 100 + 5; }
private:
virtual int val() {
static const int myVal = 10;
return myVal;
}
};

class Deri: public Base {
private:
virtual int val() {
static const int myVal = 20;
return myVal;
}
};
int main(int argc, char **argv) {
Base *o1 = new Base(),
*o2 = new Deri();

cout << "O1 val: " << o1->get() << endl
<< "O2 val: " << o2->get() << endl;
return EXIT_SUCCESS;
}

I mean, get() will always refer to polymorphic (overwritten virtual)
val()?
Actually I'm having some difficulties in figuring out *why* this
works :D

Thanks!
Jul 5 '08 #3
On Jul 5, 12:35*pm, "Alf P. Steinbach" <al...@start.nowrote:
* Alessandro [AkiRoss] Re:
On Jul 5, 2:54 am, "Alf P. Steinbach" <al...@start.nowrote:
Well, then you run into problems if you allow variable data in those properties.
But putting that aside (just don't have that), it seems that what you're after
is the old concept of meta-class, like, off the cuff,
Thanks for the reply :) It gave me some ideas...
I'd prefer to avoid meta-classes, it seems a little code bloat to me
for such an easy (?) task. What about this? It seems to work, but...
Is it correct and safe?

It seems to be formally correct (disclaimer: haven't compiled).

However it's just plain ordinary direct dynamic polymorphism.
Ok then, and thanks for the advices!
Also thanks for the meta-class concept, it may be useful in future :)

Bye
Jul 5 '08 #4

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

Similar topics

2
by: Krzysztof Stachlewski | last post by:
I tried to run the following piece of code: Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> o = object() >>> o.a...
4
by: james | last post by:
I have a custom UserControl, which can have many sub class levels derived from it. I want to be able to discover all the components at Load time, but the only components I can see from the base...
5
by: Chris Szabo | last post by:
Good afternoon everyone. I'm running into a problem deserializing a stream using the XmlSerializer. A stored procedure returns the following from SQL Server: <Student StudentId="1" Status="1"...
2
by: Wade | last post by:
Hi all, We have created some "Base" class pages for our WebForms and UserControls. For instance, when we create a WebForm called "WebForm1.aspx", instead of inheriting from "System.Web.UI.Page"...
10
by: Adrian | last post by:
Below is an example of the problem I am having. I don't understand how I can get the compiler to see deriv &operator=(const T &rhs). I am sure this is a common problem - any suggestions? ...
9
by: Anil Gupte | last post by:
After reading a tutorial and fiddling, I finally got this to work. I can now put two tables created with a DataTable class into a DataRelation. Phew! And it works! Dim tblSliceInfo As New...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
1
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,...
0
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...
0
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...
0
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...

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.