Created
September 27, 2013 12:00
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void TestC() | |
{ | |
std::shared_ptr<CC> y = CC::CreateCC(10); | |
y->Show(); | |
getchar(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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