在构造函数中将对象初始化为 NULL
是否有可能在调用构造函数时对象也被初始化为 null ?
我的程序中需要它,因为如果在构造中抛出异常,则对象被初始化为 null
我有几个选项 1. 创建一个布尔字段,表示有效或无效以显示其空值。 2.将所有成员对象初始化为null然后返回
但是所有这些实际上并没有将值设置为null只是让它感觉像它的null..
还有什么建议吗?
Is it possible that upon calling a constructor also the object be initialized to null ??
I need it in my program for the condition where if an exception is thrown in construction then object is initialized to null
I've several options for that
1. Creating a boolean field saying valid or not to show its null..
2. Initialzing all member objects to null and then returning
But all of these don't acutually set the value to null just make it feel like its null..
Any more suggestion ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说,如果无失败构造函数不可行,那么最好抛出异常并让调用代码按照他们认为合适的方式使指针无效。
I would say if a no-fail constructor isn't feasible, it's probably better to throw the exception and let the calling code invalidate the pointer how they see fit.