Thread: Return pointer from a class method

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    7

    Return pointer from a class method

    Hi, I have a class named 'Myclass' and a a class method 'function1'.
    I want to return a pointer from this method.I am not sure how to do it.
    Code:
    class Myclass
    {
    public:
    int n,m;
    double *function1();
    }
    
    double* Myclass::function1()  // is this a correct way to do it ?
    {
    ............
    }
    Can somebody plz help me ?
    Than you.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by shrinivasapte View Post
    Code:
    class Myclass
    {
    public:
    int n,m;
    double *function1();
    };
    
    double* Myclass::function1()  // is this a correct way to do it ?
    {
    ............
    }
    There is a missing semicolon, but I guess this is only a mistake in the example. All you have done is a correctly declared (and defined!) method. Now just use the 'return' statement to return pointer just like any other value:

    Code:
    double val = 100; // global variable
    double* Myclass::function1()
    {
        return &val;
    }
    Remember that you must not return pointer to a local variable. Whether this program is just an assignment or not, you should get familiar with the raw ptrs issues (SourceForge.net: Raw pointer issues - cpwiki).

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    Thanks, Can you explain me a bit about your sentence " you must not return a pointer to a local variable" ?
    What happens if I do that ?

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by shrinivasapte View Post
    Thanks, Can you explain me a bit about your sentence " you must not return a pointer to a local variable" ?
    What happens if I do that ?
    When the function goes out of scope all local variables are destructed* so your pointer would point to invalid memory.

    *) Question for the experts: Is "destructed" the correct terminology to use if the variable happen to be a pod type? Or would the destruction of a pod variable be called something else?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by _Mike
    *) Question for the experts: Is "destructed" the correct terminology to use if the variable happen to be a pod type? Or would the destruction of a pod variable be called something else?
    I would say "destroyed", not "destructed", for any object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 10-14-2010, 02:08 AM
  2. calling a class method within different class method
    By alyeska in forum C++ Programming
    Replies: 5
    Last Post: 03-08-2009, 10:56 AM
  3. return struct from asp.net web service method
    By Elkvis in forum C# Programming
    Replies: 1
    Last Post: 02-09-2009, 08:39 PM
  4. which one run first, destructor or return method
    By sleith in forum C++ Programming
    Replies: 17
    Last Post: 01-09-2009, 01:27 PM
  5. return value for global getline method
    By danglingelse in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2005, 02:54 PM

Tags for this Thread