returning a class

This is a discussion on returning a class within the C++ Programming forums, part of the General Programming Boards category; hi I want to return a class in a function but i don't want really to declare it. I want ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    76

    returning a class

    hi
    I want to return a class in a function but i don't want really to declare it. I want to do something like:
    Code:
    #include <cstdio>
    
    class wynik
    {
    public:
            int     a,
                    b;
            wynik(int _a =0, int _b =0)
            {
                    a=_a;
                    b=_b;
            }
    };
    
    wynik   f(int i, int y)
    {
            return (i, y);
    }
    
    int main()
    {
            wynik lala;
            lala=f(12,14);
            printf("%d %d\n", lala.a, lala.b);
            return 0;
    }
    but on the output i don't see 12 14 but 14 0. Can anyone tell me if something like that is possible or i have to code f() like:
    Code:
    wynik   f(int i, int y)
    {
            wynik a(i,y);
            return a;
    }
    Regards.
    apacz

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Your first method will not work, you must use something like the second method.

    Code:
    Foo f(int x, int y)
    {
      return Foo(x, y);
    }
    That's how I would write it.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    22
    I'm sure you'll probably get the same amount of replies here as you would on any other C board, but, if I am correct, you might want to post this on the board for C, this is a board for C++ and most people who know about C will be hanging out at the other board, good luck!

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >if I am correct, you might want to post this on the board for C, this is a board for C++
    class == C++

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Exclamation A function can only return ONE value.

    You can use pointers or references to change multiple variables inside a function (without returning anything).

    Or, the value returned by a function can be pointer. It can be a simple pointer to a variable, or a pointer to an array, a pointer to a structure, or a pointer to an object, etc.
    Last edited by DougDbug; 06-30-2005 at 04:10 PM.

  6. #6
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    you have to create a copy constructor. You should also provide a operator=

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    With that class you don't have to create a copy constructor or copy assignment operator=, the ints will be copied correctly automatically.

  8. #8
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    yes, but good practices pay up. If he had a ponter to allocated memory, that implementation would be troublesome....

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    IMHO, good practice is to only implement a copy constructor, copy assignment operator, and destructor when it is necessary, and leave comments that you are using the default versions if it is not obvious. Once you've written them, you must remember to maintain them every time you change the implementation of the class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21