Thread View: comp.std.c++
2 messages
2 total messages
Started by bwmott
Tue, 06 Feb 1996 22:20
Forward declaration of a sub-class?
Author: bwmott
Date: Tue, 06 Feb 1996 22:20
Date: Tue, 06 Feb 1996 22:20
63 lines
2185 bytes
2185 bytes
I posted this to comp.std.c++, but I guess I was supposed to mail it
so I'm mailing it now :-)
[ Posting should work. If it doesn't, contact your site administrator
to find out why. -mod ]
--------------------------------------------------------------------------
I've got a question about forward declaring a class as a sub-class of
another class. Is it possible under the C++ draft standard to do
something like:
class S;
class T : public S;
The reason I need such a thing is because of the restrictions placed on
redefining the return type of a virtual function.
A simplified version of my class layout is:
class S;
class T; // This should be something to tell the compiler that
// class T exists and that it's a subclass of S.
class A {
public:
virtual S& belong() = 0;
};
class B : public A {
public:
virtual T& belong(); // This doesn't work but it seems
// that it should be possible!
private:
T& myT;
};
class S {
public:
virtual A& get() = 0;
};
class T : public S {
public:
virtual B& get(); // This works fine
private:
B& myB;
};
I guess a possible solution would be for the redefinition test to be done
on the definition instead of on the declaration.
Is there any hope?
Thanks,
Bradford Mott
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy is
summarized in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
]
Re: Forward declaration of a sub-class?
Author: Steve Clamage
Date: Wed, 07 Feb 1996 01:50
Date: Wed, 07 Feb 1996 01:50
28 lines
1746 bytes
1746 bytes
In article RAA08320@pythagoras.csc.ncsu.edu, bwmott@unity.ncsu.edu writes: > >I've got a question about forward declaring a class as a sub-class of >another class. Is it possible under the C++ draft standard to do >something like: > > class S; > class T : public S; No, for several reasons. You cannot mention class internals until the definition of the class has been seen. You could otherwise in principle add things to a class without modifying the class definition. (Some languages allow you to do so, but C++ does not.) Knowing only that T is derived from S is not any more helpful than knowing only that T is a class. For example, casting a T* to an S* or the reverse in general requires the full class definitions. Your example involved covariant return types. The compiler can't generate the appropriate pointer adjustments without seeing the class definitions. --- Steve Clamage, stephen.clamage@eng.sun.com --- [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu. Contact address: std-c++-request@ncar.ucar.edu. Moderation policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads