如何防止其他人在堆栈上创建您的类的新实例?

时间:2022-11-25 08:14:32

Suppose you write a class A, with constructor being private (to prevent others to create it on stack) then one day another developer add a new ctor, say A(int), and want to use inside main():

假设您编写了一个类A,构造函数是私有的(以防止其他人在堆栈上创建它)然后有一天另一个开发人员添加一个新的ctor,比如A(int),并想在main()中使用:

A a(1)

to create it on stack. How do you prevent that?

在堆栈上创建它。你怎么防止这种情况?

my solution:

Declare a public constructor

声明一个公共构造函数

  A(void& input )
   {
 Cerr << “please do not create it on stack” << endl ; 
  exit(1);
   }

I am not sure it is correct ?

我不确定它是否正确?

thanks

4 个解决方案

#1


7  

Put in a comment that says something like this:

发表评论说:

class A
{
    private:
        // This is private on purpose to prevent allocation on the stack.
        // We'll fire you if you ever write a new constructor that isn't private.
        A();
};

This comment is tongue-in-cheek (mostly) but it points to an important concept. Code conventions like disallowing stack allocation need to be enforced by peer review. As others have said, someone else could theoretically change the code however they want. But a good peer review process will help keep that in check. IMHO, that's far more cost effective than some clever compiler tricks that new hires might not necessarily understand.

这个评论是诙谐的(主要是),但它指出了一个重要的概念。禁止堆栈分配等代码约定需要通过同行评审来强制执行。正如其他人所说,其他人理论上可以改变他们想要的代码。但是,良好的同行评审过程将有助于控制这一点。恕我直言,这比新员工可能不一定理解的一些聪明的编译技巧更具成本效益。

#2


8  

As others say, you can't prevent people who can edit your class from making it do pretty much anything...BUT...

正如其他人所说,你不能阻止那些可以编辑你的课程的人做任何事情......但是......

...if you want a slightly more compiler-enforceable method than a comment, you could inherit from a class which has no default constructor. Anyone writing a constructor would be (hopefully) led to take notice of it. You could make its name cue people into taking certain precautions.

...如果你想要一个比注释更强的编译器可执行方法,你可以继承一个没有默认构造函数的类。编写构造函数的任何人(希望)都会注意到它。你可以让它的名字提示人们采取一些预防措施。

Something like this:

像这样的东西:

class DoNotStackConstruct {
protected:
    DoNotStackConstruct(const char* dummy) {}
};

class A : protected DoNotStackConstruct {
private:
    A () : DoNotStackConstruct ("PLEASE make all A constructors private!") {
       // your code here
    }
public:
    static std::tr1::shared_ptr<A> newA() {
        return std::tr1::shared_ptr<A>(new A);
    }

/* ... a bunch of code ... */
/* ... then someone later adds the following ... */

public:
    A (int i) {
        // can't force them to make it private, but...
        // this won't compile without mentioning DoNotStackConstruct
    }
};

Once you start using C++11 there will be "delegating constructors", and this trick will have a bit less teeth:

一旦你开始使用C ++ 11,就会有“委托构造函数”,这个技巧会少一点牙齿:

Can I call a constructor from another constructor (do constructor chaining) in C++?

我可以在C ++中从另一个构造函数(做构造函数链接)调用构造函数吗?

Then they'll be able to delegate to A() without visiting the source line and copying the "hey, don't make your constructor public!" text. But by default, they'll still get the compiler error the first time they try.

然后,他们将能够委托给A(),而无需访问源代码行并复制“嘿,不要让你的构造函数公开!”文本。但默认情况下,他们第一次尝试时仍会遇到编译器错误。

#3


5  

Clearly, you can't prevent it. If someone else can directly edit your code, then they can do whatever they want.

显然,你无法阻止它。如果其他人可以直接编辑您的代码,那么他们可以做任何他们想做的事情。

#4


1  

I think the solution you want is the following steps.

