473,473 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Pointers, Classes, Objects ...

How can I get an object address as IntPtr ?
Somehow even in unsafe it's not working with objetcs...

I'am trying to compare two objects that are not the same but could be from
some point.

Example :

Let say we have two classes one derived from another

public class A
{
protected h=0;

public virtual func()
{
h=0;
}
}

public class B:A
{
public override func()
{
h=1;
}
}
I created two instance of these classes obj1 and obj2,

obj1=Activator.CreateInstance(t1); //Type t1; class A
obj2=Activator.CreateInstance(t2); //Type t2; class B

After invoking we can see in the debugger somthing like this...

obj1.A.h=0
obj2.B.A.h=1;

I dont whant to compare fieald by GetFieald() and comparing them one by one,
My idea was to try the Marshal.ReadIntPtr(IntPtr,offset) so I can read obj2
after some offset, So I needed my variable Address. (somehow even in unsafe
we cant do object *p=obj1; on objetcs...)

I tried Marshel.ReadByte and it's not working cause my object type could be
anything...
Anyone have some idea ???

Thanks

Nov 16 '05 #1
3 1188
I'am trying to compare two objects that are not the same but could be from
some point.
You can see if two variables refer to the same object with

if ( (object)obj1 == (object)obj2 )

or

if ( Object.ReferenceEquals( obj1, obj2 ) )

No need to involve pointers.

obj1=Activator.CreateInstance(t1); //Type t1; class A
obj2=Activator.CreateInstance(t2); //Type t2; class B


If you allocate two objects they will always be in different locations
in memory (with the exception of interned strings).

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Simple comparison like this won't work couse they can be similar only after
some offsetting... if ...

obj1.A.h=0
obj2.B.A.h=1;

I will need to bring them to ...

A.h=0 //obj1
A.h=1 //obj2

So ... still I will need the object pointer.

Nov 16 '05 #3

Why don't you just write a comparison method that compares the fields,
what's wrong with that? You could possibly implement IComparable for
this.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #4

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

Similar topics

3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
6
by: David Fisher | last post by:
In a recent message, "Re: Two questions about...something", Fred H wrote: >> This is covered in the FAQ. Please read FAQ before posting. >> > I have read the FAQ, and I search it quite often....
2
by: Thomas Matthews | last post by:
Hi, I would like to create a table (or vector) of pointers to templated functions. 1. How do I declare a typedef of a pointer to a templated function? For example, I have some functions...
2
by: mosfets | last post by:
Hi, I'm having a little trouble figuring out the difference in terms of memory allocation between: class person_info; class A { private:
27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
5
by: Martijn van Buul | last post by:
Hi. I'm having a peculiar problem at work. I've been googling for it, but haven't found an authorative answer. In a nutshell (Long story follows), what I'd like to know is: If I have a C++...
3
by: Tim H | last post by:
I'm wondering if there is a way to do this properly. I feel like I am missing something. I have two distinct classes. They don't have much at all in common. I want a vector-like container that...
8
by: Travis | last post by:
I have a tree of pointers and I need the ability to search the tree for a specific node. The problem is I can't provide that pointer for the node I'm looking for as criteria. The only critieria I...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
13
by: smp | last post by:
Does anyone know why making a vector of pointers is so much less efficient than a vector of objects? For a simple example: int num = 20; vector<int*v_int_ptr; v_int_ptr.reserve(num); ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.