Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   this pointer problem (http://www.go4expert.com/forums/this-pointer-problem-t2249/)

Frank Reich 15Dec2006 08:06

this pointer problem
 
hello.
i am having a problem with the this-pointer in the code below. for the
command: "MyFunctB(this)"; i am getting the following error:
error C2664: 'cTest::MyFunctB' : cannot convert parameter 1 from 'cTest *const ' to 'cTest'

whats the problem?
thanks.
V.K.

Code:

class cTest
{
public:
        cTest()
        {
                for (int i=0;i<3;i++)
                        dat[i] = rand()%43;
        }
        void MyFunctA ()
        {
                MyFunctB(this);
        }
        void MyFunctB (cTest a)
        {
        }
private:
  int dat[3];

};

int main()
{
        int i=0;

        srand(time(0));
        cTest A;

        A.MyFunctA();

        _getch();
    return 0;
}


DaWei 15Dec2006 09:12

Re: this pointer problem
 
Code:

        void MyFunctA ()
        {
                MyFunctB(this);      'this' is a pointer to a cTest
        }
        void MyFunctB (cTest a)  'a' is an actual cTest object
        {
        }

Types have to match or be convertible. Why would you pass the 'this' pointer? It's automatically passed implicitly. Now you just have two copies.


All times are GMT +5.5. The time now is 17:53.