473,805 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
+ 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.ReadInt Ptr(IntPtr,offs et) 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.ReadByt e and it's not working cause my object type could be
anything...
Anyone have some idea ???

Thanks

Nov 16 '05 #1
3 1202
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.Referenc eEquals( 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
1949
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 objects of my classes Person(superclass), Student(inherit Person), Teacher(inherit Person) in that array. The name will be the unique key. These classes are all working ok. I want to be able to add, remove, find etc. objects. To all of this, I...
6
1608
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. But this time > I didn't know what to search for. Now I know that I could have > searched for "initializaion", and found FAQ 10.6, but hey, I can't > read the whole FAQ through every time I have a question, can I...? Just a thought ... how about...
2
2013
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 that print out a type's name to an istream:
2
2653
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
3414
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 as ff: typedef struct { float *data ; int count ;
5
3185
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++ class which has several virtual functions and several member variables, will the default copy constructor also copy the virtual table pointer, or will it merely do a (shallow) copy of the member variables? I'm using gcc 4.1.1
3
2016
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 contains smart pointers (boost::shared_ptr or similar) to a set of these two classes and keeps them sorted wrt each other. For example if I insert ClassA, ClassA, ClassB, ClassA, ClassB in that order, I want to re-visit them in that same order...
8
1706
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 can provide is a node that is the same and will == true. So naturally this would work if my tree was not of pointers but of objects. Since it's of pointers, it compares my node criteria against every node in the tree and the memory addresses...
54
12034
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 the advantages of smart pointers endlessly (which are currently used in all our C++ software; we use the Boost smart pointers) as I'm seriously concerned that there is a shift to raw pointers. We are not developing system software but rather...
13
2894
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); for(int i = 0; i < num; i++) { int* my_int_ptr = new int;
0
10614
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10369
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10109
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3847
muto222
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.