Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C++

Solution for Multiple enable_shared_from_this in Inheritance Tree

Rate me:
Please Sign up or sign in to vote.
4.73/5 (5 votes)
21 Nov 2011MIT5 min read 45.9K   378   9  
The initial implementation of enable_shared_from_this of STL or the one from Boost C++ causes crash if it appears more than once in the inheritance tree of a user class. This is a solution for the problem. (search tag: enable shared from this)
#include "../EnableSharedFromThis.h"
#include "../SmartPtrBuilder.h"
#include <memory>
#include <iostream>

using namespace Generic;

class Base;
class Derived;

void FuncBase(std::shared_ptr< Base > iBase);
void FuncDerived(std::shared_ptr< Derived > iDerived);

class Base : public EnableSharedFromThis< Base >
{
public:
  Base(int iValue) : mValue(iValue) {}
  void BaseFunc()
  {
    FuncBase(EnableSharedFromThis< Base >::SharedFromThis());
  }
  void PrintValue()
  {
    std::cout << "Value = " << mValue << std::endl;
  }
private:
  int mValue;
};

class Derived : public Base, public EnableSharedFromThis< Derived >
{
public:
  // Factory method
  static std::shared_ptr< Derived > Create(int iValue)
  {
    return SmartPtrBuilder::CreateSharedPtr< Derived, Base >(new Derived(iValue));
  }
  void DerivedFunc()
  {
    FuncDerived(EnableSharedFromThis< Derived >::SharedFromThis());
  }
private:
  Derived(int iValue) : Base(iValue) {}
};

void FuncBase(std::shared_ptr< Base > iBase)
{
  iBase->PrintValue();
}

void FuncDerived(std::shared_ptr< Derived > iDerived)
{
  iDerived->PrintValue();
}

int main()
{
  // Simply use the factory method to instanciate Derived
  std::shared_ptr< Derived > wDerived = Derived::Create(123);

  // One or both of the following would crash if using the original std::enable_shared_from_this
  wDerived->BaseFunc();
  wDerived->DerivedFunc();
  return 0;
}

// Copyright (C) 2011 by Philippe Cayouette
     
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
     
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
     
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer (Senior) CAE Inc.
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions