Thread: reference and pointer

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    reference and pointer

    Okay I've got to admit first that pointers and references are not my strong point. Sometimes I can imagine and understand how things go, sometimes I don't. This is the simplified version of my problem:

    Code:
    class TestObj
    {
    	public:
    		TestObj()
    		{
    			mA = 1;
    		}
    
    		~TestObj()
    		{
    		}
    
    		int getA()
    		{
    			return mA;
    		}
    
    	private:
    		int mA;
    };
    
    class TestPointer
    {
    	public:
    		TestPointer()
    		{
    		}
    
    		~TestPointer()
    		{
    		}
    
    		TestObj* get()
    		{
    			return mXPointer;
    		}
    
    		void set(const TestObj* pointer)
    		{
    			mXPointer = pointer;
    		}
    	private:
    		TestObj* mXPointer;
    };
    
    void testFunc(TestObj *& obj)
    {
    	obj = new TestObj();
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      int n;
      char str[10];
    
      TestPointer testP;
    
      testFunc(testP.get()); //Error    1    error C2664: 'testFunc' : cannot convert parameter 1 from 'TestObj *' to 'TestObj *&'
    
    
      return 0;
    
    }
    Well, I know that I can solve the problem like below.
    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
      int n;
      char str[10];
    
      TestPointer testP;
      TestObj *obj;
    
      testFunc(obj);
      testP.set(obj);
    
      return 0;
    
    }
    But isn't it the same thing actually? Or is it? I'm sorry my logic is kind of messed up right now. So would you mind to elaborate why it was faulty? Thanks.

    Oh yeah BTW, the testFunc itself is not actually an instantiation of the TestObj like above. It's just to simplify the actual problem. So no need to bash that function and say that it's not right according to RAII or such.

    Thanks in advance.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The problem is the .get() returns a temporary and testFunc wants a non-const reference.
    This is illegal. You must either:
    - Accept a const reference,
    - Accept an rvalue reference,
    - Make .get() return a reference to the pointer.
    Think about it. What good will it do to change a temporary?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Thank you. I happen to come to the same conclusion myself today, when my head is a lot clearer. :P
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Pointer and reference
    By audinue in forum C++ Programming
    Replies: 12
    Last Post: 08-15-2009, 05:42 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. C++ pointer vs reference
    By C-Dumbie in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2002, 04:08 AM