我认为您想要的解决方案是以下步骤。

  1. Give a constructor 'private' access.
  2. 给构造函数“私有”访问权限。

  3. Make a non-member static function who creates a instance by new or malloc and returns it.
  4. 创建一个非成员静态函数,由new或malloc创建一个实例并返回它。

  5. Use the function for creating an instance of the class.
  6. 使用该函数创建类的实例。

I am sure these step may be a solution of your question.

我相信这些步骤可能是您问题的解决方案。

#1


7  

Put in a comment that says something like this:

发表评论说:

class A
{
    private:
        // This is private on purpose to prevent allocation on the stack.
        // We'll fire you if you ever write a new constructor that isn't private.
        A();
};

This comment is tongue-in-cheek (mostly) but it points to an important concept. Code conventions like disallowing stack allocation need to be enforced by peer review. As others have said, someone else could theoretically change the code however they want. But a good peer review process will help keep that in check. IMHO, that's far more cost effective than some clever compiler tricks that new hires might not necessarily understand.

这个评论是诙谐的(主要是),但它指出了一个重要的概念。禁止堆栈分配等代码约定需要通过同行评审来强制执行。正如其他人所说,其他人理论上可以改变他们想要的代码。但是,良好的同行评审过程将有助于控制这一点。恕我直言,这比新员工可能不一定理解的一些聪明的编译技巧更具成本效益。

#2


8  

As others say, you can't prevent people who can edit your class from making it do pretty much anything...BUT...

正如其他人所说,你不能阻止那些可以编辑你的课程的人做任何事情......但是......

...if you want a slightly more compiler-enforceable method than a comment, you could inherit from a class which has no default constructor. Anyone writing a constructor would be (hopefully) led to take notice of it. You could make its name cue people into taking certain precautions.

...如果你想要一个比注释更强的编译器可执行方法,你可以继承一个没有默认构造函数的类。编写构造函数的任何人(希望)都会注意到它。你可以让它的名字提示人们采取一些预防措施。

Something like this:

像这样的东西:

class DoNotStackConstruct {
protected:
    DoNotStackConstruct(const char* dummy) {}
};

class A : protected DoNotStackConstruct {
private:
    A () : DoNotStackConstruct ("PLEASE make all A constructors private!") {
       // your code here
    }
public:
    static std::tr1::shared_ptr<A> newA() {
        return std::tr1::shared_ptr<A>(new A);
    }

/* ... a bunch of code ... */
/* ... then someone later adds the following ... */

public:
    A (int i) {
        // can't force them to make it private, but...
        // this won't compile without mentioning DoNotStackConstruct
    }
};

Once you start using C++11 there will be "delegating constructors", and this trick will have a bit less teeth:

一旦你开始使用C ++ 11,就会有“委托构造函数”,这个技巧会少一点牙齿:

Can I call a constructor from another constructor (do constructor chaining) in C++?

我可以在C ++中从另一个构造函数(做构造函数链接)调用构造函数吗?

Then they'll be able to delegate to A() without visiting the source line and copying the "hey, don't make your constructor public!" text. But by default, they'll still get the compiler error the first time they try.

然后,他们将能够委托给A(),而无需访问源代码行并复制“嘿,不要让你的构造函数公开!”文本。但默认情况下,他们第一次尝试时仍会遇到编译器错误。

#3


5  

Clearly, you can't prevent it. If someone else can directly edit your code, then they can do whatever they want.

显然,你无法阻止它。如果其他人可以直接编辑您的代码,那么他们可以做任何他们想做的事情。

#4


1  

I think the solution you want is the following steps.

我认为您想要的解决方案是以下步骤。

  1. Give a constructor 'private' access.
  2. 给构造函数“私有”访问权限。

  3. Make a non-member static function who creates a instance by new or malloc and returns it.
  4. 创建一个非成员静态函数,由new或malloc创建一个实例并返回它。

  5. Use the function for creating an instance of the class.
  6. 使用该函数创建类的实例。

I am sure these step may be a solution of your question.

我相信这些步骤可能是您问题的解决方案。