错误的编译警告? [英] wrong compiler warning?

查看:99
本文介绍了错误的编译警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有问题。下面的代码在gcc和

visual c ++中都会产生警告。但是,我认为这是正确的:基本上,有一个

函数返回一个派生类的对象,它被绑定到一个

基类引用将实际对象的销毁延迟到参考范围结束的
。实际上,我没有使用引用:重要的是

代码在析构函数中,我希望它在方法结束时以

执行。


真的很奇怪,两个编译器都警告我无用

参考...我担心如果编译器思考 ;参考

没用,他们可以尝试一些危险的优化......


下面是一个简单的例子:

#包括< iostream>


类基地

{

public:

Base(){ std :: cout<< " Base\\\
英寸;的std :: cout.flush(); }

virtual~Base(){std :: cout<< "〜Base\\\
英寸;的std :: cout.flush(); }


虚拟字符* Name()const {return" Base" ;; }

};


类派生:公共基地

{

public:

Derived(){std :: cout<< " Derived\\\
英寸;的std :: cout.flush(); }

~派生(){std :: cout<< "〜Derived\\\
英寸;的std :: cout.flush(); }


char * Name()const {return" Derived" ;; }

};


派生的foo()

{

std :: cout< < foo function\\\
; std :: cout.flush();

返回Derived();

}


int main()

{

std :: cout<< 程序开始\ n; std :: cout.flush();

const Base& obj = foo();

std :: cout<< 这是程序主体\ n; std :: cout.flush();


返回0;

}



谢谢你,

Zeppe

Hi all,

I''ve a problem. The code that follows creates a warning in both gcc and
visual c++. However, I think it''s correct: basically, there is a
function that return an object of a derived class, that''s bounded to a
base class reference to delay the destruction of the actual object to
the end of the reference scope. Actually, I don''t use the reference: the
code that matters is in the destructor, and I want it to be executed at
the end of the method.

Really strange, both the compilers warn me about the "useless"
reference... I''m feared that if the compilers "think" that the reference
is useless, they could try some dangerous optimization...

Here follows a simple example:
#include <iostream>

class Base
{
public:
Base() { std::cout << "Base\n"; std::cout.flush(); }
virtual ~Base() { std::cout << "~Base\n"; std::cout.flush(); }

virtual char* Name() const { return "Base"; }
};

class Derived : public Base
{
public:
Derived() { std::cout << "Derived\n"; std::cout.flush(); }
~Derived() { std::cout << "~Derived\n"; std::cout.flush(); }

char* Name() const { return "Derived"; }
};

Derived foo()
{
std::cout << "foo function\n"; std::cout.flush();
return Derived();
}

int main()
{
std::cout << "begin of the program\n"; std::cout.flush();
const Base& obj = foo();
std::cout << "this is the program body\n"; std::cout.flush();

return 0;
}


Thank you,

Zeppe

推荐答案

zeppe写道:
zeppe wrote:

我有问题。下面的代码在gcc

和visual c ++中都会产生警告。但是,我认为这是正确的:基本上,有一个

函数返回一个派生类的对象,它被绑定到一个

基类引用将实际对象的销毁延迟到参考范围结束的
。实际上,我没有使用引用:

重要的代码在析构函数中,我希望它在方法结束时执行


I''ve a problem. The code that follows creates a warning in both gcc
and visual c++. However, I think it''s correct: basically, there is a
function that return an object of a derived class, that''s bounded to a
base class reference to delay the destruction of the actual object to
the end of the reference scope. Actually, I don''t use the reference:
the code that matters is in the destructor, and I want it to be
executed at the end of the method.



只需声明一个对象而不是参考。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

Just declare an object instead of a reference.

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:

zeppe写道:
zeppe wrote:

>我有一个问题。下面的代码在gcc
和visual c ++中都会产生警告。但是,我认为这是正确的:基本上,有一个
函数返回派生类的对象,它被限制为
基类引用,以延迟实际的破坏反对参考范围的结束。实际上,我没有使用引用:
重要的代码在析构函数中,我希望它在方法结束时执行。
>I''ve a problem. The code that follows creates a warning in both gcc
and visual c++. However, I think it''s correct: basically, there is a
function that return an object of a derived class, that''s bounded to a
base class reference to delay the destruction of the actual object to
the end of the reference scope. Actually, I don''t use the reference:
the code that matters is in the destructor, and I want it to be
executed at the end of the method.



只需声明一个对象而不是一个引用。


V


Just declare an object instead of a reference.

V



如果我声明一个对象而不是一个引用我会失去类型多态性。


ie,输出参考:


begin该计划

foo功能

基本

派生

这是程序主体

~派生

~Base

输出对象:


程序开始

foo功能

基础

派生

~派生

~Base

这是程序体

~Base

当然在现实世界中我不知道foo返回Derived,所以

对象应该是基地。


Zeppe

if i declare an object instead of a reference i lose the type polymorphism.

i.e., output with reference:

begin of the program
foo function
Base
Derived
this is the program body
~Derived
~Base
output with object:

begin of the program
foo function
Base
Derived
~Derived
~Base
this is the program body
~Base
Of course in the real world i don''t know that foo returns Derived, so
the object should be Base.

Zeppe


zeppe写道:
zeppe wrote:

Victor Bazarov写道:
Victor Bazarov wrote:

> zeppe写道:
>zeppe wrote:

>>我有问题。下面的代码在gcc
和visual c ++中都会产生警告。但是,我认为这是正确的:基本上,有一个
函数返回派生类的对象,它与基类引用有关,以延迟实际的破坏
对象到参考范围的结尾。实际上,我没有使用
引用:重要的代码在析构函数中,我希望它在方法结束时执行。
>>I''ve a problem. The code that follows creates a warning in both gcc
and visual c++. However, I think it''s correct: basically, there is a
function that return an object of a derived class, that''s bounded
to a base class reference to delay the destruction of the actual
object to the end of the reference scope. Actually, I don''t use the
reference: the code that matters is in the destructor, and I want
it to be executed at the end of the method.


只需声明一个对象而不是引用。

V


Just declare an object instead of a reference.

V



如果我声明一个对象而不是引用我失去了类型

多态性。


if i declare an object instead of a reference i lose the type
polymorphism.



什么是多态?多态性删除它? D-tors不能多态调用
虚函数。如果你想依靠正确的

d-tor来调用,你可能需要使用''auto_ptr''并从你的函数返回

a指针,而不是暂时的。

What polymorphism? Polymorphism in deleting it? D-tors cannot call
virtual functions polymorphically. If you want to rely on proper
d-tor to be called, you probably need to use ''auto_ptr'' and return
a pointer from your function, not a temporary.


ie,输出参考:


[..]


当然在现实世界中,我不知道foo会返回Derived,所以

对象应该是Base。
i.e., output with reference:

[..]

Of course in the real world i don''t know that foo returns Derived, so
the object should be Base.



嗯?


V

-

请在通过电子邮件回复时删除资本''A'

我没有回复最热门的回复,请不要问

Huh?

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


这篇关于错误的编译警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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