请教一个奇怪的语法 >> (值)(值)!

神的理想 2013-06-03 05:43:33
下面两句是某程序中的,编译通过,但是(值)(值)的语法不能理解!
UInt32 nArrValues[MaxCount];	
Info.nArrValues[i] = (Info.nArrValues[i] * 10u + nType)(0u, 999999u);


自己测试写了一个类似的语法,但是同样的编辑器却编译通不过,为什么我的不行?
int xxx[32];
xxx[2] = (xxx[2] * 10u + 3)(0u, 999999u);
...全文
243 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
cqdjyy01234 2013-06-04
  • 打赏
  • 举报
回复
很明显的运算符重载
Qt讷讷熊 2013-06-04
  • 打赏
  • 举报
回复
靠猜测补全代码来证明一些看似不合理的代码是可行的, 但这种方法没有任何意义. 看到完整使用并弄明白才是正途.
ztenv 版主 2013-06-04
  • 打赏
  • 举报
回复
楼主看一下仿函数吧或函数指针;最重要的是看一下相关代码
双杯献酒 2013-06-04
  • 打赏
  • 举报
回复
()()这种用法只能是两种情况: <1>函数调用 <2>强制类型转换 就这里来看,强制类型转换不大可能, 那就是函数调用 函数调用又分3种: <1>普通函数调用 <2>函数指针的函数调用 <3>函数对象(仿函数)的函数调用. 第一种不大可能 那么: Info.nArrValues[i] * 10u + nType就应该是一个函数对象或者函数指针. Info.nArrValues[i]已经知道是整数的话, 那nType就是一个 函数指针的数组 或者 仿函数的数组 或者重载了+, 返会函数真正的对象. 估计秘密就在nType了.
anshiny 2013-06-04
  • 打赏
  • 举报
回复
严重同意10楼说法。
ForestDB 2013-06-03
  • 打赏
  • 举报
回复
UInt32 nArrValues[MaxCount]; Info.nArrValues[i] = (Info.nArrValues[i] * 10u + nType)(0u, 999999u); 请给出红色东西的声明/定义。 C++就是这样,可以写出让你不敢说出"1 + 1"等于几的代码。
zhaokai115 2013-06-03
  • 打赏
  • 举报
回复
同意楼上的,严重怀疑nType是个函数指针的数组,或者functor的数组,(Info.nArrValues[i] * 10u + nType)结果是一个函数指针或者functor对象。
  • 打赏
  • 举报
回复
(Info.nArrValues[i] * 10u + nType)这个返回一个对象的话 就可以啊
疯狂的红豆 2013-06-03
  • 打赏
  • 举报
回复
()()这种却是是一种语法啊,无参仿函数啊,举个例子,

namespace for_eachFun
{
	template<class T>
	class AddValue
	{
	public:
		AddValue(const T &t):theValue(t) {}
		//适用于for_each算法的仿函数
		void operator() (T &elem)  //我这个是带参的仿函数,完全可以不带参像你说的那样
		{
			elem += theValue;
		}
	private:
		T theValue;
	};
}
pbdwadr 2013-06-03
  • 打赏
  • 举报
回复
有可能是UINT32这个类型做了手脚,看看是不是有操作符的重载?
神的理想 2013-06-03
  • 打赏
  • 举报
回复
Info.nArrValues[i] = (Info.nArrValues[i] * 10u + nType)(0u, 999999u); 就是这一句,看起来像强制转换,但是前面括弧中的却是值,不是内型!再想想又像是运算符优先级,但是中间没有运算符。。
神的理想 2013-06-03
  • 打赏
  • 举报
回复
但是这个程序()()语法,他的确实编译通过!
pbdwadr 2013-06-03
  • 打赏
  • 举报
回复
不可能,这种语法不存在!
赵4老师 2013-06-03
  • 打赏
  • 举报
