在没有构造函数的情况下调用析构函数。 [英] destructor invoked without constructor.

查看:98
本文介绍了在没有构造函数的情况下调用析构函数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有以下单位来解释我的问题,


班级样本

{

public:sample()

{

printf(" in in sample ... \ n");

}

~sample()

{

printf(" out sample ... \ n" );

}

void invoke(示例obj)

{

printf(" in invoke .. 。$ \\ nn;

}

};


int main()


{


样本obj;

样本obj1;

obj1.invoke(obj);

printf(" ... end ... \ n");

返回0;


}

以下是输出,


in sample ...

in sample ...

in invoke。 ..

样品......

....结束......

样品...

out sample ...


i认为第一个out sample......是为了通过

值传递的对象obj,它表示新的临时对象已被销毁,

这意味着它应该已经被创建,如果是的话,应该' '

样本中的''在调用之前再次打印过......?

Hi Everyone,

I have the following unit to explain the problem that i have,

class sample
{
public : sample()
{
printf("in sample...\n");
}
~sample()
{
printf("out sample...\n");
}
void invoke(sample obj)
{
printf("in invoke...\n");
}
};

int main()

{

sample obj;
sample obj1;
obj1.invoke(obj);
printf("...end...\n");
return 0;

}
The following is the output,

in sample...
in sample...
in invoke...
out sample...
....end...
out sample...
out sample...

i think the first "out sample..." is for the object obj passed by
value, which indicates that a new temp object has been destroyed,
which means that it should have been created, if so, shouldn''t the "in
sample" have been printed once more before "in invoke"...?

推荐答案

On 4 Maj,11:03,sam_。 .. @ yahoo.co.in写道:
On 4 Maj, 11:03, sam_...@yahoo.co.in wrote:

大家好,


我有以下单位来解释这个问题我有,


班级样本

{

public:sample()

{

printf(" in in sample ... \ n");

}

~sample()

{

printf(" out sample ... \ n");

}

void invoke(sample obj)

{

printf(" in invoke ... \ n");

}

};


int main()


{


样本obj;

样本obj1;

obj1.invoke(obj);

printf(" ... end ... \ n");

返回0;


}


以下是输出,


样本...

样本...

in invoke ...

样品......

...结束......

样品...

out sample ...


i认为第一个out sample......是为了通过

值传递的对象obj,它表示新的临时对象已被销毁,

这意味着它应该已经被创建,如果是的话,应该' '

样本中的''在调用之前再次打印过......?
Hi Everyone,

I have the following unit to explain the problem that i have,

class sample
{
public : sample()
{
printf("in sample...\n");
}
~sample()
{
printf("out sample...\n");
}
void invoke(sample obj)
{
printf("in invoke...\n");
}
};

int main()

{

sample obj;
sample obj1;
obj1.invoke(obj);
printf("...end...\n");
return 0;

}

The following is the output,

in sample...
in sample...
in invoke...
out sample...
...end...
out sample...
out sample...

i think the first "out sample..." is for the object obj passed by
value, which indicates that a new temp object has been destroyed,
which means that it should have been created, if so, shouldn''t the "in
sample" have been printed once more before "in invoke"...?



它是创建的,但是使用了copy-constructor,你没有实现
,所以使用了编译器生成的一个,并且那个人确实没有打印任何东西。添加


sample(const sample&){std :: cout<< " copy\\\
英寸; }


到你班上看看会发生什么。


-

Erik Wikstr?m


< sa ***** @ yahoo.co.inwrote in message

news:11 ******* ***************@l77g2000hsb.googlegr oups.com ...
<sa*****@yahoo.co.inwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...

大家好,


我有以下单位来解释我的问题,


班级样本

{

public:sample()

{

printf(" in sample ... \ n");

}

~sample()

{

printf(" out sample ... \ n");

}

void invoke(示例obj)

{

printf(" in invoke ... \ n");

}

};


int main()


{

样本obj;

样本obj1;

obj1.invoke(obj);

printf(" ... end ... \ n");

返回0;


}


以下是输出,


样本......

样本...

调用...

out sample ...

...结束......

示例...

示例...


i认为第一个out sample ...是为了通过

值传递的对象obj,它表示新的临时对象已被销毁,

这意味着它应该已经被创建,如果是的话,应该' '

样本中的''在调用之前再次打印过......?
Hi Everyone,

I have the following unit to explain the problem that i have,

class sample
{
public : sample()
{
printf("in sample...\n");
}
~sample()
{
printf("out sample...\n");
}
void invoke(sample obj)
{
printf("in invoke...\n");
}
};

int main()

{

sample obj;
sample obj1;
obj1.invoke(obj);
printf("...end...\n");
return 0;

}
The following is the output,

in sample...
in sample...
in invoke...
out sample...
...end...
out sample...
out sample...

i think the first "out sample..." is for the object obj passed by
value, which indicates that a new temp object has been destroyed,
which means that it should have been created, if so, shouldn''t the "in
sample" have been printed once more before "in invoke"...?



编号临时是使用复制构造函数创建的,因为你没有提供任何它自动生成的(&和
)显然没有使用

printf语句)。添加以下构造函数,它应该像你期望的那样工作:


sample(const sample& s)

{

printf(" in sample ...(copy ctor)\ n");

}


顺便说一下,它也可能很方便输出这个指针,所以你可以确切地说,看看是什么ctor-dtor对彼此相同:)


- 西尔维斯特


5月4日下午2:03,sam _... @ yahoo.co.in写道:
On May 4, 2:03 pm, sam_...@yahoo.co.in wrote:

嗨每个人,

我有以下单位来解释我的问题,


班级样本

{

public:sample()

{

printf(" in sample ... \ n");

}

~sample()

{

printf(" out sample ... \ n");

}

无效调用(示例obj)

{

pri ntf(" in invoke ... \ n");

}

};


int main()


{


样本obj;

样本obj1;

obj1.invoke (obj);

printf(" ... end ... \ n");

返回0;


}


以下是输出,


样品...

样品.. 。

调用...

样品...

...结束......

样品......

样品......


i认为第一个样品外...是为了通过

值传递的对象obj,它表示新的临时对象已被销毁,

这意味着它应该已经被创建,如果是的话,应该' '

样本中的''在调用之前再次打印过......?
Hi Everyone,

I have the following unit to explain the problem that i have,

class sample
{
public : sample()
{
printf("in sample...\n");
}
~sample()
{
printf("out sample...\n");
}
void invoke(sample obj)
{
printf("in invoke...\n");
}
};

int main()

{

sample obj;
sample obj1;
obj1.invoke(obj);
printf("...end...\n");
return 0;

}

The following is the output,

in sample...
in sample...
in invoke...
out sample...
...end...
out sample...
out sample...

i think the first "out sample..." is for the object obj passed by
value, which indicates that a new temp object has been destroyed,
which means that it should have been created, if so, shouldn''t the "in
sample" have been printed once more before "in invoke"...?



调用带有pass by value参数的函数肯定会调用

复制构造函数,而通过引用传递参数将不会

创建一个对象。

所以如果你已经为类样本定义了一个拷贝构造函数,

其中你怀疑的打印语句会'已经清理过了。


我希望这会有所帮助。


-

Sukumar R

Invoking a function with pass by value argument would definitely call
copy constructor, whereas pass the argument by reference wouldn''t
create an object.
So If you would''ve defined a copy constructor for the class sample,
within which a print statement your doubt would''ve got cleared.

I hope this helps.

-
Sukumar R


这篇关于在没有构造函数的情况下调用析构函数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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