473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Possible to require overloading of a non-pure method?

Hi all, I have a set of classes which implement the virtual
constructor idiom.

I had a slicing problem which resulted when I forgot to override the
clone() function in a derived class.

Is there something (other than documentation) that I can do to prevent
this from happening again?

TIA,
--rob
Jun 27 '08 #1
11 1194
On 2008-04-25 06:40:20 -0400, "[rob desbois]" <ro*********@gmail.comsaid:
Hi all, I have a set of classes which implement the virtual
constructor idiom.

I had a slicing problem which resulted when I forgot to override the
clone() function in a derived class.

Is there something (other than documentation) that I can do to prevent
this from happening again?
Unit testing and code reviews.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #2
On Apr 25, 12:46*pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-04-25 06:40:20 -0400, "[rob desbois]" <rob.desb...@gmail.comsaid:
Hi all, I have a set of classes which implement the virtual
constructor idiom.
I had a slicing problem which resulted when I forgot to override the
clone() function in a derived class.
Is there something (other than documentation) that I can do to prevent
this from happening again?

Unit testing and code reviews.
Allow me to clarify. I meant is there any language construct which can
enforce this requirement.

A unit test wouldn't have helped -- I'd have had to remember to write
a unit test for the new class's clone() method ensuring that the
returned pointer is castable to a pointer to the new derived type. If
I'd have remembered that I'd have remembered to code the clone method
anyway.

--rob
Jun 27 '08 #3
On 2008-04-25 10:59:20 -0400, "[rob desbois]" <ro*********@gmail.comsaid:
On Apr 25, 12:46*pm, Pete Becker <p...@versatilecoding.comwrote:
>On 2008-04-25 06:40:20 -0400, "[rob desbois]" <rob.desb...@gmail.comsaid
:
>>
>>Hi all, I have a set of classes which implement the virtual
constructor idiom.
>>I had a slicing problem which resulted when I forgot to override the
clone() function in a derived class.
>>Is there something (other than documentation) that I can do to prevent
this from happening again?

Unit testing and code reviews.

Allow me to clarify. I meant is there any language construct which can
enforce this requirement.

A unit test wouldn't have helped -- I'd have had to remember to write
a unit test for the new class's clone() method ensuring that the
returned pointer is castable to a pointer to the new derived type. If
I'd have remembered that I'd have remembered to code the clone method
anyway.

Unit tests would have helped, if they are written correctly and
reviewed correctly. What you're asking for is a language mechamism to
compensate for a weak development process. Fix the process.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #4
[rob desbois] schrieb:
On Apr 25, 12:46 pm, Pete Becker <p...@versatilecoding.comwrote:
>On 2008-04-25 06:40:20 -0400, "[rob desbois]" <rob.desb...@gmail.comsaid:
>>Hi all, I have a set of classes which implement the virtual
constructor idiom.
I had a slicing problem which resulted when I forgot to override the
clone() function in a derived class.
Is there something (other than documentation) that I can do to prevent
this from happening again?
Unit testing and code reviews.

Allow me to clarify. I meant is there any language construct which can
enforce this requirement.

A unit test wouldn't have helped -- I'd have had to remember to write
a unit test for the new class's clone() method ensuring that the
returned pointer is castable to a pointer to the new derived type. If
I'd have remembered that I'd have remembered to code the clone method
anyway.
"Design by Contract".

Something like (untestet):

class Base
{
public:
Base* clone() const
{
Base* p = doClone();
assert(typeid(*p) == typeid(*this));
return p;
}

private:
Base* doClone() const
{
return new Base(*this);
}
}

--
Thomas
Jun 27 '08 #5
Thomas J. Gritzan wrote:
[rob desbois] schrieb:
>On Apr 25, 12:46 pm, Pete Becker <p...@versatilecoding.comwrote:
>>On 2008-04-25 06:40:20 -0400, "[rob desbois]"
<rob.desb...@gmail.comsaid:
Hi all, I have a set of classes which implement the virtual
constructor idiom.
I had a slicing problem which resulted when I forgot to override
the clone() function in a derived class.
Is there something (other than documentation) that I can do to
prevent this from happening again?
Unit testing and code reviews.

