1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

this pointer problem

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

  1. Frank Reich

    Frank Reich New Member

    Joined:
    Nov 28, 2006
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    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