在这里要求集体智慧的意见 [英] asking for an opinion from the collective wisdom here

查看:57
本文介绍了在这里要求集体智慧的意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道这是不是形成错误的班级标题:


#ifndef _items_h_

#define _items_h_

#include< iostream>

#include< string>

#include< vector>


使用命名空间std;


class项目

{

公共:

项目(浮动重量) = 0,bool set_can_equip = false,bool

set_can_attack = false,bool set_can_defend = false,bool set_can_drink

= false,bool set_can_eat = false,bool set_can_use = false,bool

set_is_consumable = false,bool set_is_magical = false);

//以下是此

类中所有变量的set和get函数br />
void set_weight(浮动重量){item_weight = weight; } $ / $
float get_weight(){return item_weight; }


void set_can_equip(bool set_can_equip){can_equip =

set_can_equip; }

bool get_can_equip(){return can_equip; }


void set_can_attack(bool set_can_attack){can_attack =

set_can_attack; }

bool get_can_attack(){return can_attack; }


void set_can_defend(bool set_can_defend){can_defend =

set_can_defend; }

bool get_can_defend(){return can_defend; }


void set_can_drink(bool set_can_drink){can_drink =

set_can_drink; }

bool get_can_drink(){return can_drink; }


void set_can_eat(bool set_can_eat){can_eat = set_can_eat; }

bool get_can_eat(){return can_eat; }


void set_can_use(bool set_can_use){can_use = set_can_use; }

bool get_can_use(){return can_use; }


void set_is_consumable(bool set_is_consumable){is_consumable

= set_is_consumable; }

bool get_is_consumable(){return is_consumable; }


void set_is_magical(bool set_is_magical){is_magical =

set_is_magical; }

bool get_is_magical(){return is_magical; }


void describe(); //简单地返回描述(见下文)

void clear_describe(); //清除描述

void add_describe(string line_to_add);

/ *上面的行检查描述是否设置为

need_set或desc_cleared,如果是这样description.clear()

然后push_back(line_to_add),否则只是push_back(line_to_add)* /


private://这些是将为每个创建的项目设置的统计数据

string need_set; //用这个初始化描述

string desc_cleared; //描述将在此之后设置为

已被清除

vector< stringdescription; //这是允许多行

描述

float item_weight; //这些都是非常自我解释我认为

bool can_equip;

bool can_attack;

bool can_defend;

bool can_drink;

bool can_eat;

bool can_use;

bool is_consumable;

bool is_magical;

};


#endif


但是当我读到的时候,我对某些事感到困惑。上面的工作只是

罚款,但我也想知道构造函数应该是这样的:


项目(浮动权重= 0,bool set_can_equip = false,bool set_can_attack

= false,bool set_can_defend = false,bool set_can_drink = false,bool

set_can_eat = false,bool set_can_use = false,bool set_is_consumable =

false,bool set_is_magical = false);


在cpp文件中有这样的构造函数体:


Item :: Item(float重量,bool set_can_equip,bool set_can_attack,bool

set_can_defend,bool set_can_drink,bool set_can_eat,bool set_can_use)

{

item_weight = weight;

can_equip = set_can_equip;

can_attack = set_can_attack;

can_defend = set_can_defend;

can_drink = set_can_drink;

can_eat = set_can_eat;

can_use = set_can_use;

is_consumable = set_is_consumable;

is_magical = set_is_magical;

need_set ="你需要在这里设置描述!\ n" ;;

desc_cleared ="说明已被清除。\ n" ;;

description.clear();

description.push_back(need_set);

}


或者这个:


Item():浮动权重(0),bool can_equip(false),bool can_attack(

false),bool can_defend(false),bool can_drink( false),bool

can_eat(false),bool can_use(false),bool is_consumable(false),

bool is_magical(false);

并丢失cpp文件中构造函数的整个主体?当我实例化

对象时,我将需要

来传递参数以覆盖默认值。


问题1:这是一个格式错误的类标题?

问题2:我应该将哪个用于初始化列表?


提前致谢。

DN

-

