473,324 Members | 2,179 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,324 software developers and data experts.

Pure Abstract Classes

I was wondering what people thought about the information found at:

http://g.oswego.edu/dl/mood/C++AsIDL.html

Specifically, I am interested in the following recommendation:

----
Since interface classes cannot be directly instantiated, yet serve as
virtual base classes for implementations, the constructors should take
no arguments and should be listed as protected. Also, for similar
reasons, abstract classes should have a no-op virtual destructor, not
one listed as ... = 0.
----

Not being familiar with Doug Lea, I am not certain whether his
recommendations should be treated a genuine Perls of Wisdom or not.

Is this really the right way to do things? Could it lead to unexpected
bugs? If so, what should be payed attention to to avoid these bugs?

The reason I bring this up is that I am working with a pure abstract
class which really should be one. It seems kinda messy to have to
declare non-abstract things in it, destroying the purity of the class,
but doing so would get rid of an annoying compiler warning...and I
definitely like and prefer warning free compiles as it makes it easy to
spot warnings that point to real problems.

Dec 6 '06 #1
4 1831
Eric wrote:
I was wondering what people thought about the information found at:

http://g.oswego.edu/dl/mood/C++AsIDL.html

Specifically, I am interested in the following recommendation:

----
Since interface classes cannot be directly instantiated, yet serve as
virtual base classes for implementations, the constructors should take
no arguments and should be listed as protected. Also, for similar
reasons, abstract classes should have a no-op virtual destructor, not
one listed as ... = 0.
----

Not being familiar with Doug Lea, I am not certain whether his
recommendations should be treated a genuine Perls of Wisdom or not.

Is this really the right way to do things? Could it lead to unexpected
bugs? If so, what should be payed attention to to avoid these bugs?

The reason I bring this up is that I am working with a pure abstract
class which really should be one. It seems kinda messy to have to
declare non-abstract things in it, destroying the purity of the class,
but doing so would get rid of an annoying compiler warning...and I
definitely like and prefer warning free compiles as it makes it easy
to spot warnings that point to real problems.
Sounds close to what I consider "good C++". No unexpected bugs AFAICS.

"The right way to do things" depends on what your design goals are. If
it's "C++ As IDL", sure, it's the right way. In our application there
are almost no pure "interfaces". Even abstract classes can have data
(state) and may need initialisation other than default.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 6 '06 #2
On 2006-12-06 21:45, Eric wrote:
I was wondering what people thought about the information found at:

http://g.oswego.edu/dl/mood/C++AsIDL.html

Specifically, I am interested in the following recommendation:

----
Since interface classes cannot be directly instantiated, yet serve as
virtual base classes for implementations, the constructors should take
no arguments and should be listed as protected. Also, for similar
reasons, abstract classes should have a no-op virtual destructor, not
one listed as ... = 0.
----
The thing about the destructor is true, it's also in the FAQ [1]. It's
not really clear from the quote whether he thinks that the constructor
should be pure virtual or not, looking at the article it seems not.
Personally I can't see any reason to declare a constructor except to
prevent people from trying to instantiate the class. But since it's
abstract that's not possible anyway.

1. http://www.parashift.com/c++-faq-lit....html#faq-20.7

--
Erik Wikström
Dec 6 '06 #3
* Eric:
I was wondering what people thought about the information found at:

http://g.oswego.edu/dl/mood/C++AsIDL.html

Specifically, I am interested in the following recommendation:

----
Since interface classes cannot be directly instantiated, yet serve as
virtual base classes for implementations, the constructors should take
no arguments and should be listed as protected. Also, for similar
reasons, abstract classes should have a no-op virtual destructor, not
one listed as ... = 0.
----

Not being familiar with Doug Lea, I am not certain whether his
recommendations should be treated a genuine Perls of Wisdom or not.

Is this really the right way to do things? Could it lead to unexpected
bugs? If so, what should be payed attention to to avoid these bugs?

