I''ve got a base class foo:class foo {....};And a derived class foo_d:class foo_d : public foo {};I then make another class which takes a fooin the constructor:class bar {public:bar(foo&);};All easy stuff... but when I try this the compilercomplains:bar b(foo_d());OTOH this is ok:foo_d f;bar b(f);Why...?--<\___/>/ O O \\_____/ FTB. For email, remove my socks.In science it often happens that scientists say, ''You knowthat''s a really good argument; my position is mistaken,''and then they actually change their minds and you neverhear that old view from them again. They really do it.It doesn''t happen as often as it should, because scientistsare human and change is sometimes painful. But it happensevery day. I cannot recall the last time something likethat happened in politics or religion.- Carl Sagan, 1987 CSICOP keynote address 解决方案 fungus <um*****@SOCKSartlum.comwrote:class foo {...};class foo_d : public foo {};class bar {public: bar(foo&);};complains: bar b(foo_d());OTOH this is ok: foo_d f; bar b(f);Why...?In the first case, you create a temporary ''foo_d'' object and want topass this to the ctor of ''bar''. The problem is, that temporaries canonly be bound to _const_ references. But the ''bar'' ctor takes anon-const reference. In the second case you do not have this problem,because there is no temporary.You can fix this by changing the constructor of ''bar'' to take a''foo_d const&''.hth--jb(reply address in rot13, unscramble first)fungus wrote:>All easy stuff... but when I try this the compilercomplains: bar b(foo_d());OTOH this is ok: foo_d f; bar b(f);Why...?You can''t bind rvalues to non-const references.Make the constructorbar(const foo&);Jakob Bieling wrote:>>....complains: bar b(foo_d());OTOH this is ok: foo_d f; bar b(f);Why...? You can fix this by changing the constructor of ''bar'' to take a''foo_d const&''.I thought of that.... but when I try it I get adifferent error:test.cpp: error: ?foo::foo(const foo&)? is privateIn constructor ?bar::bar()?warning: synthesized method ?foo_d::foo_d(const foo_d&)?first required here(and yes, foo::foo(const foo&) is declared private...)--<\___/>/ O O \\_____/ FTB. For email, remove my socks.In science it often happens that scientists say, ''You knowthat''s a really good argument; my position is mistaken,''and then they actually change their minds and you neverhear that old view from them again. They really do it.It doesn''t happen as often as it should, because scientistsare human and change is sometimes painful. But it happensevery day. I cannot recall the last time something likethat happened in politics or religion.- Carl Sagan, 1987 CSICOP keynote address 这篇关于为什么不编译......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 16:24