 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Edmund Kapusniak Guest
|
Posted: Fri Jan 21, 2005 10:28 am Post subject: Pointer to member polymorphism |
|
|
Hi,
Why does the following not work?
class base
{
};
class derived
: public base
{
};
struct s
{
derived member;
};
int main( int argc, char* argv[] )
{
base s::* pointer_to_member = &s::member;
}
I realise that &s::member is of type derived s::* and not base s::*,
but with 'normal' pointers I can assign a pointer to a derived class to
a pointer of base class type with no problems. Is the same not true of
pointers to members?
If there isn't any way of achieving what I want, does anyone know the
reason why it's not allowed?
Thanks,
--
Edmund
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
dtmoore Guest
|
Posted: Sat Jan 22, 2005 5:35 am Post subject: Re: Pointer to member polymorphism |
|
|
Edmund Kapusniak wrote:
| Quote: | Hi,
Why does the following not work?
class base
{
};
class derived
: public base
{
};
struct s
{
derived member;
};
int main( int argc, char* argv[] )
{
base s::* pointer_to_member = &s::member;
}
|
You are trying to bind a pointer to part of an object that doesn't
exist. You need to create an object of type s somewhere, then you
should be able to bind its "member" object to a pointer to the base or
derived type.
HTH,
Dave Moore
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Sat Jan 22, 2005 5:35 am Post subject: Re: Pointer to member polymorphism |
|
|
Edmund Kapusniak <birotanker (AT) hotmail (DOT) com> writes:
| Quote: | class derived
: public base
{
};
struct s
{
derived member;
};
int main( int argc, char* argv[] )
{
base s::* pointer_to_member = &s::member;
}
I realise that &s::member is of type derived s::* and not base s::*,
but with 'normal' pointers I can assign a pointer to a derived class to
a pointer of base class type with no problems. Is the same not true of
pointers to members?
|
Yes. It's not true. The two pointer-to-member types in question are
unrelated.
| Quote: | If there isn't any way of achieving what I want, does anyone know the
reason why it's not allowed?
|
What problem are you trying to solve?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sat Jan 22, 2005 11:25 am Post subject: Re: Pointer to member polymorphism |
|
|
"Edmund Kapusniak" <birotanker (AT) hotmail (DOT) com> wrote...
| Quote: | Why does the following not work?
class base
{
};
class derived
: public base
{
};
struct s
{
derived member;
};
int main( int argc, char* argv[] )
{
base s::* pointer_to_member = &s::member;
}
I realise that &s::member is of type derived s::* and not base s::*,
but with 'normal' pointers I can assign a pointer to a derived class to
a pointer of base class type with no problems. Is the same not true of
pointers to members?
If there isn't any way of achieving what I want, does anyone know the
reason why it's not allowed?
|
It's been discussed here a couple of times, IIRC. The agreement was that
it wasn't needed, so nobody cared to figure it out, IIRC, again. Of course
it is quite possible that I don't RC, and there was some other reason.
Victor
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|