关于传递给构造函数的对象的问题 [英] question about objects passed into constructors

查看:77
本文介绍了关于传递给构造函数的对象的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我有一个关于将对象传递给构造函数的问题。


我如何检查以确保对象是好的/有效的,即

被传递给构造函数。


显然我可以检查它是否为非NULL,但仍然会

不一定告诉我任何事情。


例如:


/ *这里是一辆构造型汽车引擎类作为

参数。但是,我最好检查以确保引擎有效

所以我该怎么做?

* /

Car :: Car(引擎*引擎)

{


//检查以确保引擎有效。

}

提前感谢您的帮助,

John

解决方案

文章< j2 ***** ***************************@4ax.com> ;, fi ****** @ airmail.net 写道:

我如何检查以确保该对象是好的/有效的
被传递给构造函数。

显然我可以查看它是否为非NULL,但仍然不会告诉我任何事情。
例如:

/ *这里是一个构造函数汽车,它将引擎类作为
参数。但是,我最好检查以确保引擎有效
所以我该怎么做?
* /




你的意思是什么? "有效" ? Engine类应该负责确保引擎对象不是无效。


因为你传入一个指针,你需要检查空指针。


如果指针不为空,则调用者有责任传入有效的

指针。没有办法检查指针是否是野性的。是不是。


干杯,

-

Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/


<音响****** @ airmail.net>在消息中写道

新闻:j2 ******************************** @ 4ax.com ...

您好,

我有一个关于将对象传递给构造函数的问题。

我如何检查以确保对象是好/有效的,是传递给构造函数的。

显然我可以查看它是否为非NULL,


除非它是指针,否则没有什么可以检查的,明智的。

但是仍然不会告诉我任何事情。

例如:

/ *这里是一个构造函数汽车,它将引擎类作为
参数。但是,我最好检查以确保引擎有效
所以我该怎么做?
* /
Car :: Car(Engine * engine)
{

//检查以确保引擎有效。




如果有一个成员服务,你将不得不询问Engine对象

目的。除了检查对象

占用的假设内存是否是真实内存,这是系统特定的事情而且OT在这里,

没有通用的方法来判断是否对象有效。如果发动机本身

不能告诉你它是有效的,那么其他任何东西都没有。


DW


fi******@airmail.net 写道:

我有一个关于将对象传递给构造函数的问题。

我如何检查以确保
传递给构造函数的对象好/有效。

显然我可以查看它是否为非NULL
但是这仍然无法告诉我任何事情。



class Engine {

private:

//表示

const

unsigned int有效;

public:

//函数

bool valid(void)const {

返回0x55555555 ==有效;

}

//构造函数

引擎(无效):有效(0x55555555){}

~Engine(无效){有效期= 0xAAAAAAAA; }

};

例如:

//这是一个构造型车
//将引擎类作为一个引擎类争论。
//我最好检查以确保引擎有效。
//我该怎么做?

Car :: Car(const引擎和引擎) {
//检查以确保引擎有效。
if(engine.valid()){
//初始化* this。
}
else { / *处理错误。 * /}
}




Hello,

I have a question about passing an object into a constructor.

How can I check to make sure that the object is good/valid that is
being passed into the constructor.

Obviously I could check to see if it is non-NULL, but that still will
not necessarily tell me anything.

For example:

/* here is a constructor car that takes an engine class as an
argument. But, I better check to make sure that engine is valid
so how do I do that?
*/
Car::Car( Engine* engine )
{

// check to make sure that engine is valid.
}
Thanks in advance for any help,
John

解决方案

In article <j2********************************@4ax.com>, fi******@airmail.net wrote:

How can I check to make sure that the object is good/valid that is
being passed into the constructor.

Obviously I could check to see if it is non-NULL, but that still will
not necessarily tell me anything.

For example:

/* here is a constructor car that takes an engine class as an
argument. But, I better check to make sure that engine is valid
so how do I do that?
*/



What do you mean by "valid" ? The Engine class should be responsible for making
sure that Engine objects are not "invalid".

Since you are passing in a pointer, you need to check for a null pointer.

If the pointer is not null, it''s the callers responsibility to pass in a valid
pointer. There''s no way to check whether a pointer is "wild" or not.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/


<fi******@airmail.net> wrote in message
news:j2********************************@4ax.com...

Hello,

I have a question about passing an object into a constructor.

How can I check to make sure that the object is good/valid that is
being passed into the constructor.

Obviously I could check to see if it is non-NULL,
Unless it''s a pointer there''s nothing to check, NULL-wise.
but that still will
not necessarily tell me anything.

For example:

/* here is a constructor car that takes an engine class as an
argument. But, I better check to make sure that engine is valid
so how do I do that?
*/
Car::Car( Engine* engine )
{

// check to make sure that engine is valid.



You''ll have to ask the Engine object, if it has a member that serves that
purposes. Other than checking whether the supposed memory the object
occupies is real memory, which is a system-specific matter and OT here,
there is no general way to tell if an object is valid. If the Engine itself
can''t tell you it''s valid, nothing else can.

DW


fi******@airmail.net wrote:

I have a question about passing an object into a constructor.

How can I check to make sure that
the object that is being passed into the constructor is good/valid.

Obviously I could check to see if it is non-NULL
but that still will not necessarily tell me anything.

class Engine {
private:
// representation
const
unsigned int Valid;
public:
//functions
bool valid(void) const {
return 0x55555555 == Valid;
}
// constructors
Engine(void): Valid(0x55555555) { }
~Engine(void) { Valid = 0xAAAAAAAA; }
};
For example:

// Here is a constructor car
// that takes an engine class as an argument.
// I had better check to make sure that engine is valid.
// How do I do that?

Car::Car(const Engine& engine) {
// check to make sure that engine is valid.
if (engine.valid()) {
// Initialize *this.
}
else { /* Handle error. */ }
}




这篇关于关于传递给构造函数的对象的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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