Skip to content

Instantly share code, notes, and snippets.

@RklAlx RklAlx/CC.cpp
Created Sep 27, 2013

Embed
What would you like to do?
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
You can’t perform that action at this time.