POV-Ray : Newsgroups : povray.off-topic : New NearInfinity demo up : Re: New NearInfinity demo up Server Time
9 Oct 2024 22:24:08 EDT (-0400)
  Re: New NearInfinity demo up  
From: Warp
Date: 16 Dec 2008 16:31:01
Message: <49481e15@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> >   Copy constructors are called, for example, when you pass an object to
> > a function by value.

> Good to know. Thanks!  Still wading thru the C++ manuals and tutorials here.

  Copy constructors and assignment operators are important to know in C++
when designing classes.

  Usually when your class only has basic types and managed classes as
members, you don't need to create a copy constructor nor assignment operator
for your class. The default ones generated by the compiler will usually
suffice. (The default ones simply copy/assign each member of the parameter
to each member of this.)

  For example:

class MyClass
{
    int x, y; // basic types
    std::string name; // managed class

 public:
    // No need to write your own copy constructor / assignment operator
    // here because the compiler-generated ones suffice.
    ...
};

  (I think the term "managed" is not generally used in pure C++, but I'll
use that term because I can't think of any better one. Not to be confused
with managed classes in .NET; I'm not talking about those at all. With
"managed" I mean a class which may allocate memory inside itself, but is
well-behaved and can be safely copied and assigned around without worries.
std::string and the STL containers are examples.)

  Problems happen when your class has, for example, raw pointers to memory
it has allocated, or other similar resources. Example:

class MyClass
{
    int* array;

 public:
    MyClass(int size)
    {
        array = new int[size];
    }

    ~MyClass() { delete[] array; }

    // If now you don't implement a copy constructor / assignment operator
    // you will have a problem!
};

  How to solve the problem depends on how you want your class to behave.

  By far the easiest (and most common) solution is to simply forbid copying
and assignment of your class. This happens by declaring the copy constructor
and assignment operator in the private part of the class and not implementing
them. Problem solved. Of course now you can't copy nor assign instances of
your class (nor can you use them in STL containers), but in many cases you
don't need to. This is very typical for classes which you instantiate only
once during the execution of the program.

  What forbidding those functionalities does is prevent accidents, so it's
a good custom, even if you are not intending to copy/assign the class around.

  Sometimes you do want to copy/assign instances of your class, though.
In that case you have to decide whether the copies should share the data
or deep-copy it. If deep-copying is enough, then by far the easiest solution
is replacing your array with a std::vector, and consequently you don't need
to write any copy constructor / assignment operator. If you want to share,
then you could use the smart pointers offered by the Boost library. (The
current C++ standard doesn't yet offer such smart pointers, except for
auto_ptr, which cannot be used for sharing data.)

  Of course this is good only if your data is an array. If your dynamic data
has a more complex geometry, it becomes a bit more complicated.

  Also the data might not be memory at all. For example this presents the
exact same problem:

class MyClass
{
    std::FILE* inputFile;

 public:
    MyClass(): inputFile(0) {}
    ~MyClass()
    {
        if(inputFile) std::fclose(inputFile);
    }

    bool openFile(const char* filename)
    {
        if(inputFile) std::fclose(inputFile);
        return (inputFile = std::fopen(filename, "rb"));
    }

    ...
};

  The data is not memory, but if you copy/assign instances of this class,
you'll get a problem (you may access or close a closed file).

  A more subtle example:

class MyClass
{
    std::list<Node> nodes;
    std::list<Node>::iterator currentNode;
    ...
};

  Assume 'currentNode' points to some node in the list, or to nodes.end().
If this class doesn't have a proper copy constructor / assignment operator
and you copy instances of it, you'll get problems.

  I suppose the morale of this long text is: If you stick to basic types
and managed classes (as I call them) as far as possible, you'll avoid
problems. Pointers and iterators as members are often problematic.

  (Note that sometimes you might want to forbid copying/assignment even
if the compiler-generated ones were safe. This may be the case eg. if
the copying would be a very heavy operation, such as if you have huge
vectors as member variables.)

-- 
                                                          - Warp


Post a reply to this message

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