Allow me to clarify. I meant is there any language construct which
can enforce this requirement.

A unit test wouldn't have helped -- I'd have had to remember to write
a unit test for the new class's clone() method ensuring that the
returned pointer is castable to a pointer to the new derived type. If
I'd have remembered that I'd have remembered to code the clone method
anyway.

"Design by Contract".

Something like (untestet):

class Base
{
public:
Base* clone() const
{
Base* p = doClone();
assert(typeid(*p) == typeid(*this));
return p;
}

private:
Base* doClone() const
{
return new Base(*this);
}
}
;

First off, no virtual functions here, did you mean to make 'doClone'
virtual? And second, he needed the compile-time solution, AIUI.

And how does 'typeid' trick work? Does 'typeid' return the dynamic
type of '*this'? So, if you derive from 'Base', what then? How
does the splitting of the function in two help _forcing_ the derived
class provide the override?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #6
On Apr 25, 4:58*pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Thomas J. Gritzan wrote:
[rob desbois] schrieb:
On Apr 25, 12:46 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-04-25 06:40:20 -0400, "[rob desbois]"
<rob.desb...@gmail.comsaid:
Hi all, I have a set of classes which implement the virtual
constructor idiom.
I had a slicing problem which resulted when I forgot to override
the clone() function in a derived class.
Is there something (other than documentation) that I can do to
prevent this from happening again?
Unit testing and code reviews.
Allow me to clarify. I meant is there any language construct which
can enforce this requirement.
A unit test wouldn't have helped -- I'd have had to remember to write
a unit test for the new class's clone() method ensuring that the
returned pointer is castable to a pointer to the new derived type. If
I'd have remembered that I'd have remembered to code the clone method
anyway.
"Design by Contract".
Something like (untestet):
class Base
{
public:
* *Base* clone() const
* *{
* * * Base* p = doClone();
* * * assert(typeid(*p) == typeid(*this));
* * * return p;
* *}
private:
* *Base* doClone() const
* *{
* * * return new Base(*this);
* *}
}

* * ;

First off, no virtual functions here, did you mean to make 'doClone'
virtual? *And second, he needed the compile-time solution, AIUI.
I expect he did.
And how does 'typeid' trick work? *Does 'typeid' return the dynamic
type of '*this'? *So, if you derive from 'Base', what then? *How
does the splitting of the function in two help _forcing_ the derived
class provide the override?
typeid is part of C++'s runtime type identification (RTTI)
functionality and yes, provides a dynamic type identifier of 'this'.
If I derive from Base without overriding doClone(), then the assertion
fails:
assert(typeid(*p) == typeid(*this));
because *p is a Base and *this is a Derived.

--rob
Jun 27 '08 #7
On Apr 25, 4:22*pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-04-25 10:59:20 -0400, "[rob desbois]" <rob.desb...@gmail.comsaid:
On Apr 25, 12:46*pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-04-25 06:40:20 -0400, "[rob desbois]" <rob.desb...@gmail.comsaid
:
>Hi all, I have a set of classes which implement the virtual
constructor idiom.
>I had a slicing problem which resulted when I forgot to override the
clone() function in a derived class.
>Is there something (other than documentation) that I can do to prevent
this from happening again?
Unit testing and code reviews.
Allow me to clarify. I meant is there any language construct which can
enforce this requirement.
A unit test wouldn't have helped -- I'd have had to remember to write
a unit test for the new class's clone() method ensuring that the
returned pointer is castable to a pointer to the new derived type. If
I'd have remembered that I'd have remembered to code the clone method
anyway.

Unit tests would have helped, if they are written correctly and
reviewed correctly. What you're asking for is a language mechamism to
compensate for a weak development process. Fix the process.
The language, its syntax, and libraries are there to support the
developer and the development process.
Would you suggest that using assert() is a weak development process?

