C ++重载操作符+外部类 [英] C++ overload operator + outside class

查看:139
本文介绍了C ++重载操作符+外部类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我的任务是在char *和这样的整数之间重载operator +:



hello. I have the task to overload operator + between a char* and an integer like this:

char* operator+(const int k, char* c)
{
	for (int i = 0; i<strlen(c); i++)
		c[i] += k;
	return c;
}

int main()
{
	char *c = new char[10];
	cin >> c;// abc
	c = c + 2;
	cout << c;// cde
	return 0;
}





错误:

错误C2803:'operator +'必须至少有一个类型的正式参数







任何想法如何以及为什么?



我尝试了什么:



i将它写成类似这样的类来解决它



char * operator +(int k)

{

int i;

for( i = 0; i< strlen(c); i ++)

c [i] + = key;

返回c;

}



但这只适用于c = c + 2;任何想法,只有在那个类里面,这不是我的任务...



Error:
error C2803: 'operator +' must have at least one formal parameter of class type



any ides how and why?

What I have tried:

i solved it by writing it into a class like this

char* operator+(int k)
{
int i;
for (i = 0; i < strlen(c); i++)
c[i] += key;
return c;
}

but this works only for c=c+2; any ideas and only inside that class and that is not the task i have...

推荐答案

我认为你不想这样做:它非常反...直观。

通常,当你添加一个整数和一个指针时,你得到的指针已经在原始指针所指向的内存中移动:

I don't think you want to do that: it's very counter-intuitive.
Normally, when you add an integer and a pointer, you get a pointer that has moved on within the memory pointed to by the original pointer:
char *c = new char[10];
cin >> c;  // abcdefg
c = c + 2;
cout << c; // cdefg

定义一个相同的运算符而不是颠覆并且实际上更改内存将使您的代码非常,非常难以维护。

我强烈建议您不要这样做,但是定义一个名为IncrementMemory的简单函数并调用它。

And defining an identical operator than subverts that and actually changes memory is going to make your code very, very hard to maintain.
I'd strongly suggest that you don't do this, but define a simple function called IncrementMemory and call that instead.


如果这是问题的确切措辞,那么它就是陷阱。 C ++不允许在预定义类型之间覆盖算术运算符的行为,例如int和char *。这就是C2803告诉你的错误信息,在这种情况下编译器是正确的。



那么你能做些什么呢?您必须将其中一个参数设为类类型,如消息所示。您的参数c是指向以null结尾的字符串的指针,是替换的理想候选对象。例如,我们可以用std :: string引用替换它。

If that is the exact wording of the question then it's a trap. C++ does not allow to override the behavior of arithmetic operators between predefined types, for example int and char*. That's what the error message C2803 is telling you, and the compiler is correct in this case.

So what can you do about it? You have to make one of the parameters a "class type" as the message says. Your parameter c is a pointer to a null-terminated character string and is the ideal candidate for replacement. For example, we could replace it with an std::string reference.
 #include <string>
 using namespace std;

string& operator+ (const int k, string& c)
{
	size_t len = c.length();
	for (size_t i = 0; i < len; i++)
		c[i] += k;
	return c;
}



当然你也必须改变你的主要功能。



之后所有,正如格里夫所说,这首先不是一个好主意,我没有看到这个练习的目的。


Of course you had to change your main function as well.

And after all, as Griff said, this is not a very good idea in the first place and I don't see the purpose of this exercise.


这篇关于C ++重载操作符+外部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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