Miranda功能和初始化 [英] Miranda functions and Initialization

查看:69
本文介绍了Miranda功能和初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这些都是这些吗?:

班级假人

{

公开:


假人(){}


假人(const Dummy& original){}


Dummy& operator =(const Dummy& lhs){}


const Dummy * operator&()const {return this; }


Dummy * operator&(){return this; }


~Dummy(){}


};

也可以有人告诉我情况这些是什么?b $ b编辑并且它们消失了?例如,我已经知道

如下:


1)如果你提供另一个带参数的假人,假人()会消失,例如。 br />
Dummy(int)。


2)operator =并且复制构造函数将为每个额外成员更改

变量''添加到班级。

-


现在,请采取以下措施:


int main()

{

int j;

}

我完全清楚j不包含特定值,它没有被初始化。


现在采取以下措施:


int main()

{

int j = 45;

}


我完全清楚这一点等于:


int j = int(45);


但是,如果你有用户定义的类型,究竟是什么继续?


例如:


SomeClass k = 78.222;

这会变成:


SomeClass k = S. omeClass(78.222);


然后是SomeClass(const SomeClass&)复制构造函数?或者......

编译器是否只是寻找不同的复制构造函数,例如。

SomeClass(const double&)?或者这甚至是有效的吗?


无论如何,这就是我所得到的:我知道int()等于零,即。如果你指定括号,那么它会被初始化为零。现在你有了

的问题:


int main()

{

int j();

}


编译器不知道它是对象定义还是函数

声明(......如果只是extern是强制性的)。因此,它假设一个

函数声明。


是解决这个问题的唯一方法:


int j(0);





无论如何,这里有我的问题:


级巧克力

{

公开:


int a;


double b;


};

int main()

{

巧克力巧克力;

}

做a和b初始化为零?如果是这样,它是什么?它是

miranda默认构造函数吗?在下面的代码中怎么样:


class巧克力

{

public:


int a;


双b;


巧克力(){}


} ;

int main()

{

巧克力巧克力;

}

做a和b在上面初始化为零?

-JKop


Is this them all?:
class Dummy
{
public:

Dummy() {}

Dummy(const Dummy& original) { }

Dummy& operator=(const Dummy& lhs) {}

const Dummy * operator&() const { return this; }

Dummy * operator&() { return this; }

~Dummy() {}

};
Also could anyone please inform me of circumstances in which these are
edited and in which they disappear? For example I''m already aware of the
following:

1) Dummy() disappears if you supply another one that takes arguments, eg.
Dummy(int).

2) operator= and the copy constructor will change for every extra member
variable that''s added to the class.
--

Now, take the following:

int main()
{
int j;
}
I''m fully aware that j contains no particular value, it hasn''t been
initialized.

Now take the following:

int main()
{
int j = 45;
}

I''m fully aware that that''s equal to:

int j = int(45);

But, where you have user-defined types, what exactly goes on?

For instance:

SomeClass k = 78.222;
Does that become:

SomeClass k = SomeClass(78.222);

And then is the SomeClass(const SomeClass&) copy constructor called? Or...
does the compiler simply look for a different copy constructor, eg.
SomeClass(const double&)? Or is that even valid?

Anyway here''s what I''m getting at: I know int() is equal to zero, ie. if you
specify brackets then it gets intialized to zero. Now ofcourse you have the
problem of:

int main()
{
int j();
}

The compiler doesn''t know if it''s an object definition or a function
declaration (...if only "extern" was compulsory). As such, it assumes a
function declaration.

Is the only way to get around this to write:

int j(0);

?

Anyway here comes my question:

class Chocolate
{
public:

int a;

double b;

};
int main()
{
Chocolate choco;
}
Do "a" and "b" get initialized to zero? And if so, what does it? Is it the
miranda default constructor? What about in the following code:

class Chocolate
{
public:

int a;

double b;

Chocolate() {}

};
int main()
{
Chocolate choco;
}
Do "a" and "b" get initialized to zero in the above?
-JKop

推荐答案

>
无论如何这是我的问题:

类巧克力
{
公开:

int a;

双b;

};

int main()
{
巧克力巧克力;
}

做a和b初始化为零?


不,编译器生成的默认ctor保留未标记的标量类型。

如果是这样,它是什么?它是
miranda的默认构造函数吗?在下面的代码中怎么样:

类巧克力
{
公开:

int a;

双b;

巧克力(){}

};

int main()
{
巧克力巧克力; }

做a和b在上面初始化为零?
Anyway here comes my question:

