POV-Ray : Newsgroups : povray.off-topic : Adventures with C++ : An actual C++ question Server Time
29 Jul 2024 00:23:06 EDT (-0400)
  An actual C++ question  
From: Orchid Win7 v1
Date: 15 May 2013 15:58:52
Message: <5193e8fc$1@news.povray.org>
OK, so I have a class that looks like this:

   class Foobar
   {
     private:
       Secrets secrets;
     public:
       int Foo1();
       int Foo2();
       int Foo3();
   };

Now I want to put that in a header file. What I *actually* want to write is

   class Foobar
   {
     public:
       int Foo1();
       int Foo2();
       int Foo3();
   };

However, this doesn't work at all, because now the "real" Foobar class 
is a different size to what all the clients of Foobar are expecting. 
General mayhem ensues.

So I have to put the entire class definition into the header file. But 
that means I have to put Secrets into the header file too - a data 
structure that clients have absolutely no reason to know anything about. 
(And I have to include everything that Secrets mentions, and everything 
that those things mention, and...)

I thought perhaps I could just write an empty forward declaration, but 
unsurprisingly that doesn't work; the compiler needs size information.

After Googling it, the only solution I could find was to make the 
private field a *pointer* to the secret data. Now I can put a dummy 
forward declaration in the header file, and define the real data 
structure in the implementation, where it belongs.

And now I'm doing manual memory management. >_<

The example I got from Google shows a class with a constructor and a 
destructor - so, following Warp's teachings, my immediate reaction is 
"what the hell happens if somebody copies the object?" I fear the 
secrets will be double-freed at some point.

My question is... Is there some simple change I might make to the code 
to avoid all the gotchas of manual memory management?

I mean, there will only ever be one object, created in one place, so it 
"should" be fine... but it still makes me nervous. If some day somebody 
tries to use the code in a new way, I don't want it to suddenly fail in 
a spectacular way so some poor sod has to spend 3 weeks trying to figure 
out why...


Post a reply to this message

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