C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

static_instance

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
dagecko
Guest





PostPosted: Wed Feb 08, 2006 8:10 pm    Post subject: static_instance Reply with quote



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).

(PS. I also posted it on comp.std.c++)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
benben
Guest





PostPosted: Thu Feb 09, 2006 11:06 am    Post subject: Re: static_instance Reply with quote



Quote:
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.

I don't really get what you mean but it seems you really need two
classes instead of one. So:

class A
{
private: int a;
public: void funcA(); // can't deal with B::b
};

class B
{
private: int b;
public: void funcB(); // can't deal with A::a
};

class C: public X, private A, private B
{
public:
using void A::funcA();
using void B::funcB();
};

"[S]tatic data member inside the member functions..." does not really
make sense to me. Perhaps you meant a "static variable inside a member
function" or "static member variable inside the class".

Quote:
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).

What's the problem?

Quote:

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).

A number of things:

1. You don't really need polymorphism to solve your problem it seems.
2. You probably have a design problem because you can't prohibit your
user from using a public member function under some circumstances
dictated by the state of the object.

Quote:

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).

I am confused.

Quote:
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

First, c1::funcA does qualifies nothing, neither do c1::funcA::a,
c2::funcB nor c2::funcB::a.

If the above really mean the static variable inside C::funcA then how is
C::a used? If it is not used why did you put it there in the first place?

Quote:

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:

Ok. A number of points here:

1. If you need to lower memory consumption, then consider using a number
of classes utilizing different allocation strategies and let the user
choose which one to use. Or, if the class really encapsulates nothing at
all maybe it really should be a class; a number of functions would do.

2. C++ is a language that allows you to encapsulate concepts. But
encapsulation is NOT, i repeat NOT, a security measure. If you do feel
like securing a piece of data you should go to your OS for hosting and
encryption, or other means. Do not use the language to provide security
because it will not work.

Quote:

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).

Again, I am not sure what you are talking about but I am sure whatever
you are talking about you are heading the wrong way. Because...well,
because of what i just have said above.

Quote:

(PS. I also posted it on comp.std.c++)

Don't do that!

Quote:

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Ben

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Calvin
Guest





PostPosted: Thu Feb 09, 2006 11:06 am    Post subject: Re: static_instance Reply with quote



Dear dagecko,

class C: public X
{
public:
virtual void func() = 0;
};

class CA: public C
{
int a;

void funcA() {...}
public:
virtuall void func() { funcA(); }
};

class CB: public C
{
int b;

void funcB() {...}
public:
virtual void func() { funcB(); }
};

C* ca = new CA;
C* cb = new CB;

ca->func(); // call funcA()
cb->func(); // call funcB()


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Kurt Stege
Guest





PostPosted: Thu Feb 09, 2006 3:06 pm    Post subject: Re: static_instance Reply with quote

dagecko wrote:

Quote:
I know what I propose is somewhat different from what C++ classes
are actually, but I guess that is not a contradiction.

I will show you a way how to solve your problem using the
standard C++ we all have...

Quote:
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).

Yes, that comes from the static behaviour of C++. When
you create the instance of C, the memory is allocated
(from the stack, heap, ...). At this time the size of
the class has to be known.

You would like to make this size dependent on the later
usage. To decrease the memory usage you will need
some kind of dynamic memory allocation later on, when
calling funcA() or funcB().

Quote:
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).

That is good to hear. To reduce your memory problem, I
will later on replace the integers with pointers.
The pointers keep small, even when you replace the
integers with "yourOwnReallyHugeClass".

Quote:
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.

The compiler would have at least two possibilities to
implement this new feature.

First, the compiler could reserve the memory required for
a and b already when creating the object of class C. It
would just delay the call of the constructor for a and b
until the first time funcA() or funcB() is called.

This strategy does not reduce memory usage but only reduces
CPU time, when funcA() is never called. In opposite, it
increases memory usage, for the program has to store anywhere,
if a or b already have been constructed.

Second solution, this is what you had in mind when suggesting
"static_instance", I will demonstrate with following code:

