 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Thu Dec 07, 2006 12:09 am Post subject: suggestion: member to class instance function |
|
|
I suggest adding, to the standard library, a function template
that, given a member pointer to a data member of a class,
and an absolute pointer to the data member in an instance
of the class, returns a pointer to the class instance.
Here is a definition of this template, which I believe would
work properly on many C++ implementations:
template <typename c_t, class m_t>
inline c_t *mbrptr_to_classptr(
m_t c_t::*rel_mbrptr, m_t *abs_mbrptr)
{
return(
reinterpret_cast<c_t *>(
reinterpret_cast<char *>(abs_mbrptr) -
(reinterpret_cast<char *>(
&((static_cast<c_t *>(0))->*rel_mbrptr)) -
static_cast<char *>(0))));
}
Here is an example of use/simple test program:
#include <iostream>
struct A { int a, b; } a;
int main(void)
{
if (mbrptr_to_classptr(&A::b, &a.b) == &a)
cout << "yes" << endl;
return(0);
}
The examples that I have of code that I would
want to use this in are long and proprietary.
They involve a class with an array member,
and a hash table member. The hash table
member contains elements of the array
member. The hash table member is an
instantiation of an intrusive template that
uses array indexes instead of pointers to
create the internal container structure.
A philisophical argument for having this
utility is that the distinction between a
data member and a base class is in
many ways arbitrary. This template
is analogous to down-casting from
a base to a derived class, so there
is reason to believe it might
occasionally be useful and
appropriate.
---
[ 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.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Guest
|
Posted: Mon Dec 18, 2006 8:43 pm Post subject: Re: suggestion: member to class instance function |
|
|
wkaras (AT) yahoo (DOT) com wrote:
| Quote: | I suggest adding, to the standard library, a function template
that, given a member pointer to a data member of a class,
and an absolute pointer to the data member in an instance
of the class, returns a pointer to the class instance.
|
In
http://groups.google.ws/group/comp.lang.c++.moderated/browse_frm/thread/0eea5efe958e1930/1e1513390222dcb1?
I suggest to introduce a language feature to get the adress of the
enclosing object, using a mechanism equivalent to yours. My proposal
works different than yours, and it is limited to classes that have only
one instance within the enclosing class.
I assume it might be interesting for you.
| Quote: | A philisophical argument for having this
utility is that the distinction between a
data member and a base class is in
many ways arbitrary. This template
is analogous to down-casting from
a base to a derived class, so there
is reason to believe it might
occasionally be useful and
appropriate.
|
Absolutely.
-- Andreas H.
Btw, I tried to contribute some time before, but I'm afraid my post got
lost in moderation ??
---
[ 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.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
|
|
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
|
|