合法与否? [英] Legal or not?
问题描述
嗨!
我尝试用Comeau编译以下代码,但它给了我一个
令人费解的错误(代码没有意义,它只是为了表明我的困惑)
班级基地
{
公共:
基地();
Base(int);
};
class派生:public Base
{
public:
Derived(int i)
{
Base(0); //编译好,但是
Base :: Base(0); //不编译,Comeau说:
//错误:构造函数或析构函数可能没有使用
//地址
< br $>
}
};
线路是否有效?
迎接,
Christoph
" Christoph Rabel" < OD ** @ hal9000.vc-graz.ac.at>在消息中写道
news:3f *********************** @ aconews.univie.ac.a t ... < blockquote class =post_quotes>嗨!
我试着用Comeau编译下面的代码,但它给了我一个令人费解的错误(代码没有意义,它只是为了显示什么困惑
我)
班级基地
{
公开:
基地();
基地(int);
};
类派生:公共基地
{
公开:
Derived(int i)
{
Base(0); //编译正常,但是
创建一个类型为''Base''的临时对象。
Base :: Base(0); //不编译,Comeau说:
我不知道为什么它说你不应该拿地址,因为我
会想到这条线上面的意思是调用c''tor,
无法工作。
//错误:构造函数或析构函数可能没有它
//地址>
}
};
该行是否有效?
尽管我不太明白为什么Comeau打印/那个/错误
消息,我仍然觉得它无效。
hth
-
jb
(如果你想通过电子邮件回复,用x替换y)
" Christoph Rabel" < OD ** @ hal9000.vc-graz.ac.at>在留言新闻中写道:3f *********************** @ aconews.univie.ac.a t ...
基数(0); //编译好,但是
这可能不符合你的想法。这将创建一个临时对象
类型Base(用0初始化)。正如您可能期望的那样,它不是对curent对象上的Base类构造函数的调用
。
Base :: Base(0); //不编译,Comeau说:
此行无效。 Base :: Base在这种情况下根本无效。
Comeau'的错误信息有点迟钝,但因为它不是有效的
构造它只是试着去试一下它认为你想要做的事情。
你真正想要的是:
派生(int i):Base(0){}
" Jakob Bieling" <是ne ***** @ gmy.net>在消息新闻中写道:bj ************* @ news.t-online.com ...>基::碱(0); //不编译,Comeau说:
我不知道为什么它说你不应该拿地址,因为我会想到这条线上面的意思是打电话给c''tor,它无法正常工作。
不能这样,因为施工人员没有名。 Base :: Base
这里不是一个有效的序列。
Hi!
I tried to compile the following code with Comeau, but it gave me a
puzzling error(The code makes no sense, it is just to show what puzzles me)
class Base
{
public:
Base();
Base(int);
};
class Derived : public Base
{
public:
Derived(int i)
{
Base(0); // Compiles fine, but
Base::Base(0); // Does not compile, Comeau says:
// error: a constructor or destructor may not have its
// address taken
}
};
Is the line valid or not?
greets,
Christoph
"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:3f***********************@aconews.univie.ac.a t...Hi!
I tried to compile the following code with Comeau, but it gave me a
puzzling error(The code makes no sense, it is just to show what puzzles me)
class Base
{
public:
Base();
Base(int);
};
class Derived : public Base
{
public:
Derived(int i)
{
Base(0); // Compiles fine, but
Creates a temporary object of type ''Base''.
Base::Base(0); // Does not compile, Comeau says:
I am not sure why it said you should not take the address, because I
would have thought the line above would mean a call to the c''tor, which
cannot work.
// error: a constructor or destructor may not have its
// address taken
}
};
Is the line valid or not?
Even though I do not quite understand why Comeau prints /that/ error
message, I still think it is invalid.
hth
--
jb
(replace y with x if you want to reply by e-mail)
"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message news:3f***********************@aconews.univie.ac.a t...
Base(0); // Compiles fine, but
This probably does not do what you think. This creates a temporary object of
type Base (initialized with a 0). It is not, as you might be expecting, a call
to the Base class constructor on the curent object.
Base::Base(0); // Does not compile, Comeau says:
This line isn''t valid. Base::Base is not valid at all in this context.
Comeau''s error message is a bit obtuse but since it''s not a valid
construct it''s just trying to take a stab at what it thinks you''re
trying to do.
What you really want is:
Derived(int i) : Base(0) { }
"Jakob Bieling" <ne*****@gmy.net> wrote in message news:bj*************@news.t-online.com...> Base::Base(0); // Does not compile, Comeau says:
I am not sure why it said you should not take the address, because I
would have thought the line above would mean a call to the c''tor, which
cannot work.
Can''t mean that, because constructors don''t have names. Base::Base
just isn''t a valid sequence here.
这篇关于合法与否?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!