class Chocolate
{
public:

int a;

double b;

};
int main()
{
Chocolate choco;
}
Do "a" and "b" get initialized to zero?
No, the compiler generated default ctor leaves scalar types uninitalised.
And if so, what does it? Is it the
miranda default constructor? What about in the following code:

class Chocolate
{
public:

int a;

double b;

Chocolate() {}

};
int main()
{
Chocolate choco;
}
Do "a" and "b" get initialized to zero in the above?




再次没有。您编写的默认ctor与编译器相同

生成默认ctor,它会使a和b保持未初始化状态。


你可以写这个


巧克力巧克力=巧克力();


在巧克力的第一个定义中将a和b归为零,而你给的是
但是不在第二个。巧克力()是值初始化,它与默认初始化不同,因为它在没有用户声明ctor的类中初始化标量

类型。

john



Again no. The default ctor you wrote is identical to the compiler
generated default ctor, it leaves a and b uninitialised.

You can write this

Chocolate choco = Chocolate();

which would zero initalise a and b in the first definition of Chocolate
you gave but not in the second. Chocolate() is value initialsation which
differs from default initialisation in that it zero initialises scalar
types in a class without a user declared ctor.

john


JKop写道:
JKop wrote:

这些都是吗?:

class Dummy
{
公开:

假人(){}

假人(const Dummy& original){}

Dummy& operator =(const Dummy& lhs){}
const Dummy * operator&()const {return this; } * />
Dummy * operator&(){return this; }

〜Dummy(){}

};

也有人可以通知我这些情况已被编辑他们消失了?例如,我已经知道了以下内容:

1)如果你提供另一个带参数的假人,那么Dummy()会消失,例如。假(INT)。


如果您编写一个

用户定义的编译器,编译器将不会为您生成默认构造函数,如果这就是您的意思。 br />
2)operator =并且复制构造函数将针对添加到类中的每个额外
成员变量进行更改。


他们只是做成员复制。所以,是的,你可以说当你向你的班级添加成员时他们会改变



-

现在,请采取以下措施: br />
int main()
{
int j;
}

我完全清楚j不包含任何特定值,它没有被初始化。

现在采取以下措施:

int main()
{
int j = 45;
}

我完全清楚这等于:

int j = int(45);
但是,如果您有用户定义的类型,究竟会发生什么?

例如:

SomeClass k = 78.222;

这样做成为:

SomeClass k = SomeClass(78.222);


是。

那么SomeClass(const SomeClass&)复制构造函数是否被调用?


是。

或者...编译器只是寻找不同的复制构造函数,例如。 SomeClass(const double&)?


这不是复制构造函数,而是转换构造函数。是的,

需要一个,两个。首先,类的无名临时对象

SomeClass是使用该构造函数创建的,然后k是复制构造的

。但是请注意,C ++标准给出了明确的

权限,可以直接从double值创建k,省略了

临时,但即使从不调用复制构造函数,它也是如此仍然

必须可用。但是,如果你写了:


SomeClass k(78.222);


只需要来自double的转换构造函数和副本

构造函数不参与。

或者甚至有效吗?

无论如何这里是我得到的:我知道int( )等于零,即。
如果你指定括号,那么它初始化为零。


是的。这个构造函数式初始化可以在

模板中派上用场。

现在你遇到的问题是:

int main()
{
int j();
}

编译器不知道它是一个对象定义还是一个函数
声明(。 ..如果只有extern是强制性的)。因此,它假设一个函数声明。


好​​吧,上面的__是一个函数声明。

是解决这个问题的唯一方法:

int j (0);




不可以。你可以这样做:


int j = int();


但是通常,我只会写得更直观:


int j = 0;

无论如何,这里有我的问题:
类巧克力
{
公开:

int a;

双b;

};

int main()
{
巧克力巧克力;
}

做a和b初始化为零?


否。他们保持未初始化状态。

如果是这样,它是什么?它是miranda默认构造函数吗?


这是(空的)编译器生成的默认构造函数。

以下代码怎么样:

类巧克力
{
公开:

int b;

巧克力(){}

};

int main()
{
巧克力巧克力;
}

做a和b在上面初始化为零?

Is this them all?:
class Dummy
{
public:

Dummy() {}

Dummy(const Dummy& original) { }

Dummy& operator=(const Dummy& lhs) {}

const Dummy * operator&() const { return this; }

Dummy * operator&() { return this; }

~Dummy() {}

};
Also could anyone please inform me of circumstances in which these are
edited and in which they disappear? For example I''m already aware of
the following:

