POV-Ray : Newsgroups : povray.off-topic : Standard libraries : Re: Standard libraries Server Time
6 Sep 2024 13:20:17 EDT (-0400)
  Re: Standard libraries  
From: Warp
Date: 8 Mar 2009 12:03:02
Message: <49b3ec36@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> OK. What am I doing wrong here, that I'm not getting the size of the actual 
> instance passed in?

[snip]

> Why does this print "8 8 8 8"? If there's RTTI, shouldn't I at least be able 
> to figure out how big my structure is without having to code a function to 
> calculate it for every function?

  The problem is that sizeof() is a static construct which is evaluated at
compile time, not something evaluated at runtime. sizeof() in particular
does *not* take into accout RTTI. When you say "sizeof(*this)" (in your
example), that's by standard specification exactly identical to
"sizeof(Alpha)".

  But C++ does have RTTI. For example try this:


#include <iostream>
#include <string>
#include <typeinfo>

class Alpha
{
 public:
    virtual ~Alpha() {}
    std::string myType() const { return typeid(*this).name(); }
};

class Beta: public Alpha
{
};

int main()
{
    Alpha obj1;
    Beta obj2;
    Alpha& obj3 = obj2;

    std::cout << obj1.myType() << " "
              << obj2.myType() << " "
              << obj3.myType() << std::endl;
}


  Even though the class Alpha does not see the class Beta in any way (you
could make completely sure of that by putting them in different compilation
units), it will still return the correct typeid name depending on whether
the object is really of type Alpha or of type Beta.

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.