Thread: forbid creating copies of objects of child classes

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    forbid creating copies of objects of child classes

    Hello everyone,

    I just joined this forum and I already have a few questions I'd like to ask.

    First of all, I want to collect an undefined number of objects of miscellaneous classes in an STL vector. All these classes inherit from some abstract base class:
    Code:
    class Base { ... };
    Since one cannot instantiate a base class object the vector needs to be defined this way:
    Code:
    std::vector<Base*> allobjects;
    Btw.: all objects are created by factory functions, which create the class objects and return them via pointer.

    Of course I'll want to make (deep) copies of that vector at some point during run time. Since now I have to manually create those deep copies I would make use of the new operator and pass the old objects to the copy constructor. But there's the problem: I cannot do that, because I don't know the real type of the objects. So, my solution was to declare a purely virtual factory method to create a copy of "this" and return it via pointer. In every class, that inherits from "Base", the method was implemented according to the class hierarchy:
    Code:
    class Base
    {
        ...
        public:
            virtual Base* createDeepCopy(void) const = 0;
        protected:
            Base(const Base& to_copy);
        private:
            Base& operator=(const Base& rhs); // no implementation, not supposed to be called
        ...
    };
    class AChild : public Base
    {
        ...
        public:
            virtual AChild* createDeepCopy(void) const
            {
                AChild* tmp = new AChild(*this);
                // copying not inherited members
                return tmp;
            }
        ...
    };
    So, my first question would be: am I right with my assumptions above or is there a better solution for this?

    Now it gets even worse: I would like to have some child class, whose instances must not be copied!
    Code:
    class Base
    {
        // as above
    };
    class AChild : public Base
    {
        // as above
    };
    class BChild : public AChild // must not be copied!!!
    {
        ...
    };
    Of course objects of type "BChild" will never be put into the mentioned vector -- this is assured in the program.

    All I came up for a solution was this:
    Code:
    class BChild : public AChild
    {
        ...
        private:
            virtual BChild* createDeepCopy(void) const
            {
                // no real implementation
                return NULL;
            }
        ...
    };
    Second question: Is there a better way to implement this?

    Thanks in advance for your comments and hints.

    Regards
    Sigi

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Once a base class has a public virtual function, you cannot prevent it from being called.

    As for the container, look at Boost's ptr_vector. It does a lot of the stuff you want, including deep copying for objects that provide a clone() function.
    http://boost.org/libs/ptr_container/...container.html
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Can you keep the types, though? With templates functions, it's possible to keep the type of all your objects for as long as you can. If it so happens you can do it, you can just use the normal operator = and the copy constructor.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    Hi again,

    thanks for all the answers. I'm using ptr_vector now even though it took me a while to figure out how to make deep copies of such a vector.

    As for preventing calls to a child class copy constructor: I'm putting an
    Code:
    assert(false);
    in there so that the program at least terminates in debug mode.

    Regargs
    Sigi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing/working with member objects of parent classes
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2005, 11:01 PM
  2. Replies: 8
    Last Post: 01-12-2004, 10:47 PM
  3. Mixing objects from different classes?
    By teedee46 in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2002, 10:28 AM
  4. Trouble Understanding Classes and Objects. Please Help.
    By Jeffcubed in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 02:23 PM
  5. classes in an exe. objects from text file.
    By davebaggott in forum Windows Programming
    Replies: 0
    Last Post: 10-08-2001, 02:55 AM