Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When can I compare pointers to the same object in c++?

Tags:

c++

For example, I have some class hierarchy (possibly, with all kinds of inheritance - public, private, public virtual, multi-inheritance, etc.):

class A {
  int a;
public:
  virtual ~A() {}
};

class B: public A { int b; };
class C: public virtual B { int c; };
class E: public virtual B { int e; };
class F: public C, public E { int f; };

Using casts I get pointers to every sub-object of the main "big" object:

F * f = new F;
E * e = f;
C * c = f;
B * b = f;
A * a = f;

What pairs of these pointers may I compare for equality (operator==) and why? Will the comparison use delta-logic or some other technique?

What are the possible situations, when I can't compare pointers to the same complex object? What kind of object it can be?

I expect, that all of the pointers to the same object are always equal.

like image 518
abyss.7 Avatar asked Mar 30 '12 12:03

abyss.7


People also ask

How do you compare pointers in C?

Pointers to objects or functions of the same type (after pointer conversions) can be compared, with a result defined as follows: If two pointers p and q of the same type point to the same object or function, or both point one past the end of the same array, or are both null, then p<=q and p>=q both yield true and p<q and p>q both yield false.

Can two pointers of the same type be compared?

Pointers of the same type (after pointer conversions) can be compared for equality. Two pointers of the same type compare equal if and only if they are both null, both point to the same function, or both represent the same address (3.9.2).

Is it safe to compare pointers?

In general, you can only safely compare pointers if they're both pointing to parts of the same memory object (or one position past the end of the object). When p1, p1 + len, and p2 all conform to this rule, both of your if -tests are equivalent, so you needn't worry.

How to check if two pointers point to the same memory location?

The == operator on pointers will compare their numeric address and hence determine if they point to the same object. Show activity on this post. To sum up. If we want to see if two pointers point to the same memory location we can do that.


1 Answers

You can compare two pointers if one pointer type is implicitly convertible to the other; that is, if they both point to the same type, or one points to a base class of the other's. The conversion will make the necessary adjustment to the address so that, if both pointers point to the same object, they will compare equal.

In this case, you can compare any pair except c == e, since neither of C nor E is derived from the other. To compare those, you would either need to cross-cast, or to convert both to their common base class; neither of these can be done implicitly.

By the way, there's no need for dynamic_cast in your code, since you're casting to base class pointers and that safe conversion can be done implicitly.

like image 161
Mike Seymour Avatar answered Sep 26 '22 00:09

Mike Seymour