Assuming that you were developing this and made the same basic mistake
that I did here -- forgot to override clone() -- where exactly would
the mistake have been spotted?
A correctly written unit test? I forgot to implement clone() so would
likely have forgotten the test for it.
A correctly reviewed unit test? In a team of 2 developers in a small
company developing specialised software, where I have the most
experience, I think it unlikely that it would've been caught.

Is there something else I could've done?

--rob
Jun 27 '08 #8
On 2008-04-25 12:56:34 -0400, "[rob desbois]" <ro*********@gmail.comsaid:
On Apr 25, 4:22*pm, Pete Becker <p...@versatilecoding.comwrote:
>On 2008-04-25 10:59:20 -0400, "[rob desbois]" <rob.desb...@gmail.comsaid
:
>>

>>On Apr 25, 12:46*pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-04-25 06:40:20 -0400, "[rob desbois]" <rob.desb...@gmail.coms
aid
>>:
>>>>Hi all, I have a set of classes which implement the virtual
constructor idiom.
>>>>I had a slicing problem which resulted when I forgot to override the
clone() function in a derived class.
>>>>Is there something (other than documentation) that I can do to prevent
>>>>this from happening again?
>>>Unit testing and code reviews.
>>Allow me to clarify. I meant is there any language construct which can
enforce this requirement.
>>A unit test wouldn't have helped -- I'd have had to remember to write
a unit test for the new class's clone() method ensuring that the
returned pointer is castable to a pointer to the new derived type. If
I'd have remembered that I'd have remembered to code the clone method
anyway.

Unit tests would have helped, if they are written correctly and
reviewed correctly. What you're asking for is a language mechamism to
compensate for a weak development process. Fix the process.

The language, its syntax, and libraries are there to support the
developer and the development process.
Would you suggest that using assert() is a weak development process?
No.
>
Assuming that you were developing this and made the same basic mistake
that I did here -- forgot to override clone() -- where exactly would
the mistake have been spotted?
In the unit tests.
A correctly written unit test? I forgot to implement clone() so would
likely have forgotten the test for it.
A correctly reviewed unit test? In a team of 2 developers in a small
company developing specialised software, where I have the most
experience, I think it unlikely that it would've been caught.
First, accidental omissions are much more likely to be noticed by
someone who didn't write the code. That's the point of reviews: to get
fresh eyes on the code. Second, overriding clone whenever a class is
derived from Base should be part of the review checklist. So, yes, a
review would almost certainly have caught this error.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #9
On Apr 25, 5:58 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Thomas J. Gritzan wrote:
[rob desbois] schrieb:
On Apr 25, 12:46 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-04-25 06:40:20 -0400, "[rob desbois]"
<rob.desb...@gmail.comsaid:
Hi all, I have a set of classes which implement the virtual
constructor idiom.
I had a slicing problem which resulted when I forgot to override
the clone() function in a derived class.
Is there something (other than documentation) that I can do to
prevent this from happening again?
Unit testing and code reviews.
Allow me to clarify. I meant is there any language
construct which can enforce this requirement.
A unit test wouldn't have helped -- I'd have had to
remember to write a unit test for the new class's clone()
method ensuring that the returned pointer is castable to a
pointer to the new derived type. If I'd have remembered
that I'd have remembered to code the clone method anyway.
"Design by Contract".
(That should be "Programming by Contract" here. It's a coding
issue, not a design issue.)
Something like (untestet):
class Base
{
public:
Base* clone() const
{
Base* p = doClone();
assert(typeid(*p) == typeid(*this));
return p;
}
private:
Base* doClone() const
{
return new Base(*this);
}
} ;
First off, no virtual functions here, did you mean to make
'doClone' virtual?
Obviously.
And second, he needed the compile-time solution, AIUI.
You take what you can get:-).

