This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Covariant return types and order of class definitions


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]