Newsgroups: comp.lang.c++
From: Juha Nieminen <nos...@thanks.invalid>
Date: Fri, 20 Feb 2009 14:25:21 GMT
Local: Fri, Feb 20 2009 2:25 pm
Subject: Automatically create a clone() function for derived classes
I'm sure this is a frequently asked question, but...
Suppose that I have a hierarchy of objects which are typically class Base // other stuff here }; The problem is that this clone() function must be replicated in all the derived classes as well, and their implementation is always the same, except for the type which is being allocated. For example: class Derived: public Base }; It's not only tedious to have to write this line in each of the derived classes, but it's also error-prone: If you ever forget to add that line to some derived class, no compiler error will happen, and the program will even run, but when cloning a derived object using a base type pointer, you get a crippled copy, so the whole thing malfunctions, and it might malfunction in a very non-obvious way which is hard to track and debug. Isn't there any way of automating this process? (And without making You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||