Giter Site home page Giter Site logo

Comments (3)

frederick-vs-ja avatar frederick-vs-ja commented on July 22, 2024 3

另外,这个方法根本限制不了在创建静态或线程局部存储期的该类对象。实现中这些存储期的对象都不在栈上。

务必要注意:如果允许通过一个接口构造或按值返回该类的对象,那么用户就可以在堆上动态分配一块存储(使用 malloc::operator new),并用原位布置 new 把对象构造到堆上(如 ::new ((void*)p) T{args})。


一些近似的办法,但相当可能不符合题意:

  1. 完全限制该类的构造函数的访问,使得外部不得构造该类对象,而内部操作只在栈上构造该类对象。
  2. 生成不可移动或复制的 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.

SainoNamkho avatar SainoNamkho commented on July 22, 2024
  1. 完全限制该类的构造函数的访问,使得外部不得构造该类对象,而内部操作只在栈上构造该类对象。

这种方法可能不太行

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.

frederick-vs-ja avatar frederick-vs-ja commented on July 22, 2024
  1. 完全限制该类的构造函数的访问,使得外部不得构造该类对象,而内部操作只在栈上构造该类对象。

这种方法可能不太行

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.