后缀和前缀覆盖 [英] Postfix and Prefix Override

查看:108
本文介绍了后缀和前缀覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个小问题,我无法弄清楚解决方案,如果存在(我真的希望如此:))。我有一个基类,Base,在其中,我有两个嵌套类,A和B(B类派生自A)。在A类中,我有两个操作符:增量后缀和附加赋值。在B类中,我只有增量前缀(下面的代码)。



我的问题是:为什么调用增量前缀运算符而不是增量后缀运算符我做b ++?



祝你好运,

Filipe Marques



代码:



  #include   <   stdio.h  >  

class 基本
{
public
class A
{
受保护
A(){}
public
A& operator ++( int
{
printf( 1\\\
);
return A();
}

A& operator + =( unsigned int __num)
{
printf( 2 \ n);
return A();
}
};

class B: public A
{
public
B():A(){}

B& operator ++()
{
printf( 3 \ n);
return B();
}
};

B get_b()
{
return B();
}
};

void main()
{
基数;
Base :: B b = base.get_b();

// 没问题
b + = 1 ;

// 警告C4620:找不到'operator ++'的后缀形式
// 类型'Base :: B',使用前缀形式
b ++;
}





输出ID:

 2 
3

解决方案

这里发生的是隐藏。



首先是正常函数的示例:

  class  A 
{
public
void f(); // 1
};
class B: public A
{
public
void f( int ); // 2
};





2处的函数隐藏1处的函数。您可以使用A $ f; 在<$ c $中绕过 c> B



运算符++ 有两个签名:一个没有参数另一个有争论。在拨打其中一个时,您有两种选择,例如:对于作为班级成员的运营商:



隐式调用 显式调用

 ++ a; 

 a。 operator ++(); 

 a ++; 

< pre lang =c ++> a.operator ++( 0 );



类中的定义是:

  class  C 
{
public
C&操作者++(); // 前缀重载:++ a
C operator ++( INT ); // postfix overload:a ++
};





所以,最后:由于这两个运算符相互重载,你可以在A类中隐藏A类中的一个。

干杯

Andi



PS:警告说明了一切;-)。编译器决定采用错误运算符是值得商榷的,但警告告诉它这样做。


Andreas Gieriet非常清楚地给出了解释。



如果您希望将A中的operator ++重新引入B的范围,您可以使用

 使用 





http://publib.boulder.ibm.com/infocenter/comphelp/v101v121/index.jsp?topic=/com.ibm.xlcpp101。 aix.doc / language_ref / using_declaration_class_members.html [ ^ ]



那么:

  class  B: public  A 
{
public
使用 A :: operator ++;
B():A(){}

B& operator ++()
{
printf( 3 \ n);
return B();
}
};

B get_b()
{
return B();
}
};


Hi,

I have a little problem that I can not figure out the solution, if exist (I really hope so :) ). I have one base class, Base, and inside of it, I have two nested classes, A and B (class B is derived from A). In class A, I have two operatores: increment posfix and addition assignment. In class B, I only have the increment prefix (code below).

My problem is: Why it is call the increment prefix operator instead of the increment posfix operator when I do b++?

Best regards,
Filipe Marques

Code:

#include <stdio.h>

class Base
{
public:
	class A
	{
	protected:
		A() {}
	public:
		A& operator++(int)
		{
			printf("1\n");
			return A();
		}

		A& operator+=(unsigned int __num)
		{
			printf("2\n");
			return A();
		}
	};

	class B : public A
	{
	public:
		B() : A() {}

		B& operator++()
		{
			printf("3\n");
			return B();
		}
	};

	B get_b()
	{
		return B();
	}
};

void main()
{
	Base base;
	Base::B b = base.get_b();

	// No problem
	b += 1;

	// warning C4620:	no postfix form of 'operator ++' found
	// for type 'Base::B', using prefix form
	b++;
}



The output id:

2
3

解决方案

What happens here is "hiding".

First an example for normal functions:

class A
{
public:
  void f(); // 1
};
class B: public A
{
public:
  void f(int); // 2
};



The function at 2 hides the one at 1. You can circumvent this by a using A::f; in B.

The operator++ has two signatures: one without an argument and the other with argument. When calling one or the other, you have two options, e.g. for operators as class members:

implicit callexplicit call

++a;

a.operator++();

a++;

a.operator++(0);


The definition in the class is:

class C
{
public:
  C& operator++();    // prefix overload:  ++a
  C  operator++(int); // postfix overload: a++
};



So, finally: since the two operators are overloads of each other, you hide in your B class the one from the A class.
Cheers
Andi

PS: The warning says it all ;-). That the compiler decides to take the "wrong" operator is debatable, but the warning tells that it does so.


Andreas Gieriet has given the explanation very clearly.

If you wish to reintroduce operator++ from A into the scope of B you may employ

using

.

http://publib.boulder.ibm.com/infocenter/comphelp/v101v121/index.jsp?topic=/com.ibm.xlcpp101.aix.doc/language_ref/using_declaration_class_members.html[^]

So then:

	class B : public A
	{
	public:
		using A::operator++;
		B() : A() {}
 
		B& operator++()
		{
			printf("3\n");
			return B();
		}
	};
 
	B get_b()
	{
		return B();
	}
};


这篇关于后缀和前缀覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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