Topic: static_instance


Author: dagecko@free.fr (dagecko)
Date: Thu, 9 Feb 2006 04:35:43 GMT
Raw View
Hi,

First of all, sorry for my bad english, I hope you'll understand
what I'd like to mean.

I don't know if it's an interresting proposal but in some ways
I needed it. I actually simply avoid that 'problem' by simply
using data members.
I know what I propose is somewhat different from what C++ classes
are actually, but I guess that is not a contradiction.

Here is the stuff:

Imagine that scenario:

There's a class which needs to do several things (threw different
functions). Each function uses data members of the class so that
each instance has its own values (this turns out, that's the
way C++ does the things).
Now some functions can use some members but not others and that
could be the case for almost all the member functions of the
class.

Here we can represent that class:

class C: public X // might or not inherit from another class
{
   int a;
   int b;

   // might have other data members

   public:

   void funcA()
   {
      // do things with a ONLY
   }

   void funcB()
   {
      // do things with b only
   }
};

Depending on the needs, C instances might only call to funcA
or funcB but rarely both (and maybe never). Also, using static
data member inside the member functions would not help as
a and/or b are states per instance.
So, with that class, each instance of C have the same size:
they all need to allocate memory for both a and b plus for
all the other members (and inherited members).

It's not a big problem when we use only 2 integers, but
could be much more of a problem if there are many primitive
types or big types (as big classes).

I know there's the solution to truncate that class into 2
classes and by using polymorphism we could achieve good
results. But this is not always possible, almost when the
class C already belongs to a big hierarchy and when that
concerns a single member function (or 2 like here). The
main problem is that funcA or funcB could be called only
under some specific circumstances (mainly depending on the
states of other data members).

So here is what I thought of:

class C: public X // might or not inherit from another class
{
//   int a; no need for them anymore here !
//   int b;

   public:

   void funcA()
   {
      static_instance int a;
      // do things with a ONLY
   }

   void funcB()
   {
      static_instance int b;
      // do things with b only
   }
};

Here static_instance would tell that a and b belong to the
instance and the matching member function from which it is
called. Its scope is not to a class (so not like
static does with data members), and neither from the life
of a function call (so not like auto data members of
functions).
It's created only when an instance of C uses funcA or funcB
and its life length is the instance life length.

So we could then produce code like this one:

C c1, c2;

c1.funcA(); // c1::funcA::a holds a new value: x1
c2.funcA(); // c2::funcA::a holds a new value: x2
c1.funcA(); // c1::funcA::a holds a new value: y1
c2.funcA(); // c2::funcA::a holds a new value: y2

This proposal is almost made for low memory consumption and
for security. Here there's no need to allocate memory for
a and b for all instances of C, so C is lighter. But I
acutally do not know where to allocate memory for a and/or b
when there's need for (when funcA and/or funcB is called).
Maybe another specific storage. Fortunately, this might be
known at compilation time, but this is not really sure, a
simple code like this might put the compiler under unknown
behavior:

if (some_thing)
   c1.funcA();
else
   c1.anotherFunc();

Also, I spoke about security. This might be true almost when
C belongs to a hierarchy, so that ensuring no other functions
will be able to use a and/or b. This might be interresting
for a library for example (a bit like private members).

regards.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]