I think I'm the one who actually invented this technique, at
least with regards to using typeid for cloning. In practice,
however, I find that this particular bit of programming by
contract is simply not worth the extra work (although I tend to
use programming by contract fairly intensively in my work
otherwise). As Pete has said, failing to provide the clone
function is something that simply doesn't slip past code review
and unit tests.
And how does 'typeid' trick work? Does 'typeid' return the dynamic
type of '*this'?
It will if the static type is polymorphic.
So, if you derive from 'Base', what then? How does the
splitting of the function in two help _forcing_ the derived
class provide the override?
You get an assertion failure if you attempt to clone a class
which has not implemented the clone function.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #10
Victor Bazarov schrieb:
Thomas J. Gritzan wrote:
>[rob desbois] schrieb:
>>>>Hi all, I have a set of classes which implement the virtual
constructor idiom.
I had a slicing problem which resulted when I forgot to override
the clone() function in a derived class.
Is there something (other than documentation) that I can do to
prevent this from happening again?
"Design by Contract".

Something like (untestet):

class Base
{
public:
Base* clone() const
{
Base* p = doClone();
assert(typeid(*p) == typeid(*this));
return p;
}

private:
Base* doClone() const
virtual Base* doClone() const
> {
return new Base(*this);
}
}
;

First off, no virtual functions here, did you mean to make 'doClone'
virtual? And second, he needed the compile-time solution, AIUI.
First: Yes, I forgot the virtual. That's what code review is for :-)
Thanks for that.

Second, there is no compile-time solution for this. You can't force to
override a virtual function.
And how does 'typeid' trick work? Does 'typeid' return the dynamic
type of '*this'? So, if you derive from 'Base', what then? How
does the splitting of the function in two help _forcing_ the derived
class provide the override?
Having a polymorphic class (a class with virtual functions is
polymorphic), typeid(x) returns the dynamic type of x.

In the expression
assert(typeid(*p) == typeid(*this));

typeid(*this) returns the dynamic, run-time type of the current object,
typeid(*p) return the dynamic type of the new object. So, if the clone
doesn't have the exact same type as the current object, the
postcondition of the clone() method is violated.

--
Thomas
Jun 27 '08 #11
James Kanze schrieb:
On Apr 25, 5:58 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>First off, no virtual functions here, did you mean to make
'doClone' virtual?

Obviously.
>And second, he needed the compile-time solution, AIUI.

You take what you can get:-).

I think I'm the one who actually invented this technique, at
least with regards to using typeid for cloning.
In fact, I've seen it used this way the first time in one of your postings.
In practice,
however, I find that this particular bit of programming by
contract is simply not worth the extra work (although I tend to
use programming by contract fairly intensively in my work
otherwise). As Pete has said, failing to provide the clone
function is something that simply doesn't slip past code review
and unit tests.
--
Thomas
Jun 27 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
13
by: matthias_k | last post by:
Hi, I've never thought about this before, but since you are able to overload a function only by changing the const-ness of the formal parameters, some annoying side effects will arise. For...
2
by: Sandy | last post by:
Is there some way to incorporate paging with a treeview, or are there some new controls out there that would be capable of this sort of thing? Can a treeview be put in a datagrid? If so, how?...
11
by: Brent Ritchie | last post by:
Hello all, I have been using C# in my programming class and I have grown quite fond of C# properties. Having a method act like a variable that I can control access to is really something. As...
7
by: quarup | last post by:
I want to specialize a template function that lives inside a class, but am getting a compile error in VS.net 2003. Here's my code: template <class T> class A { public: template <class U> void...
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
23
by: mosesdinakaran | last post by:
Hi All, I need a small clarification in submitting the forms, Ur suggestions please. In a page I have two form and also two submit butons. (ie)
10
by: subramanian100in | last post by:
The following is a beginner's question. Suppose TYPE1 and TYPE2 are two types for which suitable ctors and operator= are defined. Suppose I have class Test { TYPE1 mem1;
2
by: Rahul | last post by:
Hi Everyone, I read in an article that over loading a base class function (ordinary, non-static non-virtual member function) hides the base class versions in the derived class. However, as in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.