Jump to content

  • Log In with Google      Sign In   
  • Create Account

#Actualelurahu

Posted 18 November 2012 - 04:50 PM

struct A
{
RFDelegate1<int> m_kDelegate;
A()
{
  std::cout << "A" << std::endl;
}
~A()
{
  std::cout << "~A" << std::endl;
}
void DoWork()
{
  if (!m_kDelegate.empty())
   m_kDelegate(69);
}
};
struct B
{
std::weak_ptr<A> m_wpA;
int m_i;
B(const std::shared_ptr<A>&amp; spA)
{
  std::cout << "B" << std::endl;
  m_wpA = std::weak_ptr<A>(spA);
  spA->m_kDelegate = RFMakeDelegate(this, &amp;B::Handler);
}
~B()
{
  std::cout << "~B" << std::endl;
  if (!m_wpA.expired())
  {
   m_wpA.lock()->m_kDelegate.clear();
  }
  std::cout << "Deleted" << std::endl;
}
void Handler(int i)
{
  m_i = i;
  std::cout << "Handled " << m_i << std::endl;
}
};

// Usage
{
std::shared_ptr<A> spA = std::make_shared<A>();
B* pkB = new B(spA);
spA.reset();
delete pkB;
}

Well this is what I was doing now - But it's hardly a "pretty" solution. But I guess that's just C++ for you.

#3elurahu

Posted 18 November 2012 - 04:50 PM

struct A
{
RFDelegate1<int> m_kDelegate;
A()
{
  std::cout << "A" << std::endl;
}
~A()
{
  std::cout << "~A" << std::endl;
}
void DoWork()
{
  if (!m_kDelegate.empty())
   m_kDelegate(69);
}
};
struct B
{
std::weak_ptr<A> m_wpA;
int m_i;
B(const std::shared_ptr<A>&amp; spA)
{
  std::cout << "B" << std::endl;
  m_wpA = std::weak_ptr<A>(spA);
  spA->m_kDelegate = RFMakeDelegate(this, &amp;B::Handler);
}
~B()
{
  std::cout << "~B" << std::endl;
  if (!m_wpA.expired())
  {
   m_wpA.lock()->m_kDelegate.clear();
  }
  std::cout << "Deleted" << std::endl;
}
void Handler(int i)
{
  m_i = i;
  std::cout << "Handled " << m_i << std::endl;
}
};

// Usage
{
std::shared_ptr<A> spA = std::make_shared<A>();
B* pkB = new B(spA);
spA.reset();
delete pkB;
}

Well this is what I was doing now - But it's hardly a "pretty" solution. But I guess that's just C++ for you.

#2elurahu

Posted 18 November 2012 - 04:50 PM

struct A
{
RFDelegate1<int> m_kDelegate;
A()
{
  std::cout << "A" << std::endl;
}
~A()
{
  std::cout << "~A" << std::endl;
}
void DoWork()
{
  if (!m_kDelegate.empty())
   m_kDelegate(69);
}
};
struct B
{
std::weak_ptr<A> m_wpA;
int m_i;
B(const std::shared_ptr<A>&amp; spA)
{
  std::cout << "B" << std::endl;
  m_wpA = std::weak_ptr<A>(spA);
  spA->m_kDelegate = RFMakeDelegate(this, &amp;B::Handler);
}
~B()
{
  std::cout << "~B" << std::endl;
  if (!m_wpA.expired())
  {
   m_wpA.lock()->m_kDelegate.clear();
  }
  std::cout << "Deleted" << std::endl;
}
void Handler(int i)
{
  m_i = i;
  std::cout << "Handled " << m_i << std::endl;
}
};

// Usage
{
std::shared_ptr<A> spA = std::make_shared<A>();
B* pkB = new B(spA);
spA.reset();
delete pkB;
}

Well this is what I was doing now - But it's hardly a "pretty" solution. But I guess that's just C++ for you.

#1elurahu

Posted 18 November 2012 - 04:50 PM

struct A
{
RFDelegate1<int> m_kDelegate;
A()
{
  std::cout << "A" << std::endl;
}
~A()
{
  std::cout << "~A" << std::endl;
}
void DoWork()
{
  if (!m_kDelegate.empty())
   m_kDelegate(69);
}
};
struct B
{
std::weak_ptr<A> m_wpA;
int m_i;
B(const std::shared_ptr<A>& spA)
{
  std::cout << "B" << std::endl;
  m_wpA = std::weak_ptr<A>(spA);
  spA->m_kDelegate = RFMakeDelegate(this, &B::Handler);
}
~B()
{
  std::cout << "~B" << std::endl;
  if (!m_wpA.expired())
  {
   m_wpA.lock()->m_kDelegate.clear();
  }
  std::cout << "Deleted" << std::endl;
}
void Handler(int i)
{
  m_i = i;
  std::cout << "Handled " << m_i << std::endl;
}
};
{
std::shared_ptr<A> spA = std::make_shared<A>();
B* pkB = new B(spA);
spA.reset();
delete pkB;
}

Well this is what I was doing now - But it's hardly a "pretty" solution. But I guess that's just C++ for you.

PARTNERS