Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Getting object from pointer to data member

Reply
Thread Tools

Getting object from pointer to data member

 
 
Salvo Isaja
Guest
Posts: n/a
 
      10-10-2009
Greetings,
this is my first post here, please be forgiving

I'm developing an embedded operating system in C++, and I'm writing my
own small library for intrusive data structures. I'd like to use a
hook member to store bookkeeping, inspired from Boost.Intrusive:
http://www.boost.org/doc/libs/1_35_0...ge_member_hook

When the data structure user instantiates the class template, it
specifies the node type and the address of the hook member, such as:

class MyDataStructureHook
{
private:
// pointers to other MyDataStructureHook's
};

template <typename T, MyDataStructureHook T::* H>
class MyDataStructure
{
public:
// ctors, dtors and other members
void push(T& x);
void pop();
T& top() const;
private:
MyDataStructureHook* top_;
};

struct MyNode
{
int somePayload_;
MyDataStructureHook hook_;
bool operator<(const MyNode& x);
};

MyDataStructure<MyNode, &MyNode::hook_> myDataStructure;


I know I can easily get a pointer to the hook member given a MyNode
object, for example in push(), using MyDataStructureHook* h = &(x.*H);

I wonder how I can do the opposite, for example in top(), to return a
T& from MyDataStructureHook* top_. I see the Boost.Intrusive
implementation is compiler-dependent, and it uses casts to char (boost/
intrusive/detail/parent_from_member.hpp), and I know I could get away
putting a T& owner_ in MyDataStructureHook. I wonder if there is a
reliable, simple and portable way to do that without adding further
bookkeeping.

For the record, I'm using GCC 4.3 on Debian Testing on x86, and as I
first try I was tempted to mess with addresses (sizeof(MyNode::*) == 4
and &MyNode.hook_ == 4 here).

Thank you,
Salvo
--
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer-to-member data and pointer-to-member functions and access specifiers Stephen Howe C++ 2 11-06-2012 12:32 PM
What is the correct grammar to make a function call by using static member data which is a pointer to a ordinary class member function? zaeminkr@gmail.com C++ 3 07-06-2007 12:50 PM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments