Thread: returning this pointer

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    returning this pointer

    Hello,
    i am confused whether in the following code i should return reference or not!

    Code:
    #include <iostream.h>
    class set
    {
      int data;
      public:
    	void getdata(int d)
    	{
    		data=d;
    	}
    	void putdata()
    	{
    		cout<<data;
    	}
    	set max(set B);
    };
    set set:: max (set B)
    {
    	if(data>B.data)
    		return *this;
    	else
    		return B;
    }
    
    
    int main(void)
    {
     set a,b;
     a.getdata(30);
     b.getdata(20);
     b=a.max(b);
     b.putdata();
     return 0;
    }
    Is the above code correct??if Yes then B is call by value and when we return B the address of B will be changed even though we return the same object B .Right or not?

    Please tell me whether i should return reference or the above code is correct?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Seems to work fine as it is.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think it is functionally correct (but with a pre-standard header included), though it is not const correct and could be improved with pass by reference and better naming, e.g.,
    Code:
    #include <iostream>
    
    class set
    {
    public:
        void setdata(int d)
        {
            data = d;
        }
        
        int getdata() const
        {
            return data;
        }
    
        const set& max(const set& B) const;
    private:
        int data;
    };
    
    const set& set::max(const set& B) const
    {
        return (B.data < data) ? *this : B;
    }
    
    int main()
    {
        set a, b;
        a.setdata(30);
        b.setdata(20);
        b = a.max(b);
        std::cout << b.getdata();
        return 0;
    }
    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

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok laser
    But if i dont use reference & then is it correct way of doing it?I mean this is much more efficient but still is that way also correct?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But if i dont use reference & then is it correct way of doing it?I mean this is much more efficient but still is that way also correct?
    Yes.
    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: 2
    Last Post: 07-11-2008, 07:39 AM
  2. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM
  5. returning pointer to string between exe and dll
    By evilpope in forum C Programming
    Replies: 2
    Last Post: 05-15-2003, 12:16 PM