Topic: RTTI: sizeof operator 4 polymorphic types
Author: "Emmanuel Deloget" <logout@free.fr>
Date: Fri, 1 Dec 2006 10:08:00 CST Raw View
jam a crit :
> It would be greater if sizeof could obtain size of polymorphic objects
> at runtime.assume:
>
> class polymorphic{virtual....};//a poly morphic type
>
> struct subtype:polymorphic{...};
>
> subtype ob;
>
> polymorphic ptr=&ob;
>
> now 'sizeof(*ptr)' should return 'sizeof(subtype)' not
> 'sizeof(polymorphic)'.
Won't that break loads of code that assume that sizeof() is executed at
compile time (where, obviously, such kind of runtime informations are
not known)? sizeof() is a primary tool in C++, and changing its
behavior may not be without consequence. Moreover, there are some easy
way to fix this issue when it is needed (using for example a virtual
function).
Regards,
-- Emmanuel Deloget, Artware
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: ron@spamcop.net (Ron Natalie)
Date: Fri, 1 Dec 2006 16:43:30 GMT Raw View
jam wrote:
> now 'sizeof(*ptr)' should return 'sizeof(subtype)' not
> 'sizeof(polymorphic)'.
>
sizeof evaluates to a constant expression in C++ currently.
Of course, C's vla changes this, so it's not completely
inconceivable. Of course if you're going to go to that
length, perhaps sizeof should be overloadable as well.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "peter koch" <peter.koch.larsen@gmail.com>
Date: Fri, 1 Dec 2006 11:05:32 CST Raw View
jam skrev:
> It would be greater if sizeof could obtain size of polymorphic objects
> at runtime.assume:
>
> class polymorphic{virtual....};//a poly morphic type
>
> struct subtype:polymorphic{...};
>
> subtype ob;
>
> polymorphic ptr=&ob;
>
> now 'sizeof(*ptr)' should return 'sizeof(subtype)' not
> 'sizeof(polymorphic)'.
I do not see why. You must provide an example to demonstrate the virtue
- and also why you can't just implement a size operator yourself:
class polymorphic
{
virtual size_t byte_size() const;
};
(which could be made more or less automatic using CRTP)
/Peter
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "jam" <farid.mehrabi@gmail.com>
Date: Thu, 30 Nov 2006 16:05:32 CST Raw View
It would be greater if sizeof could obtain size of polymorphic objects
at runtime.assume:
class polymorphic{virtual....};//a poly morphic type
struct subtype:polymorphic{...};
subtype ob;
polymorphic ptr=&ob;
now 'sizeof(*ptr)' should return 'sizeof(subtype)' not
'sizeof(polymorphic)'.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]