朋友函数std :: make_shared()在Visual Studio 2010(不是Boost) [英] friend function of std::make_shared() in Visual Studio 2010 (not Boost)

查看:607
本文介绍了朋友函数std :: make_shared()在Visual Studio 2010(不是Boost)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使 std :: make_shared()的朋友功能。

我试过:

class MyClass{
public:
     friend std::shared_ptr<MyClass> std::make_shared<MyClass>();
     //or
     //friend std::shared_ptr<MyClass> std::make_shared();
protected:
     MyClass();
};

但不起作用(我使用Visual Studio 2010 SP1)

but it does not work (i'am using Visual Studio 2010 SP1)

推荐答案

如何在类中添加静态方法:

How about adding a static method to your class:

class Foo
{
public:
  static shared_ptr<Foo> create() { return std::shared_ptr<Foo>(new Foo); }
private:
  // ...
};

这里有一个小问题:

class Foo
{
  struct HideMe { };
  Foo() { };
public:
  explicit Foo(HideMe) { };
  static shared_ptr<Foo> create() { return std::make_shared<Foo>(HideMe());
};

没有人可以使用类本身以外的公共构造函数。它本质上是公共接口的非接口部分。请问Ja​​va人是否有这样的名字: - )

Nobody can use the public constructor other than the class itself. It's essentially a non-interface part of the public interface. Ask the Java people if such a thing has a name :-)

这篇关于朋友函数std :: make_shared()在Visual Studio 2010(不是Boost)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