基于函数为const的重载 [英] overloading based on function being const

查看:89
本文介绍了基于函数为const的重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是初学者的问题。


假设TYPE1和TYPE2是两种类型,其中定义了合适的ctors和

operator =。


假设我有


等级测试

{

TYPE1 mem1;

TYPE2 mem2;


public:

Test& member1(const TYPE1& val1);

Test& member2(const TYPE2& val2);


Test& display(){/ *显示mem值* / return * this; }


const Test& display()const {/ * display mem values * / return

* this; }

};


测试& Test :: member1(const TYPE1& val1)

{

mem1 = val1;

return * this;

}


测试& Test :: member2(const TYPE2& val2)

{

mem2 = val2;

return * this;

}


假设我定义,

TYPE1 val1;

TYPE2 val2;


测试对象;

obj.display()。member1(val1).member2(val2).display();


const测试const_obj;

const_obj.display();


假设display()函数的主体是相同的。那个

是,display()不会修改对象的数据成员。 display()函数的非

提供了display()函数的const版本以便在
上调用
const测试对象。鉴于此,我有以下问题:


1)建议仅在const

(其他所有内容)上重载像display()这样的函数相同的这个功能)用于链接

用途当它不修改对象的数据成员时


2)什么时候重载一个函数,只在const上不可避免?


请说明


谢谢

V.Subramanian

The following is a beginner''s question.

Suppose TYPE1 and TYPE2 are two types for which suitable ctors and
operator= are defined.

Suppose I have

class Test
{
TYPE1 mem1;
TYPE2 mem2;

public:
Test & member1(const TYPE1 & val1);
Test & member2(const TYPE2 & val2);

Test & display( ) { /* display mem values */ return *this; }

const Test & display( ) const { /* display mem values */ return
*this; }
};

Test & Test::member1(const TYPE1 & val1)
{
mem1 = val1;
return *this;
}

Test & Test::member2(const TYPE2 & val2)
{
mem2 = val2;
return *this;
}

Suppose I define,
TYPE1 val1;
TYPE2 val2;

Test obj;
obj.display( ).member1(val1).member2(val2).display( );

const Test const_obj;
const_obj.display( );

Suppose the body of both the display( ) functions are the same. That
is, display( ) does not modify the data members of the object. The non-
const version of display( ) function is provided only for chaining.
The const version of display( ) function is provided to be called on
const Test objects. Given this, I have the following questions:

1)Is it advisable to overload a function like display( ) only on const
(everything else being the same for this function) for chaining
purpose when it does not modify the data members of the object

2) When is overloading a function, only on const inevitable ?

Kindly explain

Thanks
V.Subramanian

推荐答案

su ****** ********@yahoo.com 写道:

以下是初学者的问题。


假设TYPE1和TYPE2是两种类型,其中定义了合适的ctors和

operator =。


假设我有


班级考试

{

TYPE1 mem1;

TYPE2 mem2;


public:

Test& member1(const TYPE1& val1);

Test& member2(const TYPE2& val2);


Test& display(){/ *显示mem值* / return * this; }


const Test& display()const {/ * display mem values * / return

* this; }

};


测试& Test :: member1(const TYPE1& val1)

{

mem1 = val1;

return * this;

}


测试& Test :: member2(const TYPE2& val2)

{

mem2 = val2;

return * this;

}


假设我定义,

TYPE1 val1;

TYPE2 val2;


测试对象;

obj.display()。member1(val1).member2(val2).display();


const测试const_obj;

const_obj.display();


假设display()函数的主体是相同的。那个

是,display()不会修改对象的数据成员。

非const版本的display()函数仅用于

链接。提供了const版本的display()函数,以便在const Test对象上调用
。鉴于此,我有以下

问题:


1)建议仅在const
$上重载像display()这样的函数b $ b(此函数的所有其他内容都相同)用于链接

用途当它不修改对象的数据成员时
The following is a beginner''s question.

Suppose TYPE1 and TYPE2 are two types for which suitable ctors and
operator= are defined.

Suppose I have

class Test
{
TYPE1 mem1;
TYPE2 mem2;

public:
Test & member1(const TYPE1 & val1);
Test & member2(const TYPE2 & val2);

Test & display( ) { /* display mem values */ return *this; }

const Test & display( ) const { /* display mem values */ return
*this; }
};

Test & Test::member1(const TYPE1 & val1)
{
mem1 = val1;
return *this;
}

Test & Test::member2(const TYPE2 & val2)
{
mem2 = val2;
return *this;
}

Suppose I define,
TYPE1 val1;
TYPE2 val2;

Test obj;
obj.display( ).member1(val1).member2(val2).display( );

const Test const_obj;
const_obj.display( );

Suppose the body of both the display( ) functions are the same. That
is, display( ) does not modify the data members of the object. The
non- const version of display( ) function is provided only for
chaining. The const version of display( ) function is provided to be
called on const Test objects. Given this, I have the following
questions:

1)Is it advisable to overload a function like display( ) only on const
(everything else being the same for this function) for chaining
purpose when it does not modify the data members of the object



如果它们完全相同,并且两者都不会尝试修改对象,那么为什么非const版本AT ALL?

If they would do exactly the same, and both would not attempt to
modify the object, why have the non-const version AT ALL?


2)什么时候重载一个函数,只有不可避免的?
2) When is overloading a function, only on const inevitable ?



当你需要不同的行为时。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

When you need different behaviour.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


su ************** @ yahoo.com ,印度写道:
su**************@yahoo.com, India wrote:

