且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

后缀和前缀覆盖

更新时间:2023-02-20 23:20:24

这里发生的是隐藏。



首先是正常函数的示例:

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





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



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



隐式调用 显式调用
 ++ a; 

 a。 operator ++(); 

 a ++; 

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 call explicit 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();
	}
};