Skip to content

Instantly share code, notes, and snippets.

@RklAlx
Created September 27, 2013 12:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RklAlx/6727537 to your computer and use it in GitHub Desktop.
Save RklAlx/6727537 to your computer and use it in GitHub Desktop.
Private Constructor and make_shared: Solution: Using Pass-Key-Idiom Pattern URL: http://anthony-arnold.com/2012/04/05/three-fun-cpp-techniques/
void TestC()
{
std::shared_ptr<CC> y = CC::CreateCC(10);
y->Show();
getchar();
}
#include "C.h"
std::shared_ptr<CC> CC::CreateCC(int x)
{
return std::make_shared<CC>(CC::Key(),x);
}
CC::CC(const Key& rk, int xy)
{
y = xy;
}
CC::~CC(void)
{
}
void CC::Show()
{
printf("Value: %d\n", y);
}
#pragma once
#include <memory>
class CC
{
public:
static std::shared_ptr<CC> CreateCC(int value);
class Key {
private:
friend std::shared_ptr<CC> CC::CreateCC(int value);
Key() {}
};
CC(const Key&, int y);
~CC(void);
void Show();
private:
int y;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment