0

following the example from https://stackoverflow.com/a/3520629/5642268 which works nicely as it is.

Now i wanted to extend on it and property of the class type "User" inside the Impl and fill that propery inside the the Impl object like:

class Interface
{
  public:
  virtual void callback() = 0;
};
class User
{
  private:
    Interface& myCallback;

  public:
    User(Interface& newCallback) : myCallback(newCallback) { }
    void DoSomething() { myCallback.callback(); }
};

class Impl : public Interface
{
  private:
    User * m_user = nullptr;

  public:
    Impl () {
       m_user = new User (this); 
       //  ====================    
   }
  virtual void callback() { std::cout << "Hi from Impl\n"; }
  User * get_userinfo () { return m_user; }
};
int main()
{
  Impl impl;
  impl.get_userinfo()->DoSomething();
}

The "this" should be replaced with something referencing the Impl object, I think, or the change could be somewhere else.

Compiling the above code i get the following error messages:

In member function 'Chain* Impl::testing(Chain*)':
error: no matching function for call to 'Chain::Chain(Impl*)'
       Chain * x =new Chain(this);
note: candidate: Chain::Chain(Interface&)
      Chain (Interface& newCallback) : myCallback(newCallback)  { } 
note: no known conversion for argument 1 from 'Impl*' to 'Interface&'
note: candidate: constexpr Chain::Chain(const Chain&)
      class Chain {
note: no known conversion for argument 1 from 'Impl*' to 'const Chain&'
note: candidate: constexpr Chain::Chain(Chain&&)
note: no known conversion for argument 1 from 'Impl*' to 'Chain&&' exit status 1
no matching function for call to 'Chain::Chain(Impl*)'

Lots of notes that does not make sense to me on how to resolve the issue.

There must be a better way but i have no clue where to search for.

Any Help is welcome. Some ref's to documentation would also be a pre.

Finally i find some solution to this issue. this is a pointer to the object it self. To get the object you need to add a Asterisk before the "this" and now it compiles.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Niels
  • 28
  • 1
  • 7

0 Answers0