总是在堆中创建对象? [英] Always create the object in heap?

查看:51
本文介绍了总是在堆中创建对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在最近一次关于C ++的技术面试中,我遇到了这个奇怪的

问题....


如何确保始终在堆中创建类对象(通过

new运算符)?如果对象是在堆栈中创建的,那么编译器应该抛出错误。


注意: - 不要隐藏建设者。


有可能吗?



In a recent techincal interview on C++, I came across this strange
question....

How to ensure that your class object is always created in heap (by a
new operator)? The compiler should throw error, if the object is
created in stack.

Note:- Dont hide the construtor.

Is it possible?

推荐答案

* Karthik:
* Karthik:

>

在最近一次关于C ++的技术面试中,我遇到了这个奇怪的

问题....


如何确保始终在堆中创建类对象(通过

new运算符)?如果对象是在堆栈中创建的,那么编译器应该抛出错误。


注意: - 不要隐藏建设者。


有可能吗?
>
In a recent techincal interview on C++, I came across this strange
question....

How to ensure that your class object is always created in heap (by a
new operator)? The compiler should throw error, if the object is
created in stack.

Note:- Dont hide the construtor.

Is it possible?



一个简短的回答是让析构函数无法访问。


但是有细节,例如如何允许''删除'',然后如何确保客户端代码使用智能指针(如果可取的话)。


请参阅1.1.3及以下各节< url:

http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf>。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?

A short answer is to make the destructor inaccessible.

But there are details, e.g. how to allow to ''delete'', and then how to
ensure the client code uses smart pointers (if that is desirable).

See section 1.1.3 and following sections of <url:
http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Karthik发布:
Karthik posted:

>


在最近一次关于C ++的技术面试中,我遇到了这个奇怪的

问题....


如何确保始终在堆中创建类对象(通过

new运算符)?如果对象是在堆栈中创建的,那么编译器应该抛出错误。


注意: - 不要隐藏建设者。


有可能吗?
>

In a recent techincal interview on C++, I came across this strange
question....

How to ensure that your class object is always created in heap (by a
new operator)? The compiler should throw error, if the object is
created in stack.

Note:- Dont hide the construtor.

Is it possible?



我认为他们会做类似以下的事情。 (我不知道如何获得它

来编译...):


class MyClass {

private :


~MyClass(){}


public:


void operator delete(void * const parg)

{

::删除static_cast< MyClass *>(parg);

}

};


int main()

{

MyClass * const p = new MyClass;


删除p;

}


-


Frederick Gotham


I think they do something like the following. (I don''t know how to get it
to compile...):

class MyClass {
private:

~MyClass() {}

public:

void operator delete(void *const parg)
{
::delete static_cast<MyClass*>(parg);
}
};

int main()
{
MyClass *const p = new MyClass;

delete p;
}

--

Frederick Gotham


* Frederick Gotham:
* Frederick Gotham:

Karthik发布:
Karthik posted:

>>
在最近一次关于C ++的技术访谈中,我遇到了这个奇怪的问题....

如何确保始终在堆中创建类对象(由新运营商提供)?如果对象是在堆栈中创建的,编译器应该抛出错误。

注意: - 不要隐藏construtor。

有可能吗?
>>
In a recent techincal interview on C++, I came across this strange
question....

How to ensure that your class object is always created in heap (by a
new operator)? The compiler should throw error, if the object is
created in stack.

Note:- Dont hide the construtor.

Is it possible?




我认为他们会做类似以下的事情。 (我不知道如何获得它

来编译...):


class MyClass {

private :


~MyClass(){}


public:


void operator delete(void * const parg)

{

::删除static_cast< MyClass *>(parg);

}

};


int main()

{

MyClass * const p = new MyClass;


删除p;

}



I think they do something like the following. (I don''t know how to get it
to compile...):

class MyClass {
private:

~MyClass() {}

public:

void operator delete(void *const parg)
{
::delete static_cast<MyClass*>(parg);
}
};

int main()
{
MyClass *const p = new MyClass;

delete p;
}



否,''operator delete''是一个释放函数,不是覆盖

关键字''删除''。请参阅我给出的参考。即使它是我自己写的
... ;-)


-

答:因为它搞砸了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问: usenet和电子邮件中最烦人的事情是什么?

No, ''operator delete'' is a deallocation function, not an override of the
keyword ''delete''. See the reference I gave elsethread. Even if it was
written by myself... ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于总是在堆中创建对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