5 Replies - 496 Views - Last Post: 11 January 2013 - 03:57 PM Rate Topic: -----

#1 EXCELL3NCE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-January 13

Why would you ever... Return a Pointer or reference..

Posted 11 January 2013 - 12:57 PM

Ok, so I understand pointers and references, ive used them before plenty of times. Thing is that I just doin't understand the concept of returning a pointer or a reference..

Are you trying to return a dynamic allocated object??? To an awaiting pointer that points to allocated stack memory... Thats the only reason I would think is necessary to return a pointer or reference.

But can anybody explain to me other reasons for returning a pointer or reference.

Is This A Good Question/Topic? 0
  • +

Replies To: Why would you ever... Return a Pointer or reference..

#2 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 769
  • Joined: 27-June 09

Re: Why would you ever... Return a Pointer or reference..

Posted 11 January 2013 - 01:35 PM

Say you have a record (such as contact information for a person). If you have a collection of records and you want to find the phone number of a person, the code would look something like this

PersonRecord * myPerson = people.find("EXCELL3NCE");
cout<<myPerson->GetPhoneNum();


"Find" returns a pointer.

This post has been edited by mojo666: 11 January 2013 - 01:37 PM

Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3452
  • View blog
  • Posts: 10,655
  • Joined: 05-May 12

Re: Why would you ever... Return a Pointer or reference..

Posted 11 January 2013 - 01:46 PM

Let me elaborate a bit more in mojo666's example above. The find() could be implemented to return a copy of the entire record. But if the record was 64KB, now you'll have 64KB on the stack for the returned record, and at least another 64KB for whatever data structure is holding the record. Consider than on Linux, a typical stack size is much less than 64KB. On Windows 64KB is the typical size, but your other variables will also be using the stack. Also consider the time it will take to copy 64KB around. So the better implementation is to return a pointer or reference to the block so that instead of returning 64KB, only 4 or 8 bytes is returned.

Without pointers, consider how you would implement a linked list or tree.
Was This Post Helpful? 3
  • +
  • -

#4 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2087
  • Posts: 3,174
  • Joined: 21-June 11

Re: Why would you ever... Return a Pointer or reference..

Posted 11 January 2013 - 01:49 PM

The [] operator of container classes returns a reference, so that you can write things like my_collection[x] = y;. Generally whenever you want to be able to assign to the result of a function call, the function needs to return a reference.

Another reason why you'd want to return a reference would be to avoid creating copies. If you return one of your parameters (assuming they're references too) or something else which you know will live longer than your function (i.e. something that's not a local variable), it can make sense to return by (const) reference for that reason.

View PostSkydiver, on 11 January 2013 - 09:46 PM, said:

So the better implementation is to return a pointer or reference to the block so that instead of returning 64KB, only 4 or 8 bytes is returned.


Perhaps more importantly a find method that returned by value would be useless if you intend on modifying the record you found.
Was This Post Helpful? 1
  • +
  • -

#5 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Why would you ever... Return a Pointer or reference..

Posted 11 January 2013 - 02:03 PM

Don't forget to consider passing as reference instead, and making it const if it makes sense. This way you make your pointer usage more reliable/secure.

As void foo(const BigData& data); //Can't change the reference, can't be null
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Why would you ever... Return a Pointer or reference..

Posted 11 January 2013 - 03:57 PM

So I find this to be a little contradictory:

Quote

Ok, so I understand pointers and references, ive used them before plenty of times. Thing is that I just doin't understand the concept of returning a pointer or a reference.


if you understand pointers/references they surely you understand the need to return them from functions! They are forms of variables and you will need to manipulate them and that generally means passing them into and out-of functions.

I personally love to return references from function, ESPECIALLY member functions that return a reference to *this!

Take for example this dumb little class:
#include <iostream>

using namespace std;

class AverageFinder {
    int accumulator;
    int count;
    
    public:
    AverageFinder() : accumulator(0), count(0) { }
    
    AverageFinder& addDataPoint(const int& data) {
        accumulator += data;
        count++;
        return *this; //return a reference to the current instance
    }
    
    double getAverage() {
        return (double)accumulator/count;
    }
};

int main() {
    AverageFinder avg;
    avg.addDataPoint(1);
    avg.addDataPoint(2).addDataPoint(3).addDataPoint(4);
    cout << avg.getAverage() << endl;
    return 0;
}


By returning a reference to *this from addDataPoint() I can chain them together. I know what you are thinking, "so... that is neat I guess but... so?"

Well what if instead of a function we did that with an operator? (which is really just a function).

#include <iostream>

using namespace std;

class AverageFinder {
    int accumulator;
    int count;
    
    public:
    AverageFinder() : accumulator(0), count(0) { }
    
    AverageFinder& addDataPoint(const int& data) {
        accumulator += data;
        count++;
        return *this; //return a reference to the current instance
    }
    
    double getAverage() {
        return (double)accumulator/count;
    }
};

AverageFinder& operator<<(AverageFinder& avg, const int& data) {
    return avg.addDataPoint(data);
}

int main() {
    AverageFinder avg;
    avg.addDataPoint(1);
    avg.addDataPoint(2).addDataPoint(3).addDataPoint(4);
    
    avg << 5 << 6 << 7 << 8;
    
    cout << avg.getAverage() << endl;
    return 0;
}


but wait there's more! This is how we overload operator<< for output streams too!
#include <iostream>

using namespace std;

class AverageFinder {
    int accumulator;
    int count;
    
    public:
    AverageFinder() : accumulator(0), count(0) { }
    
    AverageFinder& addDataPoint(const int& data) {
        accumulator += data;
        count++;
        return *this; //return a reference to the current instance
    }
    
    double getAverage() {
        if (count == 0) { count = 1; }
        return (double)accumulator/count;
    }
};

AverageFinder& operator<<(AverageFinder& avg, const int& data) {
    return avg.addDataPoint(data);
}

ostream& operator<<(ostream& out, AverageFinder& avg) {
    return (out << avg.getAverage());
}

int main() {
    AverageFinder avg;
    avg << 1 << 2 << 3;
    cout << avg << endl;
    return 0;
}


--but you see the point here is not tricks you can play with syntax, the point is that pointers and references are variables and manipulating the data in variables is what programming is really kind of all about. So IF you understand pointers and references then returning them from functions is just something you find yourself doing.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1