只读,而不是const成员 [英] Read-only, as opposed to const member

查看:84
本文介绍了只读,而不是const成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我读到我的第一本C ++书时,我得到了以下场景:


class Cheese

{

公开:


int number_of_holes;


int color;


};

问题是,你想要用户这个类能够从

中读取两个上面的变量,但是无法改变它们。这本书延迟给了

以下内容:


类奶酪

{

私人:


int number_of_holes;


int color;


public:


int GetNumberOfHoles(void);


int GetColour(void);

};


您如何看待以下内容:

class奶酪

{

私人:


int number_of_holes;


int color;


public:


const int& GetNumberOfHoles(无效)

{

返回number_of_holes;

}


const int& GetColour(无效)

{

返回颜色;

}


};


int main(无效)

{

奶酪粉笔;


chalk.GetNumberOfHoles( );

}

我不知道有这么多书认为他们必须向你展示愚蠢的方式

他们给你精益,意味着高效的方式。

-JKop

Back when I read my first C++ book, I was given the following scenario:

class Cheese
{
public:

int number_of_holes;

int colour;

};
The thing is, you want the "user" of this class to be able to read from the
two above variables, but no be able to change them. The book retardly gave
the following:

class Cheese
{
private:

int number_of_holes;

int colour;

public:

int GetNumberOfHoles(void);

int GetColour(void);
};

What do yous think of the following:
class Cheese
{
private:

int number_of_holes;

int colour;

public:

const int& GetNumberOfHoles(void)
{
return number_of_holes;
}

const int& GetColour(void)
{
return colour;
}

};

int main(void)
{
Cheese chalk;

chalk.GetNumberOfHoles();
}
I wonder was so many books think they have to show you the stupid way before
they give you the lean, mean, efficient way.
-JKop

推荐答案




更好想法!:

class奶酪

{

私人:


int prv_number_of_holes;


int prv_colour;


public:


const int& number_of_holes;


const int&颜色;


奶酪(无效):number_of_holes(prv_number_of_holes),

颜色(prv_colour)

{

;

}


无效Blah(无效)

{

prv_number_of_holes = 52 ;


prv_colour = 2;

}


};

int main (无效)

{

奶酪粉笔;


TakesInt(chalk.number_of_holes);


chalk.number_of_holes = 52; //编译错误

}

这样,实际的类成员函数和友元函数对这些成员变量有写入访问权限,而用户则是用户。只有只读

访问权限!

-JKop


An even better idea!:
class Cheese
{
private:

int prv_number_of_holes;

int prv_colour;

public:

const int& number_of_holes;

const int& colour;

Cheese(void) : number_of_holes(prv_number_of_holes),
colour(prv_colour)
{
;
}

void Blah(void)
{
prv_number_of_holes = 52;

prv_colour = 2;
}

};
int main(void)
{
Cheese chalk;

TakesInt(chalk.number_of_holes);

chalk.number_of_holes = 52; //Compile error
}
This way, the actual class member functions and also friend functions have
write-access to these member variables, while the "user" only has read-only
access!
-JKop


在消息< z%**** *************@news.indigo.ie> ;, JKop< NU ** @ NULL.NULL>

写道
In message <z%*****************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
返回当我读到我的第一本C ++书时,给出了以下场景:

类奶酪
{
公开:

int number_of_holes;

int color;

};

问题是,你想要用户这个类能够从上面两个变量中读取,但是无法改变它们。这本书延迟给出了以下内容:

类奶酪
{
私人:

int number_of_holes;
int color;

public:

int GetNumberOfHoles(void);

int GetColour(void);


这两个都应该是const函数,而void是是什么意思。};

您如何看待以下内容:

class奶酪
{
私人:

int number_of_holes;

int color;

public:

const int& GetNumberOfHoles(void)
{
返回number_of_holes;
}

const int& GetColour(无效)
{
返回颜色;
}


我认为这些也应该是const函数,你不需要

" void"。

};

int main(无效)
{
奶酪粉笔;

chalk.GetNumberOfHoles();


毫无意义。你不是说


something = chalk.GetNumberOfHoles();


?有一个重要的区别。}

我不知道有这么多书认为他们必须向你展示愚蠢的方式才能给你精益,平均,高效的方式。
Back when I read my first C++ book, I was given the following scenario:

class Cheese
{
public:

int number_of_holes;

int colour;

};
The thing is, you want the "user" of this class to be able to read from the
two above variables, but no be able to change them. The book retardly gave
the following:

class Cheese
{
private:

int number_of_holes;

int colour;

public:

int GetNumberOfHoles(void);

int GetColour(void);
Both of those should be const functions, and the "void" is unidiomatic.};

What do yous think of the following:
class Cheese
{
private:

int number_of_holes;

int colour;

public:

const int& GetNumberOfHoles(void)
{
return number_of_holes;
}

const int& GetColour(void)
{
return colour;
}
I think those should be const functions, too, and you don''t need the
"void".
};

int main(void)
{
Cheese chalk;

chalk.GetNumberOfHoles();
Pointless. Didn''t you mean

something = chalk.GetNumberOfHoles();

? There''s an important difference.}
I wonder was so many books think they have to show you the stupid way before
they give you the lean, mean, efficient way.




因为它是安全的方式吗?一旦你开始返回引用,

接下来的事情就是尝试返回对temporaries的引用,带来热闹的

后果。


什么你认为你会通过返回参考来获得收益吗?


我怀疑按价值返回int可能会比任何更肥胖或者b $ b效率低于b $ b返回一个const引用。即使你用一个昂贵的复制构造函数替换了一些类,也不要忘记

最终你想要_do_具有什么价值的东西

函数返回,或者为什么要先调用它?这意味着

无论如何在某处或其他地方都可能是复制操作,

无论你是返回值还是引用。鉴于返回

值优化的存在,成本差异可能小于预期的b $ b。


-

Richard Herring



Because it''s the safe way? Once you start returning references, the
next thing is to try returning references to temporaries, with hilarious
consequences.

What do you think you would gain by returning a reference?

I doubt that returning an int by value is likely to be any more obese or
less efficient than returning a const reference. Even if you''d
substituted some class with a costly copy constructor, don''t forget that
ultimately you want to _do_ something with the value of whatever the
function returns, or why call it in the first place? And that means
that somewhere or other there''s likely to be a copy operation anyway,
whether you return a value or a reference. Given the existence of return
value optimisation, the difference in cost may be less than you
expected.

--
Richard Herring


JKop写道:

您如何看待以下内容:

班级奶酪
{
私人:

int number_of_holes;

int color;

公众:

const int& GetNumberOfHoles(void)
{
返回number_of_holes;
}

const int& GetColour(无效)
{
返回颜色;
}

};

int main(无效)
{
奶酪粉笔;

chalk.GetNumberOfHoles();
}
我不知道有这么多书认为他们必须先向你展示愚蠢的方式<他们为您提供精益,平均,高效的方式。

What do yous think of the following:

class Cheese
{
private:

int number_of_holes;

int colour;

public:

const int& GetNumberOfHoles(void)
{
return number_of_holes;
}

const int& GetColour(void)
{
return colour;
}

};

int main(void)
{
Cheese chalk;

chalk.GetNumberOfHoles();
}

I wonder was so many books think they have to show you the stupid way before
they give you the lean, mean, efficient way.




在大多数典型的机器上,这并不比返回更高效

值直接。对于内置类型的每个引用传递更换

a传递值几乎没有意义

如int,double,char等...

-

Karl Heinz Buchegger
kb ****** @ gascad。在


这篇关于只读,而不是const成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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