473,770 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting pointer to members

Compiling the following code (gcc 3.3.6), I get an invalid conversion
error.
Is there a valid reason why this conversion can't be performed?

---

class A {};
class B : public A {};

class T {
public:
B b;
};

int main() {
A T::*a = &T::b;
}

---

Paolo Capriotti

Oct 3 '05 #1
4 1248
* Paolo Capriotti:
Compiling the following code (gcc 3.3.6), I get an invalid conversion
error.
Is there a valid reason why this conversion can't be performed?

class A {};
class B : public A {};

class T {
public:
B b;
};

int main() {
A T::*a = &T::b;
}


For the purposes of this group, the explanation is that §3.9.2/3 tells us that
statements regarding pointers do not apply to pointers to members (except for
pointers to static members), so that the general B* -> A* conversion doesn't
apply, and then §4.11 does not list the conversion above for member pointers.

As to why, I think that's a defect in the standard.

I cannot think of any reason why the standard should forbid this conversion,
but then I don't understand e.g. how the dynamic type of an object that a
pointer to member is applied to, can not contain that member, as mentioned in
§5.5/4 (except for perhaps pure virtual functions, but that's covered
elsewhere and I think if it was that then the standard would just say so).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 3 '05 #2
Alf P. Steinbach wrote:
* Paolo Capriotti:
Compiling the following code (gcc 3.3.6), I get an invalid conversion
error.
Is there a valid reason why this conversion can't be performed?

class A {};
class B : public A {};

class T {
public:
B b;
};

int main() {
A T::*a = &T::b;
}


For the purposes of this group, the explanation is that §3.9.2/3 tells us that
statements regarding pointers do not apply to pointers to members (exceptfor
pointers to static members), so that the general B* -> A* conversion doesn't
apply, and then §4.11 does not list the conversion above for member pointers.

As to why, I think that's a defect in the standard.

I cannot think of any reason why the standard should forbid this conversion,
but then I don't understand e.g. how the dynamic type of an object that a
pointer to member is applied to, can not contain that member, as mentioned in
§5.5/4 (except for perhaps pure virtual functions, but that's covered
elsewhere and I think if it was that then the standard would just say so).


An object of class B may have stricter alignment requirements than an
object of class A. As a consequence, it is not safe to assume that an A
T::* member pointer can be safely converted to a B T::* member pointer.
They may in point to different locations in A and B respectively.

But as long as B is a subclass of A - and its alignment requirements
are no stricter than A's - then it is safe to use reinterpret_cas t to
convert from A T::* to an B T::* (see 5.2/9).

Greg

Oct 3 '05 #3
* Greg:

An object of class B may have stricter alignment requirements than an
object of class A. As a consequence, it is not safe to assume that an A
T::* member pointer can be safely converted to a B T::* member pointer.
They may in point to different locations in A and B respectively.

But as long as B is a subclass of A - and its alignment requirements
are no stricter than A's - then it is safe to use reinterpret_cas t to
convert from A T::* to an B T::* (see 5.2/9).


I don't understand what you're saying, in particular the conversion you're
discussing seems to be opposite of the original example, and using the result
of a one-way reinterpret_cas t is simply undefined (do you perhaps mean
in-practice safe?), but I see that alignment requirements are mentioned for
round-trip (there and back again) reinterpret_cas t discussed in §5.2.10/9.

Could you give an example or elaboration?

Anyway, thanks for the reference to §5.2/9, I assume you meant §5.2.9/9?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 3 '05 #4
class A {};
class B : public A {};

class T {
public:
B b;
};

int main() {
T t;
A*a = &t.b;
}

Oct 3 '05 #5

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

Similar topics

4
3481
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A good way to do this is by example. So I will give an example and please tell me what you think: I have a base class A with a virtual destructor, and a class B that is it inherits publicly from A and defines som extra stuff.
5
3153
by: Ingo Nolden | last post by:
Hi there, I am writing a smart pointer that is similar to the boost intrusive ptr. I am trying to make it behave like a c++ pointer including the implicit and explicit casting behaviour. Below is the part more or less relevant to my problem. Version 1 is fine for casting to a derived class, but it must be called explicitly even though it is used to cast to a base class.
3
7047
by: Rob Jackson | last post by:
HiI've got a struct, known by file A.c, which contains a pointer to struct B. Struct B is unknown by file A.c (it is declared in C.h), and contains a typedef enum, which is declared in a file B.h, which is included in file A.c (this is getting really confusing, I know!).I want to access the enum in file A.c, but don't want to include file C.h, for various reasons.I've tried casting the enum, but still get the error "Dereferencing pointer...
3
2991
by: joe bruin | last post by:
hello all. i am trying to get rid of some warnings and do "the right thing". although in this particular case, i am not sure what the right thing is. the code: typedef struct {
20
2117
by: j0mbolar | last post by:
I was reading page 720 of unix network programming, volume one, second edition. In this udp_write function he does the following: void udp_write(char *buf, <everything else omitted) struct udpiphdr *ui; struct ip *ip; ip = (struct ip *) buf;
17
2573
by: goldfita | last post by:
I saw some code that appeared to do something similar to this struct foo { char offset; int d; }; struct foo { int a; int b;
8
5664
by: wkaras | last post by:
In my compiler, the following code generates an error: union U { int i; double d; }; U u; int *ip = &u.i; U *up = static_cast<U *>(ip); // error I have to change the cast to reinterpret_cast for the code
4
15312
by: Schkhan | last post by:
Hi, I am sturggling with a peace of code mainly its about casting a pointer to one type of structure to a pointer to another type of structure. the confusion begins with the following line...(modified to highlight the actual prob) #define TEST_MACRO(first) ((struct second *) &first) /*here it would return a pointer to second, but the question is why it uses Here in this example "first" and "second" are two different structures*/
9
3465
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
0
9618
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10260
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
10101
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...
0
9906
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
8933
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
7456
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
6712
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();...
0
5354
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
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

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.