Objects as arguments and as return values

This is a discussion on Objects as arguments and as return values within the C++ Programming forums, part of the General Programming Boards category; I have a question about how the arguments are copied to a function and how the return value is copied ...

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Objects as arguments and as return values

    I have a question about how the arguments are copied to a function and how the return value is copied out of the function:

    Say that you have two classes, A and B, and a function func:

    Code:
    B func(A arg) {
        B ret;
        return ret:
    }
    and som more code

    Code:
    A in;
    func(A).print(); //Suppose B contains a print funtion
    Now, when in is sent to func, it is copied to arg, but how is it copied? Is it done like A arg; arg = in; or like A arg(in);? Or in some other way? And what object is operated on when the print function is called, and how is that object created; is it a copy of ret or is it the ret object itself (before it is destroyed)?
    Come on, you can do it! b( ~_')

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The arg object is I believe copy-constructed. In main, a temporary B is created which gets the return value from func (again copy-constructed IIRC) and that object is used to call print. (Since it's a temporary object, print must be marked const.)

    EDIT: And of course, you don't have to believe me; create a copy constructor and an operator = that print something to the screen, run the code, and see what happens. (Same for destructor of B.)
    Last edited by tabstop; 09-26-2009 at 05:26 PM.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    The copy constructors are called, not the assignment operators.
    And the returned B is a temporary (copied from B) since you do not return a reference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The copy constructor won't be necessarily called because the compiler is allowed to optimize constructor calls away. The copy constructor must be accessible, though.

    Code:
    #include <iostream>
    
    class B
    {
    public:
        B() { std::cout << "B()\n"; }
        ~B() { std::cout << "~B()\n"; }
        void print() { std::cout << "B::print()\n"; }
    //private:
        B(const B&) { std::cout << "B(const B&)\n"; }
    
    };
    
    B foo()
    {
        B b;
        return b;
    }
    
    int main()
    {
        foo().print();
    }
    Perfectly good output:
    B()
    B::print()
    ~B()
    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).

  5. #5
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Good idéa to create a test class just to see what happens, I will try that myself next time. Thank you

    -Kristofer
    Last edited by TriKri; 09-27-2009 at 04:21 AM.
    Come on, you can do it! b( ~_')

Popular pages Recent additions subscribe to a feed

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