Is there a safe and legal way to go from a pointer to a member of an object to a pointer to that same object?
What did you try that hasn't worked?
> Is there a safe and legal way to go from a pointer to a member of an object
> to a pointer to that same object?
A pointer to a member is a pointer to a member of a class, not a pointer to a member of an object. When you dereference a pointer to a member of a class, you need you need to provide an object of that class with respect to which it is derefenced.
The object comes into picture only when the pointer to member is dereferenced; the same pointer to member can be dereferenced many times, each time with a different object.
A pointer to a member is really just an offset of that member to the start of the class.
Therefore assume that I have derefenced the pointer to member PTR_MBR using an instance I of the class C and obtain M.
Now, let's assume M is used later in the code and we don't have anymore knowledge of what instance it came from, only that it is a member of the class C obtained with PTR_MBR
Then couldn't we just say that the address of I is the adress of M minus the offset of the member in the class (which is PTR_MBR):
I = M - PTR_MBR !
It actually works when you do appropriate casting, but I suspect this is not much legal.
But is there a way to do this legally and without casting?
I am sure you will ask: What would you use it for?
There is a very useful situation which is the following.
Suppose you want to write a generic list iterator and you use in the implementation of the iterator the actual address of "next" which points to the address of the "next" of the next item, )and you also keep somewhere PTR_MBR of next for that item class).
Now, you want to obtain the item address itself from the address of "next", it would be great if you just go substract the PTR_MBR from the address of next.
Of course, the "normal" alternative is to have "next" point directly to the item (and then get the next "next" using the PTR_MBR) but that makes it less versatile because you might want to keep the pointer to the list as a simple isolated "next" without the rest of the item, something you could do with the previous approach but not with the "normal" alternative.
Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"
Here is the sample output that I ...
Hello All ...
Iam Getting An Error With try to excecute the stored procedure .
I have Have Sql database , the stored procedure like so :
USE [MPRS] GO /****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE ...
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...