Like Tree1Likes
  • 1 Post By laserlight

Return pointer from a class method

This is a discussion on Return pointer from a class method within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a class named 'Myclass' and a a class method 'function1'. I want to return a pointer from ...

  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
    684
    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).
    I never put signature, but I decided to make an exception.

  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
    23,773
    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.
    _Mike likes this.
    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, 07:39 PM
  4. which one run first, destructor or return method
    By sleith in forum C++ Programming
    Replies: 17
    Last Post: 01-09-2009, 12:27 PM
  5. return value for global getline method
    By danglingelse in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2005, 01:54 PM

Tags for this Thread


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