以下是初学者的问题。


假设TYPE1和TYPE2是两种类型,其中定义了合适的ctors和

operator =。


假设我有


class Test

{

TYPE1 mem1;

TYPE2 mem2;


public:

测试& member1(const TYPE1& val1);

Test& member2(const TYPE2& val2);


Test& display(){/ *显示mem值* / return * this; }


const Test& display()const {/ * display mem values * / return

* this; }

};


测试& Test :: member1(const TYPE1& val1)

{

mem1 = val1;

return * this;

}


测试& Test :: member2(const TYPE2& val2)

{

mem2 = val2;

return * this;

}


假设我定义,

TYPE1 val1;

TYPE2 val2;


测试对象;

obj.display()。member1(val1).member2(val2).display();


const测试const_obj;

const_obj.display();


假设display()函数的主体是相同的。那个

是,display()不会修改对象的数据成员。 display()函数的非

提供了display()函数的const版本以便在
上调用
const测试对象。鉴于此,我有以下问题:


1)建议仅在const

(其他所有内容)上重载像display()这样的函数相同的这个功能)用于链接

目的,当它不修改对象的数据成员时
The following is a beginner''s question.

Suppose TYPE1 and TYPE2 are two types for which suitable ctors and
operator= are defined.

Suppose I have

class Test
{
TYPE1 mem1;
TYPE2 mem2;

public:
Test & member1(const TYPE1 & val1);
Test & member2(const TYPE2 & val2);

Test & display( ) { /* display mem values */ return *this; }

const Test & display( ) const { /* display mem values */ return
*this; }
};

Test & Test::member1(const TYPE1 & val1)
{
mem1 = val1;
return *this;
}

Test & Test::member2(const TYPE2 & val2)
{
mem2 = val2;
return *this;
}

Suppose I define,
TYPE1 val1;
TYPE2 val2;

Test obj;
obj.display( ).member1(val1).member2(val2).display( );

const Test const_obj;
const_obj.display( );

Suppose the body of both the display( ) functions are the same. That
is, display( ) does not modify the data members of the object. The non-
const version of display( ) function is provided only for chaining.
The const version of display( ) function is provided to be called on
const Test objects. Given this, I have the following questions:

1)Is it advisable to overload a function like display( ) only on const
(everything else being the same for this function) for chaining
purpose when it does not modify the data members of the object



无需定义非const成员函数,如果他们的身体是凹凸不平的;


当我需要两个成员函数时,它有一个我能记得的情况


类型const& ; func()const;

类型& func();


对于代码重用,你可以这样写:


类型& func()

{

const_cast< Klass *> func(this) - > func();

// func() const必须返回非const类型对象

}

我记得这个NG或者clc ++。m对此有一些讨论,一些

民间甚至发明了一些宏来缓解这个Paired-Member-Function(TM)工作。

No need to define a non-const member function if their body are indentical;

It has one case that I can recall, when both member functions are needed

Type const& func() const;
Type & func();

For code reuse, you can write this way:

Type& func()
{
const_cast<Klass*>func(this)->func();
// func() const must return non-const Type object
}
I recalled that this NG or c.l.c++.m has some discussion on this, some
folk even invent some macro to ease this Paired-Member-Function(TM) work.


2)什么时候重载一个函数,只有const不可避免?
2) When is overloading a function, only on const inevitable ?



我不确定你的意思。


但如果我没错你,

(1)已经有了答案。

更喜欢const成员函数到非const成员


-

谢谢

Barry

I''m not sure what you mean.

But if I don''t get you wrong,
(1) already has your answer.
prefer const member function to non-const one

--
Thanks
Barry


Barry写道:
Barry wrote:

[..]

我记得有一个案例,当两个成员函数都需要



类型const& func()const;

类型& func();


对于代码重用,你可以这样写:


类型& func()

{

const_cast< Klass *> func(this) - > func();

// func() const必须返回非const类型对象
[..]
It has one case that I can recall, when both member functions are
needed
Type const& func() const;
Type & func();

For code reuse, you can write this way:

Type& func()
{
const_cast<Klass*>func(this)->func();
// func() const must return non-const Type object



首先,必须有一个返回值。所以,按照你的
模式,它将是


返回const_cast<类型&>(const_cast< const Klass *>(this) - > func ());

//我认为你错过了资格赛^^^^^这里


这是一个坏想法(tm),因为它促进了undefined行为。 ''func''的
const版本可能会返回对某些真正常量

内存的引用,并且调用者可能最终想要更改返回值

不知何故:KABOOM!

First of all, there has to be a return value. So, following your
pattern it would be

return const_cast<Type&>(const_cast<const Klass*>(this)->func());
// I think you missed the qualifier ^^^^^ here

which is a BAD IDEA(tm) because it promotes undefined behaviour. The
const version of ''func'' may return a reference to some truly constant
memory, and the caller may end up wanting to change the return value
somehow: KABOOM!


}
}



复制代码会更好为了避免不得不处理

const casts。

It would be better to duplicate the code to avoid having to deal with
const casts.


我记得这个NG或者clc ++。m对此有一些讨论,一些

民众甚至发明了一些宏来缓解这个Paired-Member-Function(TM)
的工作。 [..]
I recalled that this NG or c.l.c++.m has some discussion on this, some
folk even invent some macro to ease this Paired-Member-Function(TM)
work. [..]



V

-

请在回复时删除资金''A'通过电子邮件

我没有回复最热门的回复,请不要问

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于基于函数为const的重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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