Boost logo

Geometry :

Subject: [ggl] space partitioning
From: Duzy Chan (duzy)
Date: 2010-09-11 00:49:04


On Fri, Sep 10, 2010 at 9:11 PM, Hartmut Kaiser <hartmut.kaiser_at_[hidden]>wrote:

> > Since all these examples uses EMPTY classes, codes as following will
> > complaint nothing and event run as people expects -- the tree::foo() will
> > be invoked:
> >
> > struct tree : public node
> > {
> > void foo()
> > {
> > do_something_with_node(static_cast<node*>(this));
> > }
> > /* no atributes */
> > }
> >
> > internal_node *in = new internal_node(); tree *t =
> > static_cast<tree*>(static_cast<node*>(in));
> > t->foo();
> >
> > As a member function needs a object pointer to be invoked, in facts, it's
> > not matter what's the pointer really pointed to, if your class hierarchy
> > is empty classes, no CRASH will complaint. But if you try to insert some
> > member fields in 'tree', and intend to access any fields, the whole world
> > will CRASH -- the 'this' pointer is in fact pointed to something totally
> > different.
>
> Even with empty classes it's still undefined behavior, so all bets are off.
>
>
Still undefined of cause, unless people uses virtual on 'foo()'. In fact
examples like that tree-node-foo is nothing object oriented, that
empty-class trick is just a strange thing in object oriented, hides real
object type to C++, and cause more strange things happened while doing
typeinfo on that 'this' pointer of tree. Think of the following segment:

#include <iostream>

#include <typeinfo>

> struct base

{

};

> struct derived : base

{

  void blah() { std::cout<<"derived"<<std::endl; }

  void pointer_type() { std::cout<<typeid(this).name()<<std::endl; }

  void object_type() { std::cout<<typeid(*this).name()<<std::endl; }

};

> int main()

{

  base obj;

  base *o = &obj;

  derived *p = (derived*)o;

  p->blah();

  p->pointer_type();

  p->object_type();

}

Well the typeid operator is in fact static binded, so people may gains
output as one expected, but we all knows that the C++ compiler has been
tricked. *How to make C++ compiler smarter? And how could C++ people be more
aware of the real type without so many virtuals?*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/ggl/attachments/20100911/e28fd418/attachment.html


Geometry list run by mateusz at loskot.net