关于auto_ptr的问题,哪个函数会先调用? [英] Question on auto_ptr, Which function will call first?

查看:40
本文介绍了关于auto_ptr的问题,哪个函数会先调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在研究<< C ++中的思考>第二卷。在

第一章中,示例代码:Auto_ptr.cpp

// --------------------- ----------------------------------

#include< memory>

#include< iostream>

#include< cstddef>

使用命名空间std;


class TraceHeap {

int i;

public:

static void * operator new(size_t siz){// *****注意A

void * p = :: operator new(siz);

cout<< 在堆上分配TraceHeap对象

<< at address << p << endl;

返回p;

}

static void operator delete(void * p){

cout< ;< 删除地址处的TraceHeap对象

<< p << endl;

:: operator delete(p);

}

TraceHeap(int i)// *******注意B

:我(i)

{

;

}

int getVal()const {return i; }

};


int main(){

auto_ptr< TraceHeappMyObject(new TraceHeap(5));

cout<< pMyObject-> getVal()<< ENDL; //打印5

}

// --------------------------- ---------------------------------------


我的问题是:


在我的代码中:首先调用哪些代码? *注意A *或*注B *?

为什么?


我正在通过这段代码调试并发现* NOTE A *将首先打电话给

。有人可以解释一下吗?

当我追踪* new *函数时,* siz *值是4.我不知道

为什么它会是4 ?


感谢您阅读我的留言。

Hi, everyone, I''m studying the <<Thinking in C++>volume Two. In
Chapter One, the example code : Auto_ptr.cpp
//-------------------------------------------------------
#include <memory>
#include <iostream>
#include <cstddef>
using namespace std;

class TraceHeap {
int i;
public:
static void* operator new(size_t siz) { //*****NOTE A
void* p = ::operator new(siz);
cout << "Allocating TraceHeap object on the heap "
<< "at address " << p << endl;
return p;
}
static void operator delete(void* p) {
cout << "Deleting TraceHeap object at address "
<< p << endl;
::operator delete(p);
}
TraceHeap(int i) //*******NOTE B
: i(i)
{
;
}
int getVal() const { return i; }
};

int main() {
auto_ptr<TraceHeappMyObject(new TraceHeap(5));
cout << pMyObject->getVal() << endl; // Prints 5
}
//------------------------------------------------------------------

My question is :

In My code: which code will be called first? The *NOTE A* or *NOTE B* ?
And Why?

I''m debugging through this code and found that *NOTE A* will called
first. Can someone explained it?
When I trace into the *new* function, the *siz* value is 4. I don''t know
why it will be 4?

Thank you for reading my message.

推荐答案

asm23写道:
asm23 wrote:

大家好,我正在研究<< Thinking in C ++>第二卷。在

第一章中,示例代码:Auto_ptr.cpp

// --------------------- ----------------------------------

#include< memory>

#include< iostream>

#include< cstddef>

使用命名空间std;


class TraceHeap {

int i;

public:

static void * operator new(size_t siz){// *****注意A

void * p = :: operator new(siz);

cout<< 在堆上分配TraceHeap对象

<< at address << p << endl;

返回p;

}

static void operator delete(void * p){

cout< ;< 删除地址处的TraceHeap对象

<< p << endl;

:: operator delete(p);

}

TraceHeap(int i)// *******注意B

:我(i)

{

;

}

int getVal()const {return i; }

};


int main(){

auto_ptr< TraceHeappMyObject(new TraceHeap(5));

cout<< pMyObject-> getVal()<< ENDL; //打印5

}

// --------------------------- ---------------------------------------


我的问题是:


在我的代码中:首先调用哪些代码? *注意A *或*注B *?

为什么?
Hi, everyone, I''m studying the <<Thinking in C++>volume Two. In
Chapter One, the example code : Auto_ptr.cpp
//-------------------------------------------------------
#include <memory>
#include <iostream>
#include <cstddef>
using namespace std;

class TraceHeap {
int i;
public:
static void* operator new(size_t siz) { //*****NOTE A
void* p = ::operator new(siz);
cout << "Allocating TraceHeap object on the heap "
<< "at address " << p << endl;
return p;
}
static void operator delete(void* p) {
cout << "Deleting TraceHeap object at address "
<< p << endl;
::operator delete(p);
}
TraceHeap(int i) //*******NOTE B
: i(i)
{
;
}
int getVal() const { return i; }
};

int main() {
auto_ptr<TraceHeappMyObject(new TraceHeap(5));
cout << pMyObject->getVal() << endl; // Prints 5
}
//------------------------------------------------------------------

My question is :

In My code: which code will be called first? The *NOTE A* or *NOTE B* ?
And Why?



''operator new''函数是类范围的分配函数。

在构造该类的任何对象之前免费商店,必须分配

内存。这就是为什么分配发生在

之前的结构。

The ''operator new'' function is the class-wide allocation function.
Before any object of that class can be constructed in free store, the
memory has to be allocated. That''s why the allocation happens before
the construction.


我正在通过这段代码调试并发现*注意A *将首先调用

。有人可以解释一下吗?
I''m debugging through this code and found that *NOTE A* will called
first. Can someone explained it?



我想我刚刚做过。

I think I just did.


当我追踪* new *函数时,* siz *值是4.我不知道

为什么它会是4?
When I trace into the *new* function, the *siz* value is 4. I don''t know
why it will be 4?



你正在创建的TraceHeap类型的对象有一个数据

成员,''int i''和no虚函数或基类。结论除了数据成员之外没有任何东西对

