473,386 Members | 1,736 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,386 software developers and data experts.

pointer equality and inheritance, take 2

sb
Here is a different example, closer to what I want to do. I want to
have a class that models "relations" between objects. "add" adds a
relation, "check" checks if the relation exists:

#include <algorithm>
#include <vector>
#include <iostream>

class Base {
std::vector<Base*> relations;
public:
void add(Base* p) { relations.push_back(p); }
bool check(Base* p) {
return std::find(relations.begin(), relations.end(), p) \
!= relations.end();
}
};
class Derived1 : public Base {
// other members
};

class Derived2 : public Base {
// other members
};

class Derived2x : public Derived2 {
// other members
};
int main() {
Derived1 d1;
Derived2x d2x;
Derived2* p = &d2x;
d1.add(&d2x);
std::cout << d1.check(p) << '\n'; // always true ?
}
On my system, without any "other members", this is true.
Jul 22 '05 #1
3 1358
"sb" <sp**********@yahoo.com> wrote...
Here is a different example, closer to what I want to do. I want to
have a class that models "relations" between objects. "add" adds a
relation, "check" checks if the relation exists:

#include <algorithm>
#include <vector>
#include <iostream>

class Base {
std::vector<Base*> relations;
public:
void add(Base* p) { relations.push_back(p); }
bool check(Base* p) {
return std::find(relations.begin(), relations.end(), p) \
!= relations.end();
}
};
class Derived1 : public Base {
// other members
};

class Derived2 : public Base {
// other members
};

class Derived2x : public Derived2 {
// other members
};
int main() {
Derived1 d1;
Derived2x d2x;
Derived2* p = &d2x;
d1.add(&d2x);
std::cout << d1.check(p) << '\n'; // always true ?
}
On my system, without any "other members", this is true.


And _with_ "other members"? Hint: shouldn't be any difference.

I am not sure what you're asking about. 'p' and '&d2x' share
a common trait: a base class (both Derived2 and Derived2x have
'Base' as a base class). If you convert Derived2* to Base* or
Derived2x* to Base*, you are supposed to get the same exact
part of the object if you're dealing with the same object. It
is like you have

+----------------+
| Derived2x |<-- this is where &d2x points
| +------------+ |
| | Derived2 |<+-- this is where 'p' points
| | +--------+ | |
| | | Base |<+-+-- this is what you get when either of
| | | | | | them is converted to Base*
| | +--------+ | |
| +------------+ |
+----------------+

You began with the same object (d2x in your program). Its
address is '&d2x'. 'p' is a pointer to its 'Derived2' subobject.
Whether you convert the original '&d2x' or 'p' to 'Base', you
will get the same subobject (one directly, the other indirectly).

Victor

Jul 22 '05 #2

"sb" <sp**********@yahoo.com> wrote in message
news:22**************************@posting.google.c om...
Here is a different example, closer to what I want to do. I want to
have a class that models "relations" between objects. "add" adds a
relation, "check" checks if the relation exists:

#include <algorithm>
#include <vector>
#include <iostream>

class Base {
std::vector<Base*> relations;
public:
void add(Base* p) { relations.push_back(p); }
bool check(Base* p) {
return std::find(relations.begin(), relations.end(), p) \
!= relations.end();
}
};
class Derived1 : public Base {
// other members
};

class Derived2 : public Base {
// other members
};

class Derived2x : public Derived2 {
// other members
};
int main() {
Derived1 d1;
Derived2x d2x;
Derived2* p = &d2x;
d1.add(&d2x);
std::cout << d1.check(p) << '\n'; // always true ?
}
On my system, without any "other members", this is true.


Please don't create new threads.
You should have given this detail in the first one.

The answer is obviously YES. You only ran into trouble in your first thread
because you lose too much info when casting to void*
Jul 22 '05 #3

"sb" <sp**********@yahoo.com> wrote in message
news:22**************************@posting.google.c om...
Here is a different example, closer to what I want to do. I want to
have a class that models "relations" between objects. "add" adds a
relation, "check" checks if the relation exists:

#include <algorithm>
#include <vector>
#include <iostream>

class Base {
std::vector<Base*> relations;
public:
void add(Base* p) { relations.push_back(p); }
bool check(Base* p) {
return std::find(relations.begin(), relations.end(), p) \
!= relations.end();
}
};
class Derived1 : public Base {
// other members
};

class Derived2 : public Base {
// other members
};

class Derived2x : public Derived2 {
// other members
};
int main() {
Derived1 d1;
Derived2x d2x;
Derived2* p = &d2x;
d1.add(&d2x);
std::cout << d1.check(p) << '\n'; // always true ?
}
On my system, without any "other members", this is true.


Please don't create new threads.
You should have given this detail in the first one.

The answer is obviously YES. You only ran into trouble in your first thread
because you lose too much info when casting to void*
Jul 22 '05 #4

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

Similar topics

5
by: sb | last post by:
Given this relationship, class Base { //... }; class Derived : public Base { //... bool myself(const void* p) { return p == this}
2
by: sb | last post by:
Here is a different example, closer to what I want to do. I want to have a class that models "relations" between objects. "add" adds a relation, "check" checks if the relation exists: #include...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
40
by: Ike Naar | last post by:
In K&R "The C++ programming language (2nd ANSI C edition), the reference manual states (paragraphs 7.9 and 7.10) that pointer comparison is undefined for pointers that do not point to the same...
26
by: Meenu | last post by:
Hi, Can two different far pointers contain two different addresses but refer to the same location in memory?
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
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...

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.