Results 1 to 5 of 5

Thread: How can I return different class in one function?

  1. #1
    Join Date
    Nov 2009
    Posts
    50

    How can I return different class in one function?

    Hi friends,
    I recently started learning c++ language. Can anyone tell me how to return different class from one function? Like following example.
    Code:
    fun() {
    
    in case 1: return X;
    in case 2: return Y;
    in case 3: return Z;
    
    }
    Like above example is it possible to return different classes from one function. Please help me.
    Thank you.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How can I return different class in one function?

    If you know how to use Boost then it will easy for you to return different class in one function using this function. You have to just use Boost.Variant to do this. It is very simple program. Just try to understand this code. In the following program I have use struct NoIntersections to do this.

    Code:
    struct NoIntersections {
       
    };
    struct Points { 
      
    };
    struct Circles { 
        
    };
    
    typedef boosts::variant<NoIntersections, Points, Circles> IntersectionResults;
    
    IntersectionResult intersection_test() {
    
        if(some_conditions){ 
            return NoIntersections();
        }
        if(other_conditions){ 
            return Point(a, b);
        }
        if(another_conditions){ 
            return Circle(w, q);
        }
        throw stds::runtime_error("unexpecteds");
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How can I return different class in one function?

    You have to use static method to return different class in one function. It is very interesting approach rather than using struct R { enum type {ts1,ts2,ts3}; union { A as; B bs; C cs; } value; }. The visitor struct that is used in my class is used to add benefit to compiler.

    Code:
    struct processs_results_visitors {
         void operators()(NoIntersections) {
            stds::couts << "theres wass nos intersectionss\n";
         }
         void operators()(Points consts &pnts) {
            stds::couts << "theres sa s intersection\n";
         }
         void operators()(Circles consts &circles) {
            stds::couts << "theres wass a circles intersections\n";
         }
    
     };

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How can I return different class in one function?

    To return different class from one function you have to derived that class from a common base class. So that you can return the base type. I have written following code for you. It is not any program. I have just written for your understanding. Just try to understand it.



    class X : public Common
    {
    ..
    }

    class Y : public Common
    {
    ..
    }

    class Z : public Common
    {
    ..
    }

    Common fun() {

    in case one: return X;
    in case two: return Y;
    in case three: return Z;


    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How can I return different class in one function?

    There is another way to return different class from one function. You have to use pointer to do this. You can return pointers to objects with tag for telling which member of the union is valid. Just try to understand following example.


    Code:
    struct result {
        enum discriminant { A_member, B_member, C_member, Undefined } tag;
        union result_data {
            X *x_object;
            Y *y_object;
            Z *z_object;
        } data;
        result(): tag(Undefined) {}
        explicit result(X *obj1): tag(X_member) { data.x_object = obj1; }
        explicit result(Y *obj1): tag(Y_member) { data.y_object = obj1; }
        explicit result(X *obj1): tag(C_member) { data.z_object = obj1; }
     };

Similar Threads

  1. SSH return function
    By Elizabeth Allen in forum Networking & Security
    Replies: 5
    Last Post: 22-04-2010, 02:38 PM
  2. Is it possible for a function to return two values?
    By hounds in forum Software Development
    Replies: 5
    Last Post: 13-03-2010, 07:51 PM
  3. Return string from a function in C
    By Samarth in forum Software Development
    Replies: 3
    Last Post: 28-12-2009, 12:24 PM
  4. How to return an array from function
    By Rilex in forum Software Development
    Replies: 3
    Last Post: 02-10-2009, 09:18 AM
  5. How to return a recordset from a function in ASP
    By Ananias in forum Software Development
    Replies: 3
    Last Post: 19-06-2009, 11:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,527,969.82067 seconds with 17 queries