473,385 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

pointer to members arithmetic

Hi to all,

to make it short: I'm trying to build a small class that I conditionally
include in some other class just for debugging purposes.
This class should register itself in a container on construction and
unregister on destruction. The point is that I would like to query
the **enclosing class** (just during debugging...) through the
container. (check the code, hope it's clearer than the explanation..)
:-)
I would like to know if the pointer arithmetics and castings I've used
may bring to "undefined behavior" according to the standard.
I tried to understand it myself but I guess my English is not good
enough... ;-)
Just for the record: I'm trying to follow this path because I do not
want to litter the constructors of the enclosing classes...
Deep regards,
Francesco

P.S.
First posting with Thunderbird... hope it works....

#include <iostream>
#include <set>
#include <algorithm>
#include <functional>

class CDebugBase
{
public:
static void DumpStatic()
{ std::for_each( sSet.begin(), sSet.end(),
std::mem_fun( &CDebugBase::Dump ) ); }

protected:

virtual ~CDebugBase() {}

virtual void Dump() const = 0;

static std::set< CDebugBase * sSet;
};

std::set< CDebugBase * CDebugBase::sSet;

//

template< typename T >
class CDebug : private CDebugBase
{
public:

CDebug() { sSet.insert( this ); }
~CDebug() { sSet.erase( this ); }

T const * GetEnclPtr( void ) const
{
CDebug< T T::* membPtr( &T::mDebugObject );
// IS IT OK?
return reinterpret_cast< T const * >
( this - &( static_cast< T const * >( 0 )->*membPtr ) );
}

void Dump() const
{ std::cout << GetEnclPtr() << " - "
<< typeid( T ).name() << '\n'; }

};

//

struct A
{
CDebug< A > mDebugObject;
};

struct B
{
CDebug< B mDebugObject;
};

struct Trigger
{
~Trigger( void )
{ std::cout << "-----\n";
CDebugBase::DumpStatic(); }
} trigger;

//

int main()
{
A obj1, obj2, obj3;
B obj4, obj5, obj6;
A * ptr1 = new A[ 5 ];
B * ptr2 = new B[ 5 ];
CDebugBase::DumpStatic();
std::cout << "--------\n";
delete[] ptr1;
delete[] ptr2;
CDebugBase::DumpStatic();
std::cout << "--------\n";

}

Jun 27 '08 #1
1 1310
I realized that my problem can be formulated in a much more concise way:
I know the address of a member object and I would like to know the
address of the enclosing object.
The following seems to work but is it valid, no lurking "undef behav",
etc...?
Thanks
F

#include <iostream>

struct CInner {};
struct COuter
{
char mPad[ 10 ];
CInner mInner;
};

int main()
{
COuter outer;
CInner COuter::* membPtr( &COuter::mInner );
std::cout << &outer << std::endl;
std::cout << &( outer.*membPtr) << std::endl;
CInner * innerPtr( &( outer.*membPtr ) );
// IS THIS OK?
COuter * outerPtr = reinterpret_cast< COuter * >
( innerPtr - &( static_cast< COuter * >( 0 )->*membPtr ) );
std::cout << outerPtr << std::endl;
}
Jun 27 '08 #2

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

Similar topics

4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
1
by: andy_dufresne | last post by:
hi, i'd a question abt taking the difference between two pointers. for eg: if you have 2 char pointers pointing to members of an array, you advance one till you encounter a space then take the...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
7
by: barikat | last post by:
int a; int *Ptr1, *Ptr2; Ptr1 = a; Ptr1++; Ptr2 = a; printf("Ptr1 : %p\n", Ptr1); printf("Ptr2 : %p\n\n", Ptr2);
14
by: Vols | last post by:
If the people ask what is the different between pointer and reference, what is the brief and good answer? I say " pointer could point to NULL, but there is no null reference", What is your...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
19
by: =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= | last post by:
Coming originally from C++, I used to do the likes of the following, using a pointer in a conditional: void Func(int *p) { if (p) { *p++ = 7; *p++ = 8;
25
by: Ioannis Vranos | last post by:
Are the following codes guaranteed to work always? 1. #include <iostream> inline void some_func(int *p, const std::size_t SIZE) {
14
by: Szabolcs Borsanyi | last post by:
Deal all, The type typedef double ***tmp_tensor3; is meant to represent a three-dimensional array. For some reasons the standard array-of-array-of-array will not work in my case. Can I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.