C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Pointer to member polymorphism

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Edmund Kapusniak
Guest





PostPosted: Fri Jan 21, 2005 10:28 am    Post subject: Pointer to member polymorphism Reply with 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;
}


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





PostPosted: Sat Jan 22, 2005 5:35 am    Post subject: Re: Pointer to member polymorphism Reply with quote




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





PostPosted: Sat Jan 22, 2005 5:35 am    Post subject: Re: Pointer to member polymorphism Reply with quote



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





PostPosted: Sat Jan 22, 2005 11:25 am    Post subject: Re: Pointer to member polymorphism Reply with quote

"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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.