Forward declaration allowed?
Hi,
I always though to return an instance of a class by value, it had to be
defined - i.e. forward declaration isn't good enough?
Consider the following code snippet:
class RGBA;
class Colour
{
public:
virtual RGBA ToRGBA() const = 0; // [1]
};
class RedComponent;
class RGBA : public Colour
{
public:
RGBA( double r, double g, double b, double a = 1.0 );
virtual RGBA ToRGBA() const;
RedComponent GetRed() const;
};
class HSV : public Colour
{
public:
HSV( double h, double s, double v, double a = 1.0 );
virtual RGBA ToRGBA() const;
};
int main()
{
RGBA red( 1.0, 0.0, 0.0 );
HSV papayaWhip( 37.0, 0.16, 1.0 );
RGBA c = papayaWhip.ToRGBA();
c.Red(); // << [2]
return 0;
}
Colour::ToRGBA can be declared to return an RGBA by value even though it is
only forward declared?
I understand the error at [2] in the call to c.Red() - RedComponent is
incomplete. But this is the error I would have expected at [1] - which
compiles cleanly in Visual Studio 2005 and gcc version 4.0.1 (Apple
Computer, Inc. build 5363)
Is it because Colour::ToRGBA is only declared here, and RGBA must be fully
defined at it's definition, but only requires a declaration elsewhere? Or
is this something special to do with pure virtuals?
I was always taught that a forward declaration is good enough for a pointer
or reference use, but a by-value use required the full class's definition?
Thanks for any insights...
--
Regards,
Steve.