i would like to achieve this:
but i know of no way to do this. is it possible?Code:class Row { public: static Vector transpose(const Row& r); }; class Vector { public: static Row transpose(const Vector& v); };
i would like to achieve this:
but i know of no way to do this. is it possible?Code:class Row { public: static Vector transpose(const Row& r); }; class Vector { public: static Row transpose(const Vector& v); };
Where exactly are you experiencing an issue? If it's just with getting the compiler to process it, you could use a forward declaration of Vector above Row - although I think you'll then have to return a pointer/reference instead of a fully-fledged object. Alternatively, declare them as functions outside of (and below) the class definitions and you can have them both return objects.
On another note - why have you declared both transpose() methods to be static? If they both act on a single instance of their own class, it would make sense to make them both instance methods... no?
Yes. Recall that you do not have to define member functions inline in the class definition.Originally Posted by m37h0d
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
right but if i want to return by value, the full type definition has to be available, correct? and it becomes a 'chicken-and-egg' problem where only 1 can have the definition of the other - that's the thrust of what i'm asking about.
if i return by an indirect type, then i have to return dynamically allocated memory.
The "full type definition" does not need to include the definition of member functions. For example:Originally Posted by m37h0d
Code:class Vector; class Row { public: static Vector transpose(const Row& r); }; class Vector { public: static Row transpose(const Vector& v); }; Vector Row::transpose(const Row& r) { return Vector(); } Row Vector::transpose(const Vector& v) { return Row(); } int main() { Vector v; Row r(Vector::transpose(v)); v = Row::transpose(r); }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yes, argument and return types may be incomplete in function declarations. For example, within a class definition the class itself is incomplete (but it is complete in methods defined inline):
Code:template <unsigned> struct X {}; struct Y { void foo(X<sizeof(Y)>); //error, Y is still incomplete Y bar(Y); //OK, argument or return types may be incomplete };
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.