hello all

i want to know the uses of function returning by refrence and how to use them

i tried this but it gave an Error

int& sumz(int x,int y)
{
    int *sum = new int;
    *sum = x + y;
    return sum;
}

Dani AI

Generated

Returning a function result by reference lets the call be used as an lvalue (assignment and modification). It is useful when the function needs to expose an existing object owned by the callee (for example, a container element or an internal member) or when returning a large object where copying would be expensive — but only when the returned object outlives the call.

Common pitfalls from the thread: attempted to return a reference to memory created inside the function, and corrected the return-type issue. As warned, allocating with new inside the function and handing back a raw reference or pointer hides ownership and leaks or double-free risks. Also never return a reference to a local (automatic) variable — that yields a dangling reference and undefined behavior.

Safer patterns (examples):

Return a reference to a container/member when the storage clearly belongs to the object:

class MyVec {
    std::vector<int> data;
public:
    int& operator[](size_t i) { return data[i]; }
    const int& operator[](size_t i) const { return data[i]; }
};

Transfer ownership instead of returning a raw reference when the callee creates the object:

std::unique_ptr<int> make_sum(int a, int b) {
    return std::make_unique<int>(a + b); // caller owns the result
}

Quick rules of thumb:

  • For small built-ins (int, double): return by value — cheap and safe.
  • To allow modification of an existing object: return a non-const reference.
  • To avoid copies for large objects but not allow modification: return a const reference — only if the referent definitely outlives the caller.
  • If the function creates an object and ownership should transfer: return a smart pointer or return by value (RVO/move is often optimal).
  • Beware container reallocation/invalidation: references into a vector can be invalidated by operations like push_back.

Further reading: Lifetime of objects and std::unique_ptr.

Recommended Answers

All 3 Replies

int& sumz(int x,int y)
{
    int *sum = new int;
    *sum = x + y;
    return *sum;
}

thanks .....:D:D

but i also want to know when to use it and what is it's uses

thanks .....:D:D

but i also want to know when to use it and what is it's uses

A function that returns a valid reference can be treated as a lvalue.

int& sumz(int x,int y)
{
    int *sum = new int;
    *sum = x + y;
    return *sum;
}
int x = 2, y = 3;

sumz(x, y) = 5; // valid because it is a reference

But your example is not a good place to use this since each time your method runs, you will declare a new location in memory that can hold an int and you can't retrieve that address unless you first store it.

#include <cstdlib>
#include <iostream>

using namespace std;

int& sumz(int x,int y)
{
    int *sum = new int;
    *sum = x + y;
    return *sum;
}

int main()
{
    
    int x = 2, y = 3, *z = NULL;
    
    z = &sumz(x, y); // 2 + 3 = 5, z points to new sum declared/defined in sumz(int, int) method
    
    (*z) += 5; // ( ((*sum) or (2 + 3) because z points to same address )    + 5 )
    
    cout << (*z) << endl; // printing the result

    delete z; // free the dynamic memory returned by the method (pointed to by z)
    
    cin.get();
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.