[我的电子邮件中没有x']


我有权留下沉默

(并且应该尽可能多地使用它)

我打字的任何东西都可以用来对付我

在法庭上白痴

我有权利出错

(可能是)

如果我不能提出自己的错误

我相信有人会为我提供。

解决方案

Devon Null写道:


我只是想知道这是不是格式错误的类头:


#ifndef _items_h_

#define _items_h_


是的,你在这里是畸形的。根据标准,任何带有前导下划线的标识符

保留给

全局命名空间中的实现。 _items_h_位于全局命名空间中。


Devon Null< th ************* @ xgmailx.comwrote:
< blockquote class =post_quotes>
我想知道构造函数应该是这样的:


项目(浮动权重= 0,bool set_can_equip = false,bool set_can_attack

= false,bool set_can_defend = false,bool set_can_drink = false,bool

set_can_eat = false,bool set_can_use = false,bool set_is_consumable =

false,bool set_is_magical = false);


在cpp文件中有这样的构造函数体:


Item :: Item(float weight,bool set_can_equip, bool set_can_attack,bool

set_can_defend,bool set_can_drink,bool set_can_eat,bool set_can_use)

{

item_weight = weight;

can_equip = set_can_equip;

can_attack = set_can_attack;

can_defend = set_can_defend;

can_drink = set_can_drink;

can_eat = set_can_eat;

can_use = set_can_use;

is_consumable = set_is_consumable;

is_magical = set_is_magical;

need_set = 你需要在这里设置描述!\ n" ;;

desc_cleared ="说明已被清除。\ n" ;;

description.clear( );

description.push_back(need_set);

}


或者这个:


Item():float weight(0),bool can_equip(false),bool can_attack(

false),bool can_defend(false),bool can_drink(false),bool

can_eat(false),bool can_use(false),bool is_consumable(false),

bool is_magical(false);


并输掉cpp文件中构造函数的整个主体?当我实例化

对象时,我将需要

来传递参数以覆盖默认值。



你不能丢失构造函数的整个主体。但是要回答

基础问题,你应该使用无参数构造函数,只需要

初始化所有变量。


项目:: Item():

weight(0),

can_equip(false),

can_attack(false),

can_defend(false),

can_drink(false),

can_eat(false),

can_use(false),
is_consumable(false),

is_magical(false)

{}


在某些时候你会意识到您的代码中充满了块,

检查此类中的各种标志,并根据标志设置的不同行为取决于

。然后当你了解多态性时,你就会开始编写更好的类。


6月3日凌晨1点06分,Daniel T " < danie ... @ earthlink.netwrote:


Devon Null< theronnights ... @ xgmailx.comwrote:


我想知道构造函数应该是这样的:


Item(float weight = 0,bool set_can_equip = false,bool set_can_attack

= false,bool set_can_defend = false,bool set_can_drink = false,bool

set_can_eat = false,bool set_can_use = false,bool set_is_consumable =

false ,bool set_is_magical = false);


在cpp文件中有这样的构造函数体:


Item :: Item(浮动权重,bool set_can_equip,bool set_can_attack,bool

set_can_defend,bool set_can_drink,bool set_can_eat,bool set_can_use)

{

item_weight = weight;

can_equip = set_can_equip;

can_attack = set_can_attack;

can_defend = set_can_defend;

can_drink = set_can_drink;

can_eat = set_can_eat;

can_use = set_can_use;

is_consumable = set_is_consumable;

is_magical = set_is_magical;

need_set ="你需要在这里设置一个描述!\ n" ;;

desc_cleared ="描述已被清除。\ n" ;;

description.clear();

description.push_back(need_set);

}


或者这个:


Item(): float weight(0),bool can_equip(false),bool can_attack(

false),bool can_defend(false),bool can_drink(false),bool

can_eat(false ),bool can_use(false),bool is_consumable(false),

bool is_magical(false);


并丢失cpp文件中构造函数的整个主体?当我实例化

对象时,我将需要

来传递参数以覆盖默认值。



你不能丢失构造函数的整个主体。但是要回答

基础问题,你应该使用无参数构造函数,只需要

初始化所有变量。


项目:: Item():

weight(0),

can_equip(false),

can_attack(false),

can_defend(false),

can_drink(false),

can_eat(false),

can_use(false),
is_consumable(false),

is_magical(false)

{}


在某些时候你会意识到您的代码中充满了块,

检查此类中的各种标志,并根据标志设置的不同行为取决于

。然后,当你了解多态性时,你就会开始编写更好的类.-隐藏引用文本 -


- 显示引用文本 -



这两种形式都可以用于ctors,但是当const

数据成员存在时,后者会更好,当成员必须是$
时需要前者b $ b指定相关值:


int f(int,int); //一个大函数


A类{

public:

const int m;

int n,p;

A(int i,int j):

p(0),

m(f(i,j))// const必须在这里初始化。

{n = m;}; / *如果n在初始化列表中被初始化f被称为

两倍,这个时间很贵。* /

};


I was simply wondering if this was a mal-formed class header:

#ifndef _items_h_
#define _items_h_

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Item
{
public:
Item( float weight = 0, bool set_can_equip = false, bool
set_can_attack = false, bool set_can_defend = false, bool set_can_drink
= false, bool set_can_eat = false, bool set_can_use = false, bool
set_is_consumable = false, bool set_is_magical = false );
//Below are the set and get functions for all variables in this
class
void set_weight( float weight ) { item_weight = weight; }
float get_weight() { return item_weight; }

void set_can_equip( bool set_can_equip ) { can_equip =
set_can_equip; }
bool get_can_equip() { return can_equip; }

void set_can_attack( bool set_can_attack ) { can_attack =
set_can_attack; }
bool get_can_attack() { return can_attack; }

void set_can_defend( bool set_can_defend ) { can_defend =
set_can_defend; }
bool get_can_defend() { return can_defend; }

void set_can_drink( bool set_can_drink ) { can_drink =
set_can_drink; }
bool get_can_drink() { return can_drink; }

void set_can_eat( bool set_can_eat ) { can_eat = set_can_eat; }
bool get_can_eat() { return can_eat; }

void set_can_use( bool set_can_use ) { can_use = set_can_use; }
bool get_can_use() { return can_use; }

void set_is_consumable( bool set_is_consumable ) { is_consumable
= set_is_consumable; }
bool get_is_consumable() { return is_consumable; }

void set_is_magical( bool set_is_magical ) { is_magical =
set_is_magical; }
bool get_is_magical() { return is_magical; }

void describe(); // simply returns description (see below)
void clear_describe(); // clears description
void add_describe( string line_to_add );
/*Line above checks to see if description is set to either
need_set or desc_cleared, and if so description.clear()
then push_back( line_to_add ), else just push_back( line_to_add)*/

private: // These are stats that will be set for every item created
string need_set; // description is initialized with this
string desc_cleared; // description will be set to this after it
has been cleared
vector<stringdescription; // This is to allow multi-line
descriptions
float item_weight; // These are pretty self explanatory I think
bool can_equip;
bool can_attack;
bool can_defend;
bool can_drink;
bool can_eat;
bool can_use;
bool is_consumable;
bool is_magical;
};

#endif

but as I read along I was confused over something. The above works just
fine, but I was also wondering should the constructor be this:

Item( float weight = 0, bool set_can_equip = false, bool set_can_attack
= false, bool set_can_defend = false, bool set_can_drink = false, bool
set_can_eat = false, bool set_can_use = false, bool set_is_consumable =
false, bool set_is_magical = false );

with a constructor body like this in the cpp file:

Item::Item( float weight, bool set_can_equip, bool set_can_attack, bool
set_can_defend, bool set_can_drink, bool set_can_eat, bool set_can_use )
{
item_weight = weight;
can_equip = set_can_equip;
can_attack = set_can_attack;
can_defend = set_can_defend;
can_drink = set_can_drink;
can_eat = set_can_eat;
can_use = set_can_use;
is_consumable = set_is_consumable;
is_magical = set_is_magical;
need_set = "You need to set a description here!\n";
desc_cleared = "The description has been cleared.\n";
description.clear();
description.push_back( need_set );
}

or this:

Item() : float weight( 0 ), bool can_equip( false ), bool can_attack(
false ), bool can_defend( false ), bool can_drink( false ), bool
can_eat( false ), bool can_use( false ), bool is_consumable( false ),
bool is_magical( false );

and lose the entire body of the constructor in the cpp file? I will need
to pass in parameters to override the defaults when I instantiate the
objects.

Question 1: Is this a mal-formed class header?
Question 2: Which should I use for the initialization list?

Thanks in advance.
DN
--
[there are no x''s in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I''m sure someone will provide it for me.

解决方案

Devon Null wrote:

I was simply wondering if this was a mal-formed class header:

#ifndef _items_h_
#define _items_h_

Yes, you are malformed right here. Per the Standard, any identifier
with a leading underscore is reserved to the implementation in the
global namespace. _items_h_ is in the global namespace.


Devon Null <th*************@xgmailx.comwrote:

I was wondering should the constructor be this:

Item( float weight = 0, bool set_can_equip = false, bool set_can_attack
= false, bool set_can_defend = false, bool set_can_drink = false, bool
set_can_eat = false, bool set_can_use = false, bool set_is_consumable =
false, bool set_is_magical = false );

with a constructor body like this in the cpp file:

Item::Item( float weight, bool set_can_equip, bool set_can_attack, bool
set_can_defend, bool set_can_drink, bool set_can_eat, bool set_can_use )
{
item_weight = weight;
can_equip = set_can_equip;
can_attack = set_can_attack;
can_defend = set_can_defend;
can_drink = set_can_drink;
can_eat = set_can_eat;
can_use = set_can_use;
is_consumable = set_is_consumable;
is_magical = set_is_magical;
need_set = "You need to set a description here!\n";
desc_cleared = "The description has been cleared.\n";
description.clear();
description.push_back( need_set );
}

or this:

Item() : float weight( 0 ), bool can_equip( false ), bool can_attack(
false ), bool can_defend( false ), bool can_drink( false ), bool
can_eat( false ), bool can_use( false ), bool is_consumable( false ),
bool is_magical( false );

and lose the entire body of the constructor in the cpp file? I will need
to pass in parameters to override the defaults when I instantiate the
objects.

You can''t lose the entire body of the constructor. However to answer the
underlying question, you should use the no argument constructor and just
initialize all the variables.

Item::Item() :
weight( 0 ),
can_equip( false ),
can_attack( false ),
can_defend( false ),
can_drink( false ),
can_eat( false ),
can_use( false ),
is_consumable( false ),
is_magical( false )
{ }

At some point you will realize that your code is full of blocks that
check the various flags in this class and behave differently depending
on the setting of the flag. Then when you learn about polymorphism, you
will start writing better classes.


On Jun 3, 1:06 am, "Daniel T." <danie...@earthlink.netwrote:

Devon Null <theronnights...@xgmailx.comwrote:

I was wondering should the constructor be this:

Item( float weight = 0, bool set_can_equip = false, bool set_can_attack
= false, bool set_can_defend = false, bool set_can_drink = false, bool
set_can_eat = false, bool set_can_use = false, bool set_is_consumable =
false, bool set_is_magical = false );

with a constructor body like this in the cpp file:

Item::Item( float weight, bool set_can_equip, bool set_can_attack, bool
set_can_defend, bool set_can_drink, bool set_can_eat, bool set_can_use )
{
item_weight = weight;
can_equip = set_can_equip;
can_attack = set_can_attack;
can_defend = set_can_defend;
can_drink = set_can_drink;
can_eat = set_can_eat;
can_use = set_can_use;
is_consumable = set_is_consumable;
is_magical = set_is_magical;
need_set = "You need to set a description here!\n";
desc_cleared = "The description has been cleared.\n";
description.clear();
description.push_back( need_set );
}

or this:

Item() : float weight( 0 ), bool can_equip( false ), bool can_attack(
false ), bool can_defend( false ), bool can_drink( false ), bool
can_eat( false ), bool can_use( false ), bool is_consumable( false ),
bool is_magical( false );

and lose the entire body of the constructor in the cpp file? I will need
to pass in parameters to override the defaults when I instantiate the
objects.


You can''t lose the entire body of the constructor. However to answer the
underlying question, you should use the no argument constructor and just
initialize all the variables.

Item::Item() :
weight( 0 ),
can_equip( false ),
can_attack( false ),
can_defend( false ),
can_drink( false ),
can_eat( false ),
can_use( false ),
is_consumable( false ),
is_magical( false )
{ }

At some point you will realize that your code is full of blocks that
check the various flags in this class and behave differently depending
on the setting of the flag. Then when you learn about polymorphism, you
will start writing better classes.- Hide quoted text -

- Show quoted text -

both forms can be used for ctors but the later is better when const
data members exist and the former is needed when members have to be
assigned assciated values:

int f(int,int);//A big function

class A{
public:
const int m;
int n,p;
A(int i,int j):
p(0),
m(f(i,j))//const must be initialized here.
{n=m;};/*if n is initailized in the initializer list f is called
twice which time expensive.*/
};


这篇关于在这里要求集体智慧的意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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