public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Covariant return types and order of class definitions
@ 2003-12-18 21:52 Ben Davis
  2003-12-18 23:02 ` Ben Davis
  0 siblings, 1 reply; 2+ messages in thread
From: Ben Davis @ 2003-12-18 21:52 UTC (permalink / raw)
  To: gcc-help

Hi,

I am working on a project that has the following structure. (Feel free to skip 
this and look at the code, but the explanation may help clarify how the code 
structure works.) DSPModule is an abstract class representing any stream of 
audio (PCM, sampled waveform) data that can be generated. When the user 
wishes to generate the data, she should create a subclass of DSPModulePlayer 
(also abstract) and request the data from that.

(Side-note: DSP stands for Digital Signal Processing.)

Some DSPModules stand alone; a Sample (containing prerecorded or pregenerated 
audio) is an example of this. But most DSPModules act as transformations that 
can be applied to other DSPModules which I call the 'subjects'. An example is 
a VolumeEnvelope, which applies a volume envelope - a graph of volume 
(amplitude) against time - to its subject's audio data. A VolumeEnvelope 
therefore contains a 'DSPModule *subject' member.

A VolumeEnvelope can spawn a VolumeEnvelopePlayer, but in order to do so it 
needs to spawn a player from its subject. This player will be a subclass of 
DSPModulePlayer, and it can be obtained with DSPModule's virtual getPlayer() 
method. This method is a clear candidate for covariant return types.

Here is the code, vastly simplified:


/* dsp.h */

class DSPModule;
class DSPModulePlayer;

class DSPModule {
    virtual DSPModulePlayer *getPlayer();
};

class DSPModulePlayer {
    ...
};


/* volenv.h */

#include "dsp.h"

class VolumeEnvelope;
class VolumeEnvelopePlayer;

class VolumeEnvelope : public DSPModule {
    virtual VolumeEnvelopePlayer *getPlayer();     /* foo */
};

class VolumeEnvelopePlayer : public DSPModulePlayer {
    ...
};


This does not work. When the compiler reaches the line marked 'foo', it knows 
that a class called VolumeEnvelopePlayer exists, but it does not know that 
the class is a subclass of DSPModulePlayer. Hence it generates an error about 
conflicting return types.

I can fix this problem by putting the whole VolumeEnvelopePlayer definition 
above the VolumeEnvelope definition, but I don't want to do this - it seems 
much more logical to me to define VolumeEnvelope first. I will do this as a 
last resort.

What I was hoping to do was something like this:

   class VolumeEnvelopePlayer : public DSPModulePlayer;

But with GCC 3.3.1, this doesn't compile: parse error before ';'. However, a 
friend says he was able to compile such code with GCC 3.2.2.

So is it possible to declare a class, and specify its inheritance information, 
without defining the whole body of the class? Is it correct that the above 
line doesn't compile, or is it a bug in GCC 3.3.1?

Thanks in advance,

Ben

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Covariant return types and order of class definitions
  2003-12-18 21:52 Covariant return types and order of class definitions Ben Davis
@ 2003-12-18 23:02 ` Ben Davis
  0 siblings, 0 replies; 2+ messages in thread
From: Ben Davis @ 2003-12-18 23:02 UTC (permalink / raw)
  To: gcc-help

Hi,

I have found an answer to my question. The C++ ISO standard says:

"If the return type of D::f differs from the return type of B::f, the class 
type in the return type of D::f shall be complete at the point of declaration 
of D::f or shall be the class type D."

So I have to put the whole of VolumePlayerEnvelope's definition first. I guess 
this renders a forward declaration with inheritance information somewhat 
useless. :)

Thanks anyway,

Ben

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2003-12-18 23:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-18 21:52 Covariant return types and order of class definitions Ben Davis
2003-12-18 23:02 ` Ben Davis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).