Thread: two classes with methods that return each other by value

  1. #1
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838

    two classes with methods that return each other by value

    i would like to achieve this:
    Code:
    class Row
    {
    public:
        static Vector transpose(const Row& r);
    };
    class Vector
    {
    public:
        static Row transpose(const Vector& v);
    };
    but i know of no way to do this. is it possible?

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    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?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by m37h0d
    but i know of no way to do this. is it possible?
    Yes. Recall that you do not have to define member functions inline in the class definition.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by laserlight View Post
    Yes. Recall that you do not have to define member functions inline in the class definition.
    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.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by m37h0d
    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.
    The "full type definition" does not need to include the definition of member functions. For example:
    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);
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    thanks anon and laserlight. this misconception had been kicking around for years, and it was only now that i had need to dispel it.

    Quote Originally Posted by JohnGraham View Post
    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?
    they are equivalent, i personally think the static function is cleaner syntactically, but it's just a matter of taste.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thoughts on return type from template container classes.
    By Subsonics in forum C++ Programming
    Replies: 17
    Last Post: 10-17-2010, 03:10 AM
  2. Can you inherit Methods into other methods?
    By bobbelPoP in forum C++ Programming
    Replies: 5
    Last Post: 08-02-2008, 08:32 AM
  3. call methods across classes
    By vb.bajpai in forum C++ Programming
    Replies: 11
    Last Post: 09-12-2007, 01:31 PM
  4. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  5. Replies: 8
    Last Post: 07-27-2003, 01:52 PM