对象的大小有贡献,这是合理的。在您的平台上''int''可能有4个字节的

大小...尝试在某个地方打印出'sizeof(TraceHeap)',什么

你呢得到。


V

-

请在通过电子邮件回复时删除资金'A' />
我没有回复最热门的回复,请不要问

The object of type TraceHeap that you''re creating has a single data
member, ''int i'', and no virtual functions or base classes. It''s
reasonable to conclude that nothing contributes to the size of the
object except the data members. On your platform ''int'' probably has the
size of 4 bytes... Try printing out ''sizeof(TraceHeap)'' somewhere, what
do you get.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Victor Bazarov写道:
Victor Bazarov wrote:

>

''operator new''函数是类范围的分配函数。

在构造该类的任何对象之前在免费商店,必须分配

内存。这就是为什么分配发生在

之前的结构。
>
The ''operator new'' function is the class-wide allocation function.
Before any object of that class can be constructed in free store, the
memory has to be allocated. That''s why the allocation happens before
the construction.



谢谢Victor。我明白了,我与* allocation *和

* construction *混淆了。但是现在,我知道它们是不同的并且执行了它们的b $ b序列。

Thanks Victor. I understand, I confused with *allocation* and
*construction*. But now, I know they are different and executing
sequence of them.


>

我认为我刚刚做了。


你正在创建的TraceHeap类型的对象有一个数据

成员,''int i'',没有虚函数或基类。结论除了数据成员之外没有任何东西对

对象的大小有贡献,这是合理的。在您的平台上''int''可能有4个字节的

大小...尝试在某个地方打印出'sizeof(TraceHeap)',什么

你呢得到。


V
>
I think I just did.
The object of type TraceHeap that you''re creating has a single data
member, ''int i'', and no virtual functions or base classes. It''s
reasonable to conclude that nothing contributes to the size of the
object except the data members. On your platform ''int'' probably has the
size of 4 bytes... Try printing out ''sizeof(TraceHeap)'' somewhere, what
do you get.

V



哦,是的,sizeof(TraceHeap)是4.但我仍然有一个难题。为什么

在重载的new和delete操作之前这些是* static *?

我知道静态函数没有传递* this *参数。但是在

这个案例中,一个静态的新的或正常的新似乎没有区别。


谢谢你的帮助。


10月3日下午6:00,asm23< asmwarr ... @ gmail.comwrote:
On Oct 3, 6:00 pm, asm23 <asmwarr...@gmail.comwrote:

嗨大家好,我正在研究<<思考在C ++>第二卷。

在第一章中,示例代码:Auto_ptr.cpp

// ------------------------------------------------ -------

#include< memory>

#include< iostream>

#include< cstddef>

using namespace std;
Hi, everyone, I''m studying the <<Thinking in C++>volume Two.
In Chapter One, the example code : Auto_ptr.cpp
//-------------------------------------------------------
#include <memory>
#include <iostream>
#include <cstddef>
using namespace std;


class TraceHeap {

int i;

public:

static void * operator new(size_t siz){// *****注意A

void * p = :: operator new(siz);

cout << 在堆上分配TraceHeap对象

<< at address << p << endl;

返回p;

}

static void operator delete(void * p){

cout< ;< 删除地址处的TraceHeap对象

<< p << endl;

:: operator delete(p);

}

TraceHeap(int i)// *******注意B

:我(i)

{

;

}

int getVal()const {return i; }

};
class TraceHeap {
int i;
public:
static void* operator new(size_t siz) { //*****NOTE A
void* p = ::operator new(siz);
cout << "Allocating TraceHeap object on the heap "
<< "at address " << p << endl;
return p;
}
static void operator delete(void* p) {
cout << "Deleting TraceHeap object at address "
<< p << endl;
::operator delete(p);
}
TraceHeap(int i) //*******NOTE B
: i(i)
{
;
}
int getVal() const { return i; }
};


int main(){

auto_ptr< TraceHeappMyObject(new TraceHeap(5));

cout<< pMyObject-> getVal()<< ENDL; //打印5

}

// --------------------------- ---------------------------------------
int main() {
auto_ptr<TraceHeappMyObject(new TraceHeap(5));
cout << pMyObject->getVal() << endl; // Prints 5
}
//------------------------------------------------------------------


我的问题是:
My question is :


在我的代码中:首先调用哪个代码? *注意A *或

*注意B *?为什么?
In My code: which code will be called first? The *NOTE A* or
*NOTE B* ? And Why?



注意A,显然。琐碎的原因是因为这就是语言所需要的。当然,语言

要求这样做的原因是它不可能正常工作:你需要为b / b
分配内存才能

构造它。

NOTE A, obviously. The trivial reason is because that is what
the language requires. Of course, the reason the language
requires this is that it couldn''t possibly work otherwise: you
have to allocate the memory for the object before you can
construct it.


我正在通过这段代码进行调试,发现* NOTE A *将调用

第一。有人可以解释一下吗?
I''m debugging through this code and found that *NOTE A* will called
first. Can someone explained it?



想想。如果在注释A之前调用注释B是什么意思。

Think. What would it mean if NOTE B were called before NOTE A.


当我追踪到* new *函数时,* siz *值为4.我不知道不知道

为什么会是4?
When I trace into the *new* function, the *siz* value is 4. I don''t know
why it will be 4?



因为这是你机器上TraceHeap对象的大小。


-

James Kanze(GABI Software)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,France,+ 33(0)1 30 23 00 34

Because that''s the size of a TraceHeap object on your machine.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


这篇关于关于auto_ptr的问题,哪个函数会先调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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