this pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kasya
    New Member
    • Jun 2006
    • 57

    this pointer

    Hello,

    I have some problems with "this" pointer:

    Code:
    CInstance::CInstance(CPrototype *backing) {
    _backing = backing; //CPrototype *_backing;
    this = backing->newInstance(); //CInstance * CPrototype::newInstance();
    }
    It gives me error when i do it: C2106: '=' : left operand must be l-value (LINE 3)

    What can i do?

    Thanks,
    Kasya

    P.S I don't want to use function like void CPrototype::new Instance(CInsta nce * instance);
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    The this pointer is not a modifiable lvalue so you can't assign anything to it.

    kind regards,

    Jos

    Comment

    • arnaudk
      Contributor
      • Sep 2007
      • 425

      #3
      If you're trying to make a new Cinstance from a Cprototype, you can use the default constructor and simply
      [code=cpp]
      CPrototype * backing;
      Cinstance * myinstance = backing->newInstance( );
      [/code]
      This has the same logic as your code, which would have resulted in something like
      [code=cpp]
      CPrototype * backing;
      Cinstance * myinstance = new Cinstance(backi ng);
      [/code]
      except that you can't assign to the this pointer.

      Comment

      Working...