POV-Ray : Newsgroups : povray.off-topic : Teach yourself C++ in 21 days : Re: Days 1-5 Server Time
29 Jul 2024 20:18:38 EDT (-0400)
  Re: Days 1-5  
From: Invisible
Date: 20 Apr 2012 11:25:44
Message: <4f917ff8$1@news.povray.org>
On 20/04/2012 04:16 PM, Warp wrote:

>    Btw, a class that has only been declared but not defined (in other words,
> you have only said "class X;") is called an "incomplete type" in C++
> parlance.

Right.

>    You can have pointers and references to an incomplete type, and you can
> use it wherever a type is required (without any actual object), such as in
> function declarations. In other words, you can do eg. like this:
>
> //--------------------------------------------------------------------
> class MyClass;
>
> // Function that takes an object of type MyClass as parameter and
> // returns another object of that type:
> MyClass aFunction(MyClass object);
> //--------------------------------------------------------------------
>
>    (Of course if you try to *call* that function without a full class
> definition, you'll get a compiler error.)

How about if you try to write the function body without the full class? 
I'm presuming that will fail too, since the compiler needs to know how 
much space to allocate...

>    You could also declare an extern variable of that type (although that's
> a quite rare circumstance, but it's still possible).

Right. But presumably you can't actually /create/ the variable, because 
you don't know it's size. (?)

>    One situation where you ought to not be able to do it, but you can, due
> to a quirk in the language, is deleting an object using an incomplete
> type. In other words, this will compile, albeit usually with a warning:
>
> //--------------------------------------------------------------------
> class MyClass;
>
> void foo(MyClass* objPtr)
> {
>      delete objPtr; // Never do this!
> }
> //--------------------------------------------------------------------
>
>    The major, major problem with that is that, while the memory taken by
> that object will be freed, the destructor of the object will *not* be
> called. Bad things will happen. (There was some reasoning why it's
> nevertheless allowed by the standard, but I don't remember now what it
> was.)

Oooohhhhh that's evil.

>    In the code above where the returned object is destroyed, at no point do we
> have a full definition of 'MyClass'. Nevertheless, when the std::shared_ptr
> that's managing it goes out of scope, it will delete it and its destructor
> will be called. It achieves this through template magic.

Sure. When the template expands, we /do/ have the full class spec, so it 
works. (At least, I'm presuming that's what happens.)

>    (Nevertheless, you seldom need to resort to this kind of trickery in
> practical code. At least not this explicitly. Don't take this as an
> example of what kind of code people usually write in C++.)

Oh hell yes - I don't intend on trying this... ever, actually.


Post a reply to this message

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