1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

this pointer problem

Discussion in 'C' started by Frank Reich, Dec 15, 2006.

  1. Frank Reich

    Frank Reich New Member

    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; 
    }
     
  2. DaWei

    DaWei New Member

    Code:
            void MyFunctA ()
            {
                    MyFunctB(this);      [COLOR=Red]'this' is a pointer to a cTest[/COLOR]
            }
            void MyFunctB (cTest a)  [COLOR=Red]'a' is an actual cTest object[/COLOR]
            {
            }
    
    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.
     

Share This Page