1) Dummy() disappears if you supply another one that takes arguments,
eg. Dummy(int).
The compiler won''t generate a default constructor for you if you write a
user defined one, if that''s what you mean.
2) operator= and the copy constructor will change for every extra
member variable that''s added to the class.
They just do a memberwise copy. So yes, you could say that they change
when you add members to your class.
--

Now, take the following:

int main()
{
int j;
}
I''m fully aware that j contains no particular value, it hasn''t been
initialized.

Now take the following:

int main()
{
int j = 45;
}

I''m fully aware that that''s equal to:

int j = int(45);

But, where you have user-defined types, what exactly goes on?

For instance:

SomeClass k = 78.222;
Does that become:

SomeClass k = SomeClass(78.222);
Yes.
And then is the SomeClass(const SomeClass&) copy constructor called?
Yes.
Or... does the compiler simply look for a different copy constructor,
eg. SomeClass(const double&)?
That''s not a copy constructor, but a conversion constructor. And yes,
that one is needed, two. First, a nameless temporary object of class
SomeClass is created using that constructor, then k is copy-constucted
from that one. Note however, that the C++ standard gives explicit
permission to directly create k from the double value, omiting the
temporary, but even if the copy constructor is never called, it still
has to be available. If, however, you write:

SomeClass k(78.222);

only the conversion constructor from double is needed and the copy
constructor is not involved.
Or is that even valid?

Anyway here''s what I''m getting at: I know int() is equal to zero, ie.
if you specify brackets then it gets intialized to zero.
Yes. This constructor-style initialization can come in handy in
templates.
Now ofcourse you have the problem of:

int main()
{
int j();
}

The compiler doesn''t know if it''s an object definition or a function
declaration (...if only "extern" was compulsory). As such, it assumes
a function declaration.
Well, the above _is_ a function declaration.
Is the only way to get around this to write:

int j(0);

?
No. You can do:

int j = int();

But usually, I''d just write the more intuitive:

int j = 0;
Anyway here comes my question:

class Chocolate
{
public:

int a;

double b;

};
int main()
{
Chocolate choco;
}
Do "a" and "b" get initialized to zero?
No. They stay uninitialized.
And if so, what does it? Is it the miranda default constructor?
It''s the (empty) compiler-generated default constructor.
What about in the following code:

class Chocolate
{
public:

int a;

double b;

Chocolate() {}

};
int main()
{
Chocolate choco;
}
Do "a" and "b" get initialized to zero in the above?




你没有写任何代码来初始化a或b,所以他们没有得到

初始化。



You didn''t write any code to initialze a or b, so they don''t get
initialized.




#include< string>


班级奶酪

{

public:


int a;

double b;

std :: string name;

};

int main()

{

奶酪鼠标;

}

所以在上面,a和b包含白噪声,而名称包含白噪声。已经正确构建了




有什么方法可以将函数声明变成对象

定义?!


我看到我能做到:


奶酪鼠标=奶酪();


是那个*唯一*方式?


所以无论如何...



struct SETTINGS

{

int AMOUNT_PORTS;

bool ANY_OPEN;

unsigned char * SET_ID;

};

in他们拥有的代码:

SETTINGS设置;

memset(& sets,0,sizeof(sets));


所以可以写一下:


SETTINGS sets = SETTINGS();


我的另一个方法是,给出任何指针变量他们是正确的

空指针值,而不是只有0.

BTW,我也是jus最近注意到你可以做到以下几点:


extern void Blah(unsigned long);

extern void Blah(unsigned);

extern void Blah(unsigned short);


int main()

{

Blah(无符号短(52U) ));

}

欢乐时光!

-JKop

#include <string>

class Cheese
{
public:

int a;
double b;
std::string name;
};
int main()
{
Cheese mouse;
}
So in the above, "a" and "b" contain white noise, while "name" has been
constructed properly.

Is there any way I can turn the function declaration into an object
definition?!

I see that I can do:

Cheese mouse = Cheese();

Is that the *only* way?

So anyway...

I see in a lot of Win32 documentation that when they have a structure that''s
to be passed to a Win32 function, like:

struct SETTINGS
{
int AMOUNT_PORTS;
bool ANY_OPEN;
unsigned char* SET_ID;
};
that in the code they have:
SETTINGS sets;
memset(&sets,0,sizeof(sets));

So instead of that can one write:

SETTINGS sets = SETTINGS();

Another thing with my way, any pointer variables are given they''re proper
null pointer value, as opposed to just 0.
BTW, I''ve also just noticed lately that you can do the following:

extern void Blah(unsigned long);
extern void Blah(unsigned);
extern void Blah(unsigned short);

int main()
{
Blah(unsigned short(52U));
}
Happy Days!
-JKop


这篇关于Miranda功能和初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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