回复
自己写代码搞不清楚算符优先级请多加括号。 看别人代码搞不清楚算符优先级能调试的话请单步调试对应汇编。 看别人代码搞不清楚算符优先级不能调试的话想办法照写一小段孤立的可调试的代码然后单步调试对应汇编。 看别人代码搞不清楚算符优先级不能调试的话且没有办法照写一小段孤立的可调试的代码然后单步调试对应汇编的话只能参考算符优先级表猜了(提醒:并不能100%猜对)。
//C++ Operators
//  Operators specify an evaluation to be performed on one of the following:
//    One operand (unary operator)
//    Two operands (binary operator)
//    Three operands (ternary operator)
//  The C++ language includes all C operators and adds several new operators.
//  Table 1.1 lists the operators available in Microsoft C++.
//  Operators follow a strict precedence which defines the evaluation order of
//expressions containing these operators.  Operators associate with either the
//expression on their left or the expression on their right;    this is called
//“associativity.” Operators in the same group have equal precedence and are
//evaluated left to right in an expression unless explicitly forced by a pair of
//parentheses, ( ).
//  Table 1.1 shows the precedence and associativity of C++ operators
//  (from highest to lowest precedence).
//
//Table 1.1   C++ Operator Precedence and Associativity
// The highest precedence level is at the top of the table.
//+------------------+-----------------------------------------+---------------+
//| Operator         | Name or Meaning                         | Associativity |
//+------------------+-----------------------------------------+---------------+
//| ::               | Scope resolution                        | None          |
//| ::               | Global                                  | None          |
//| [ ]              | Array subscript                         | Left to right |
//| ( )              | Function call                           | Left to right |
//| ( )              | Conversion                              | None          |
//| .                | Member selection (object)               | Left to right |
//| ->               | Member selection (pointer)              | Left to right |
//| ++               | Postfix increment                       | None          |
//| --               | Postfix decrement                       | None          |
//| new              | Allocate object                         | None          |
//| delete           | Deallocate object                       | None          |
//| delete[ ]        | Deallocate object                       | None          |
//| ++               | Prefix increment                        | None          |
//| --               | Prefix decrement                        | None          |
//| *                | Dereference                             | None          |
//| &                | Address-of                              | None          |
//| +                | Unary plus                              | None          |
//| -                | Arithmetic negation (unary)             | None          |
//| !                | Logical NOT                             | None          |
//| ~                | Bitwise complement                      | None          |
//| sizeof           | Size of object                          | None          |
//| sizeof ( )       | Size of type                            | None          |
//| typeid( )        | type name                               | None          |
//| (type)           | Type cast (conversion)                  | Right to left |
//| const_cast       | Type cast (conversion)                  | None          |
//| dynamic_cast     | Type cast (conversion)                  | None          |
//| reinterpret_cast | Type cast (conversion)                  | None          |
//| static_cast      | Type cast (conversion)                  | None          |
//| .*               | Apply pointer to class member (objects) | Left to right |
//| ->*              | Dereference pointer to class member     | Left to right |
//| *                | Multiplication                          | Left to right |
//| /                | Division                                | Left to right |
//| %                | Remainder (modulus)                     | Left to right |
//| +                | Addition                                | Left to right |
//| -                | Subtraction                             | Left to right |
//| <<               | Left shift                              | Left to right |
//| >>               | Right shift                             | Left to right |
//| <                | Less than                               | Left to right |
//| >                | Greater than                            | Left to right |
//| <=               | Less than or equal to                   | Left to right |
//| >=               | Greater than or equal to                | Left to right |
//| ==               | Equality                                | Left to right |
//| !=               | Inequality                              | Left to right |
//| &                | Bitwise AND                             | Left to right |
//| ^                | Bitwise exclusive OR                    | Left to right |
//| |                | Bitwise OR                              | Left to right |
//| &&               | Logical AND                             | Left to right |
//| ||               | Logical OR                              | Left to right |
//| e1?e2:e3         | Conditional                             | Right to left |
//| =                | Assignment                              | Right to left |
//| *=               | Multiplication assignment               | Right to left |
//| /=               | Division assignment                     | Right to left |
//| %=               | Modulus assignment                      | Right to left |
//| +=               | Addition assignment                     | Right to left |
//| -=               | Subtraction assignment                  | Right to left |
//| <<=              | Left-shift assignment                   | Right to left |
//| >>=              | Right-shift assignment                  | Right to left |
//| &=               | Bitwise AND assignment                  | Right to left |
//| |=               | Bitwise inclusive OR assignment         | Right to left |
//| ^=               | Bitwise exclusive OR assignment         | Right to left |
//| ,                | Comma                                   | Left to right |
//+------------------+-----------------------------------------+---------------+
计算机组成原理→DOS命令→汇编语言→C语言(不包括C++)、代码书写规范→数据结构、编译原理、操作系统→计算机网络、数据库原理、正则表达式→其它语言(包括C++)、架构…… 对学习编程者的忠告: 眼过千遍不如手过一遍! 书看千行不如手敲一行! 手敲千行不如单步一行! 单步源代码千行不如单步对应汇编一行! VC调试时按Alt+8、Alt+7、Alt+6和Alt+5,打开汇编窗口、堆栈窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应堆栈、内存和寄存器变化,这样过一遍不就啥都明白了吗。 对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或者在某行按F9设了断点后按F5执行停在该断点处的时候。
神的理想 2013-06-03
  • 打赏
  • 举报
回复
UInt32 nArrValues[MaxCount]; //这一句是我加的,原型是在类中的! Info.nArrValues[i] = (Info.nArrValues[i] * 10u + nType)(0u, 999999u);

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