Comments (3)
另外,这个方法根本限制不了在创建静态或线程局部存储期的该类对象。实现中这些存储期的对象都不在栈上。
务必要注意:如果允许通过一个接口构造或按值返回该类的对象,那么用户就可以在堆上动态分配一块存储(使用 malloc
或 ::operator new
),并用原位布置 new
把对象构造到堆上(如 ::new ((void*)p) T{args}
)。
一些近似的办法,但相当可能不符合题意:
- 完全限制该类的构造函数的访问,使得外部不得构造该类对象,而内部操作只在栈上构造该类对象。
- 生成不可移动或复制的 lambda 表达式闭包类型(需要至少 C++17)。
struct Pinned {
Pinned() = default;
Pinned(const Pinned&) = delete;
Pinned& operator=(const Pinned&) = delete;
};
int main()
{
auto pinned_lambda = [p = Pinned{}]{};
// 此后不能在其他地方构造另一个与 pinned_lambda 拥有相同类型的对象
}
个人认为完全符合题目本意的做法基本上是不存在的,除非通过和操作系统交流,在构造函数中确定 this
是否指向系统划定的栈空间。
from interview.
- 完全限制该类的构造函数的访问,使得外部不得构造该类对象,而内部操作只在栈上构造该类对象。
这种方法可能不太行
struct X {
static X construct() { return {}; }
private:
X() = default;
};
struct Y : X {
Y() : X{X::construct()} {}
};
int main()
{
X* px1 = new Y;
X* px2 = new (operator new(sizeof(X))) X(X::construct());
}
from interview.
- 完全限制该类的构造函数的访问,使得外部不得构造该类对象,而内部操作只在栈上构造该类对象。
这种方法可能不太行
struct X { static X construct() { return {}; } private: X() = default; }; struct Y : X { Y() : X{X::construct()} {} }; int main() { X* px1 = new Y; X* px2 = new (operator new(sizeof(X))) X(X::construct()); }
这个例子仍然相当于允许在外部构造该类对象。我指的是任何能创建该类对象的函数都不能被外部调用。
from interview.
Related Issues (20)
- CPP动态多态处勘误 HOT 3
- 单词里少了个字母 HOT 2
- 快排算法的疑问 HOT 3
- c++
- 快排里的else left++会执行吗?
- Interview HOT 3
- C++ 的inline 远不止如此 HOT 3
- #pragma pack(n) 应该强调是编译器扩展 HOT 2
- 使用typedef struct在C++毫无意义
- `dynamic_cast` 的描述不够严谨 HOT 10
- 关于 `extern “C”` 的描述,是不严谨,乃至错误的。 HOT 2
- C/C++ 智能指针中,unique_ptr小节有错别字(粗体处) HOT 3
- 图挂了
- dynamic_cast,向下转型返回空指针才对
- 大佬,图片挂了
- SMTP的全称似乎错了
- 无名管道,应为单工通信,写的半双工 HOT 2
- End HOT 5
- 大佬,我基于你的这个文档做了一个小程序,可否贴在这里呢? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from interview.