The reason I bring this up is that I am working with a pure abstract
class which really should be one. It seems kinda messy to have to
declare non-abstract things in it, destroying the purity of the class,
but doing so would get rid of an annoying compiler warning...and I
definitely like and prefer warning free compiles as it makes it easy to
spot warnings that point to real problems.
First, although the ideal in C++ is that interface classes serve as
virtual base classes, that ideal is often compromised due to reasons
such as efficiency or having to work with an existing framework, and
sticking to the ideal would mean not creating that program.

Second, there's nothing inherently wrong about a pure virtual
destructor, and it's a not uncommon convention to "document" a class as
abstract by having a pure virtual destructor at the top of the class'
code. But if a destructor is declared, it should also be implemented.
Including that a pure virtual destructor should always be implemented
(the implementation will be called non-virtually). A good linker will
ensure that. But unfortunately you can't count on that.

Third, the notion that an abstract class should have a no-op destructor
is IMO an academic purist view of abstract classes where they don't
manage resources. IMO that's just silly, and not very practical. The
early Smalltalk notion of an abstract class, a class with one or more
methods as "subclass responsibility", is IMO much better suited to
actual programming, but the subset of abstract classes that don't
enforce class invariants directly or leave all resource allocation and
deallocation to derived classes and only provide ways of checking the
invariant, are very important -- e.g., they include interface classes.

Having said that, the advice quoted above (I didn't follow the link) is
good, but as general advice, not as an absolute guideline.

As always, it's better to do what you think best -- because you
understand that, and can learn from it and fix it if it turns out to be
less than ideal -- than to blindly follow advice you don't understand.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 6 '06 #4

"Eric" <er******@gmail.comwrote in message
news:11*********************@73g2000cwn.googlegrou ps.com...
>I was wondering what people thought about the information found at:

http://g.oswego.edu/dl/mood/C++AsIDL.html

Specifically, I am interested in the following recommendation:

----
Since interface classes cannot be directly instantiated, yet serve as
virtual base classes for implementations, the constructors should take
no arguments and should be listed as protected. Also, for similar
reasons, abstract classes should have a no-op virtual destructor, not
one listed as ... = 0.
----

Not being familiar with Doug Lea, I am not certain whether his
recommendations should be treated a genuine Perls of Wisdom or not.

Is this really the right way to do things? Could it lead to unexpected
bugs? If so, what should be payed attention to to avoid these bugs?

The reason I bring this up is that I am working with a pure abstract
class which really should be one. It seems kinda messy to have to
declare non-abstract things in it, destroying the purity of the class,
but doing so would get rid of an annoying compiler warning...and I
definitely like and prefer warning free compiles as it makes it easy to
spot warnings that point to real problems.
In spite of my great respect for some of the other responders who have a
different view, it is my opinion that "pure abstract classes" should NEVER
have any state to initialize and therefore should never have constructors at
all. I will defend this position by pointing out that if you want a starting
point which has a state, you can always derive one from the pure abstract
class and derive the rest of your classes from there.

Otherwise it ain't pure. Just my opinion.

The no-op virtual destructor is a good idea. If any of the derived classes
have non-trivial destructors it may prevent a hard-to-find resource leak.

Cy
Dec 6 '06 #5

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

Similar topics

51
by: Noam Raphael | last post by:
Hello, I thought about a new Python feature. Please tell me what you think about it. Say you want to write a base class with some unimplemented methods, that subclasses must implement (or...
9
by: | last post by:
I don't understand. What's the difference in practice between 2 next lines? When I *must* use first line and when second? ------------- virtual int a(int b) = 0; virtual int a(int b);...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
3
by: Thomas Kowalski | last post by:
Hi everyone, following situation: class A { void m(T value) =0; } class B : A {
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
7
by: sam_cit | last post by:
Hi Everyone, I wanted to know as to what is the exact difference between a virtual function and a pure virtual function? Thanks in advance!!!
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
4
emibt08
by: emibt08 | last post by:
Hi. I know the title looks a little bit silly and that we can not have pure virtual static functions. But i've been wondering what approach to take to make the abstraction. This is the case: I have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.