returning this pointer

This is a discussion on returning this pointer within the C++ Programming forums, part of the General Programming Boards category; Hello, i am confused whether in the following code i should return reference or not! Code: #include <iostream.h> class set ...

  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
    Katy, Texas
    Posts
    2,309
    Seems to work fine as it is.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,303
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 08:39 AM
  2. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02:34 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  4. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 06:40 PM
  5. returning pointer to string between exe and dll
    By evilpope in forum C Programming
    Replies: 2
    Last Post: 05-15-2003, 01:16 PM

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