Created
March 3, 2019 06:12
-
-
Save frankie-yanfeng/54c9f05946c6c4e6f4e75d644aaa32cf to your computer and use it in GitHub Desktop.
C++ Private Constructor
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
class WonderfulClass | |
{ | |
public: | |
static WonderfulClass* makeAnObject() | |
{ | |
// 创建一个WonderfulClass对象并返回其指针 | |
return (new WonderfulClass); | |
} | |
private: | |
WonderfulClass() { } | |
}; | |
int main() | |
{ | |
WonderfulClass *p = WonderfulClass::makeAnObject(); | |
delete p; // Not neccesary here, but it's a good habit. | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment