静态成员对象初始化 [英] static member object initialization
问题描述
考虑以下程序:
#include< iostream>
使用命名空间std;
class Test
{
静态测试t;
静态测试init_Test(){return t; }
测试(const测试& rhs){cout<< copy ctor << ENDL; }
};
测试测试:: t = init_Test();
int main()
{
返回0;
}
这个程序在g ++和VC +下编译都很好+2005快递版
并且两者都产生以下相同的输出
copy ctor
但请考虑声明
测试测试:: t = init_Test();
这里调用init_Test(),返回静态成员对象
正在建设中,这是't'' 。我不明白我们怎么能
返回一个仍在建设的对象。
如何被编译器接受?
请详细解释
谢谢
V.Subramanian
尝试输入一个写入一些输出的默认构造函数。我想
会告诉你发生了什么。此外,你没有返回t
你将返回一份t。
9月25日,下午12:47, subramanian10 ... @ yahoo.com,India"
< subramanian10 ... @ yahoo.comwrote:
考虑以下程序:
#include< iostream>
使用命名空间std;
class Test
{
静态测试t;
静态测试init_Test(){return t; }
测试(const测试& rhs){cout<< copy ctor << ENDL; }
};
测试测试:: t = init_Test();
int main()
{
返回0;
}
此程序在g ++和VC ++ 2005 Express Edition下编译好了
并且两者都产生以下相同的输出
copy ctor
但是考虑声明
测试测试:: t = init_Test();
这里调用init_Test()返回静态成员对象
下这是不的建筑。我不明白我们怎么能
返回一个仍在建设的对象。
如何被编译器接受?
请详细说明
谢谢
V.Subramanian
su ************** @ yahoo.com 写道:
考虑以下程序:
#include< iostream>
>
使用命名空间std;
class测试
{
静态测试t;
static测试init_Test(){return t; }
测试(const测试& rhs){cout<< copy ctor << ENDL; }
};
测试测试:: t = init_Test();
int main()
{
返回0;
}
这个程序在g ++和VC +下编译都很好+2005快递版
并且两者都产生以下相同的输出
copy ctor
但请考虑声明
测试测试:: t = init_Test();
这里调用init_Test(),返回静态成员对象
正在建设中,这是't'' 。我不明白我们怎么能
返回一个仍在建设的对象。
如何被编译器接受?
编译器应该如何捕获*逻辑*错误?
编译器没有义务告诉您逻辑不正确。或者
你有鸡和蛋的问题。编译器只检查
的语法和类型。您的代码具有未定义的行为,因为它
尝试使用未初始化的对象。和
一样:
int& r = r;
我认为编译器是不需要在代码中查找和标记
未定义行为的所有实例。
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要问
su ************* *@yahoo.com ,印度写道:
考虑以下程序:
#include< iostream>
使用命名空间std;
class测试
{
静态测试t;
static测试init_Test(){return t; }
测试(const测试& rhs){cout<< copy ctor << ENDL; }
};
测试测试:: t = init_Test();
int main()
{
返回0;
}
这个程序在g ++和VC +下编译都很好+2005快递版
并且两者都产生以下相同的输出
copy ctor
但请考虑声明
测试测试:: t = init_Test();
这里调用init_Test(),返回静态成员对象
正在建设中,这是't'' 。我不明白我们怎么能
返回一个仍在建设的对象。
如何被编译器接受?
这是惊人的,甚至令人费解的是init_Test()''编译,其中
应该是Test :: init_Test();为了形成良好,我认为
Comeau在线也接受所有案例。
等待大师的解释。
- -
谢谢
Barry
Consider the following program:
#include <iostream>
using namespace std;
class Test
{
static Test t;
static Test init_Test( ) { return t; }
Test(const Test & rhs) { cout << "copy ctor" << endl; }
};
Test Test::t = init_Test( );
int main()
{
return 0;
}
This program compiles fine under both g++ and VC++2005 Express Edition
and both produce the following same output
copy ctor
However consider the statement
Test Test::t = init_Test( );
Here init_Test( ) is called which returns the static member object
under construction which is ''t''. I do not understand how we can
return an object which is still under construction.
How is it accepted by the compiler ?
Kindly explain
Thanks
V.Subramanian
Try putting in a default constructor that writes some output. I think
that will show you what''s going on. Also, you''re not returning t
you''re returning a copy of t.
On Sep 25, 12:47 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:Consider the following program:
#include <iostream>
using namespace std;
class Test
{
static Test t;
static Test init_Test( ) { return t; }
Test(const Test & rhs) { cout << "copy ctor" << endl; }
};
Test Test::t = init_Test( );
int main()
{
return 0;
}
This program compiles fine under both g++ and VC++2005 Express Edition
and both produce the following same output
copy ctor
However consider the statement
Test Test::t = init_Test( );
Here init_Test( ) is called which returns the static member object
under construction which is ''t''. I do not understand how we can
return an object which is still under construction.
How is it accepted by the compiler ?
Kindly explain
Thanks
V.Subramanian
su**************@yahoo.com wrote:Consider the following program:
#include <iostream>
using namespace std;
class Test
{
static Test t;
static Test init_Test( ) { return t; }
Test(const Test & rhs) { cout << "copy ctor" << endl; }
};
Test Test::t = init_Test( );
int main()
{
return 0;
}
This program compiles fine under both g++ and VC++2005 Express Edition
and both produce the following same output
copy ctor
However consider the statement
Test Test::t = init_Test( );
Here init_Test( ) is called which returns the static member object
under construction which is ''t''. I do not understand how we can
return an object which is still under construction.
How is it accepted by the compiler ?How should the compiler be able to catch your *logical* errors? The
compiler is not obligated to tell you your logic is incorrect. Or
that you have a chicken and egg problem. The compiler just checks
the syntax and types. Your code has undefined behaviour since it
makes an attempt to use an uninitialised object. The same thing as
here:
int &r = r;
I think the compiler is not required to find and flag all instances of
undefined behaviour in your code.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
su**************@yahoo.com, India wrote:Consider the following program:
#include <iostream>
using namespace std;
class Test
{
static Test t;
static Test init_Test( ) { return t; }
Test(const Test & rhs) { cout << "copy ctor" << endl; }
};
Test Test::t = init_Test( );
int main()
{
return 0;
}
This program compiles fine under both g++ and VC++2005 Express Edition
and both produce the following same output
copy ctor
However consider the statement
Test Test::t = init_Test( );
Here init_Test( ) is called which returns the static member object
under construction which is ''t''. I do not understand how we can
return an object which is still under construction.
How is it accepted by the compiler ?
This is stunning, and even puzzling that "init_Test()'' compiles, which
should be Test::init_Test(); to be well-formed, I think
Comeau online also accepts all the cases.
Waiting guru for explanation.
--
Thanks
Barry
这篇关于静态成员对象初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!