class C: public X // might or not inherit from another class
{
int *a; // TODO: initialize in the constructor to 0.
int *b; // TODO: call delete in the destructor.

int get_instance_of_a() { if (!a) a = new int(); return a; }
int get_instance_of_b() { if (!b) b = new int(); return b; }

public:

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

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

I guess, this does exactly what you had in mind.
When you need this pattern, you can implement it
quickly without any compiler support.

I suppose, this strategy is not important enough
to propose an extension to C++ that supports
"static_instance".

Quote:
This proposal is almost made for low memory consumption and
for security.

Security? As in restricted access rights as when using "private"?

What happens, when there is another function

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

Gets this function a new version of a, or shares it the
same a as used in funcA()?

When introducing "static_instance", I would suggest to
declare that variables at the same position as all other
members of the class are declared as well, namely in the
class declaration, and thus re-using the established
access protections provided by "private" or "public".



So, when you need your idiom, you can implement it with
only few effort. Alas, I have the feeling that in most
cases there should be other solutions for the real problem
you want to solve that are more elegant...

HTH,
Kurt.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
dagecko
Guest





PostPosted: Thu Feb 09, 2006 3:06 pm    Post subject: Re: static_instance Reply with quote

This is not what I meant. I don't want to hide funcA and funcB
inside another member function. Also, sometimes, polymorphism
isn't accurate for some motivations. And I told that in some
ways, users might need to call both funcA and funcB, in that
case, your way isn't a solution but with using multiple
inheritence. I have nothing against multiple inheritence, but
that might not help under some circumstances, ie when the
programmer uses a library he didn't wrote, so he can't change it
as he wishes (and specially he might not be able to inherit
from some library classes, mainly if instances are created
regarding a config file or if the library wasn't wrote for allowing
multiple inheritence to be done easily, ie if base classes are not
made virtual).

Well I see I can't tell any good example, and I know the examples
I made could be made differently without the necessity of a new
keywoard. That mainly depends on how the code is created, and how
the code could be created: Sometimes, design does not allow some
things to be done as easily as in a simple short and not in
real situation example.

But I have another motivation that I didn't want to show now
because it might be subject to several controversies and also
because I didn't want to share it now since it doesn't tend
to a 'perfection'.

I called it DisparatContainer, its objectives are to set and
get values in a container regarding their types. I'd have several
uses for such a concept, but I actually can't do it as I wish
because of its current inconveniencies.

So here it is:

class DisparatContainer
{
public:

template <class T>
T* const
Do (T *t = 0)
{
static T *_t = 0;

if (t)
_t = t;

return _t;
}
};

The main interrest of such a class is to allow users to get values
regarding the type they specify. But as this, all instances of this
class will own the same values regarding the types:

int i = 1;
float j = 1.2;

DisparatContainer a, b;

cout << *a.Do (&i) << endl; // returns 1
cout << *a.Do (&j) << endl; // returns 1.2
cout << *a.Do<int>() << endl; // returns 1
cout << *a.Do<float>() << endl; // returns 1.2
cout << a.Do<long>() << endl; // returns null pointer

// static_instance would help having several disparat
// containers owning the same things !!

cout << *b.Do<float>() << endl; // also returns 1.2 !!

Here, a and b hold exactly the same things. This is not a good
thing because a program might need different instances of this
type. The only solution to this is to create as much class of
this kind as needed. But the fallback is that they will be
different types, so requesting other parts of the program that
will need that to be templated.

So here something like that would have help more:

class DisparatContainer
{
public:

template <class T>
T* const
Do (T *t = 0)
{
static_instance T *_t = 0; // note static_instance here

if (t)
_t = t;

return _t;
}
};

Here instances a and b will hold different values. And as far
as I know, there aren't any other way to do that.
I also know that new keywoards are the last thing to add to C++
(as told by B. Stroustrup), but in some ways I need such a
kind of possibility.

Finally, I don't know how far this is interresting, neither how
far it is doable. But it would at least be nice if we could
discuss about it.

Hope that's understandable now.

regards.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
dagecko
Guest





PostPosted: Fri Feb 10, 2006 3:06 am    Post subject: Re: static_instance Reply with quote

Le Thu, 09 Feb 2006 09:42:38 -0500, Kurt Stege a écrit :

Quote:

The compiler would have at least two possibilities to
implement this new feature.

Thanks to see the potential goods.

Quote:

First, the compiler could reserve the memory required for
a and b already when creating the object of class C. It
would just delay the call of the constructor for a and b
until the first time funcA() or funcB() is called.

That seems good, like static variables work.

Quote:

This strategy does not reduce memory usage but only reduces
CPU time, when funcA() is never called. In opposite, it
increases memory usage, for the program has to store anywhere,
if a or b already have been constructed.

Yes but that would not be a such big increase.

Quote:

Second solution, this is what you had in mind when suggesting
"static_instance", I will demonstrate with following code:

class C: public X // might or not inherit from another class
{
int *a; // TODO: initialize in the constructor to 0.
int *b; // TODO: call delete in the destructor.

int get_instance_of_a() { if (!a) a = new int(); return a; }
int get_instance_of_b() { if (!b) b = new int(); return b; }

public:

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

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

I guess, this does exactly what you had in mind.
When you need this pattern, you can implement it
quickly without any compiler support.

That makes sense. But I think this won't help in all the
situations. I also think I found a good example to show
why:

class A
{
int a,b,c,d,e; // or maybe more (and surely not int)

public:

void funcA(); // works with a only
void funcB(); // works with b only
void funcC(); // works with c only
void funcD(); // works with d only
void funcE(); // works with e only
};

Under such a situation, the programmer will have to implement
all the allocations for pointers to a,b,c,d,e... So it would
be a really hard stuff.

Also using pointers can provoke much cpu and memory consumption
because the solution you proposed needs dynamic allocations
(this turns out) which is slowler than static allocation, and
also copy by value which might not be good under all the
situations, almost when using polymorphism. After all, int a
could be APolymorphicClass *p.
Also, when using templates things might be more difficult
(cf the post I made to answer Calvin).

Quote:

I suppose, this strategy is not important enough
to propose an extension to C++ that supports
"static_instance".

Not this one, and maybe neither not the other one in the
other post.

Quote:
Security? As in restricted access rights as when using "private"?

What happens, when there is another function

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

Gets this function a new version of a, or shares it the
same a as used in funcA()?

I suppose funcC belongs to the same class than the other func.
Well, there is a new version of a.

static_instance (or maybe another keyword but this is not
the problem) would tell that the variable belongs to the
member function from the matching instance that called it.

If 2 member functions need to share the same variable, I
actually don't see why that variable would not be declared
directly in the class, this turns out for me.

Here is a little example on how the things would work:

class A
{
public:

int func()
{
static_instance int i = 0;

return i++;
}
};

A a, b;

cout << a.func() << endl; // returns 0
cout << a.func() << endl; // returns 1
cout << b.func() << endl; // returns 0

Quote:

When introducing "static_instance", I would suggest to
declare that variables at the same position as all other
members of the class are declared as well, namely in the
class declaration, and thus re-using the established
access protections provided by "private" or "public".

No, this is not what I was thinking of. static_instance
means that a variables belongs to a unic member function
and per instance. So it simply can't be declared in the
class as other members.
Accessibility has nothing to do with 'static_instance'
just because it belongs to a member function not a class,
which can't be inherited neither accessed outside the
member function.

Quote:

So, when you need your idiom, you can implement it with
only few effort. Alas, I have the feeling that in most
cases there should be other solutions for the real problem
you want to solve that are more elegant...

Yes I know that. This is why I don't do such a thing often,
just my first time. And this is also why we discuss of it
here. Maybe someone will show me something that will be good
for all the situations.

Finally I'd like to say that the only things that would
really hurt for that 'new keywoard', is how it will be
realized, how it interracts with the already existing
keywords. This is just my point of view, of course.

PS: try to read my other post, answering to Calvin, this is
one of the main reasons of this topic even if I'm also
persuaded that would help in other situations (those I
figured out on the first post and this one).


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Kurt Stege
Guest





PostPosted: Mon Feb 13, 2006 5:06 pm    Post subject: Re: static_instance Reply with quote

dagecko wrote:
Quote:
Le Thu, 09 Feb 2006 09:42:38 -0500, Kurt Stege a écrit :

First, the compiler could reserve the memory required for
a and b already when creating the object of class C. It
would just delay the call of the constructor for a and b
until the first time funcA() or funcB() is called.


That seems good, like static variables work.

No, not like static variables, but like ordinary member
variable work.

Quote:
Second solution, this is what you had in mind when suggesting
"static_instance", ...

<reply snipped>

OK, that was not what you had in mind. Especially when
I see your usage of static_instance within template
functions.


Quote:
class A
{
int a,b,c,d,e; // or maybe more (and surely not int)

public:

void funcA(); // works with a only
void funcB(); // works with b only
void funcC(); // works with c only
void funcD(); // works with d only
void funcE(); // works with e only
};

Under such a situation, the programmer will have to implement
all the allocations for pointers to a,b,c,d,e...

Yes.

Quote:
So it would be a really hard stuff.

No. It can be done using template classes.
You could write

class A
{
static_instance<int> a, b, c, d, e;
...
};

and provide a template class static_instance<typename T>
that has the pointer as (private) member variable
and implements the getter that is called within the
clients code of funcA().


Quote:
Also using pointers can provoke much cpu and memory consumption
because the solution you proposed needs dynamic allocations
(this turns out) which is slowler than static allocation, and
also copy by value which might not be good under all the
situations, almost when using polymorphism. After all, int a
could be APolymorphicClass *p.

After all, you have to be clear about what you really
like to have, and than you have to pay the price for it.

Quote:
Also, when using templates things might be more difficult
(cf the post I made to answer Calvin).

OK, I copy the relevant part of that posting later on, and
handle a few snippets of this posting just now.

Quote:
What happens, when there is another function

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

Gets this function a new version of a, or shares it the
same a as used in funcA()?


I suppose funcC belongs to the same class than the other func.

Yes.

Quote:
Well, there is a new version of a.

OK. My suggested template static_instance<> would define
the variables as ordinary member functions within the class
definition. In that case there can only be one "a", and
several functions could access the same variable.


Quote:
static_instance (or maybe another keyword but this is not
the problem) would tell that the variable belongs to the
member function from the matching instance that called it.

No! Exactly that is the problem, for what your interpretation
of static_instance is difficult (and only with a high price)
to implement. It does not belong to the function, but to the
object of the class (like a member variable). There is only
one function named funcA(), which is the same for all objects.

I will go into more details later down within your Container
example.


Quote:
Here is a little example on how the things would work:

Example snipped and replaced by ...

Quote:
PS: try to read my other post, answering to Calvin, this is
one of the main reasons of this topic even if I'm also
persuaded that would help in other situations (those I
figured out on the first post and this one).

.... the Example for Calvin.

:class DisparatContainer
:{
: public:
:
: template <class T>
: T* const
: Do (T *t = 0)
: {
: static_instance T *_t = 0; // note static_instance here
:
: if (t)
: _t = t;
:
: return _t;
: }
:};

When using your static_instance within a template, you
are introducing hundreds of millions (potential) member
variables of class DisparatContainer. They obviously
cannot be allocated all, when an instance of type
DisparatContainer is created. And when the class is
compiled, now one knows, which types actually might
be used.

Therefore, you (or your compiler implementing this
feature "static_instance") simply has to implement
some dynamic behaviour. Either the class gets a
member variable that holds a list of all actually
accessed "static_instance"-variables, or (easier
to implement and nearer to your vision), the
member function gets a static variable that holds
a list of all instances of the one static_instance-variable
used in that function. You need for each object of
DisparatContainer an own instance of T, and thus
need to create and hold several of these instances,
and you need to keep track which instance of T
belongs to which instance of DisparatContainer.

And somewhere in this thread you mentioned one
further problem: You have to be clear about what
happens, when you try to copy a DisparatContainer.
Do all the existing static_instance variables
gets copied as well? Using their copy constructor?

In that case, the copy constructor of
DesperateContainer (pun intended) has to have the
knowledge that there are some member functions,
that use "static_instance" somewhere in their
implementation.


So, just put somewhere a map that holds all
static_instance instances together with the
DisparatContainer they belong to, and implement
the copy constructor and the member functions
of DisparatContainer to access this map and
implement them in a way that they behave just
like you like.

I guess, your context and your vision of
static_instance (moving member variables
from the class into the functions) is far
too complex and far too specialized to justify
any special support from the language.

Far more, I would like to get a more general
support that allows me to use the pimpl-idiom
without any overhead of typing! For example:

// myClass.h
class myClass
{
public:
// ...
private:
auto pimpl!
};

// myClassImpl.h
#include "myClass.h"
class myClass auto pimpl private declarations
{
... // implementation stuff of myClass
}

// myClass.cpp
#include "myClassImpl.h" // could be inlined instead of an own file
.... // implementation of all functions declared
.... // in myClass.h and myClassImpl.h


Just a wild idea without any thinking behind it...

Best regards,
Kurt.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
nobody@ebi.ee
Guest





PostPosted: Thu Feb 16, 2006 3:06 am    Post subject: Re: static_instance Reply with quote

dagecko <dagecko (AT) free (DOT) fr> wrote in
news:pan.2006.02.08.17.45.04.805445 (AT) free (DOT) fr:

[...]
Quote:
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.

It appears you have two concerns: accessibility and memory usage. For
accessibility you may split up your class into smaller pieces and use
derivation/composition to build up the main class, as suggested by one
poster. For reducing memory footprint you can use the pointers and
dynamic creation of data members on demand, as suggested by another
poster.

However, there is also another approach you might consider. You
essentially want to have data visible in only one member function, but
to have separate data for each object. The most natural way to achieve
this (in the current language) is like follows:

void C::funcA() {
static std::map<C*, int> instance_static_map;
int& a = instance_static_map[this];

// do things with a
}

This avoids messing up with the C class and inserting irrelevant data
objects or pointers there. This might even be the most efficient
solution if the data objects are large (instead of the plain int here)
and funcA() is called only for a small portion of all of the C objects.
But anyway only the profiler can tell you where the bottlenecks are in
your app.

regards
Paavo

PS. Removing the entry from the map upon C object deletion is left as
an exercise to the reader;-)


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Guest






PostPosted: Sat Feb 18, 2006 1:06 am    Post subject: Re: static_instance Reply with quote

dagecko wrote:
Quote:

There's a class which needs to do several things (threw different
functions).
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).

Seems like something that's solved by members of type
boost::optional<T>.

HTH,
Michiel Salters.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.