我在处理C++中重载成员函数的指针时遇到了一个小问题.以下代码编译正常:
class Foo {
public:
float X() const;
void X(const float x);
float Y() const;
void Y(const float y);
};
void (Foo::*func)(const float) = &Foo::X;
Run Code Online (Sandbox Code Playgroud)
但是这不会编译(编译器抱怨重载是不明确的):
void (Foo::*func)(const float) = (someCondition ? &Foo::X : &Foo::Y);
Run Code Online (Sandbox Code Playgroud)
大概这与编译器分离出条件运算符的返回值和函数指针类型有关吗?我可以解决它,但我很想知道规范如何说这一切应该工作,因为它似乎有点不直观,如果有一些方法可以解决它而不会回到5行if-then-else .
我正在使用MSVC++,如果这有任何区别.
谢谢!
考虑到评估时间,以下是两个相当的?
if(condition1)
{
//code1
}
else
{
//code2
}
Run Code Online (Sandbox Code Playgroud)
condition1 ? code1 : code2
或者它们只是语法上的不同?
if-statement operators ternary-operator conditional-operator micro-optimization
你可以在php中对条件语句进行赋值,如下所示:
if(siteName_err = isValid("sitename", $_POST['sitename'], false))
{
$siteName = $_POST['sitename'];
}
Run Code Online (Sandbox Code Playgroud) 我们刚刚找到了一位同事和我,使用If条件语法编写了一个奇怪的编译语法:
if (true); {
foo();
}
Run Code Online (Sandbox Code Playgroud)
这里有人向我们解释这种奇怪的语法吗?谢谢.
我试图建立一个像我这样的类的优先级队列 -
std::priority_queue<Position> nodes;
Run Code Online (Sandbox Code Playgroud)
我在这样的位置重载了<运算符 -
bool Position::operator<(Position& right) {
return (fvalue < right.getFValue());
}
Run Code Online (Sandbox Code Playgroud)
但是,每当我尝试编译时,我都会收到此错误消息,指出<运算符未过载 -
error: no match for ‘operator<’ in ‘__x < __y’
position.h:30: note: candidates are: bool Position::operator<(Position&)
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?任何帮助表示赞赏.
c++ operator-overloading priority-queue operators conditional-operator
考虑以下代码:
int i, k, m;
k = 12;
m = 34;
for (i = 0; i < 2; i++) ((i & 1) ? k : m) = 99 - i;
printf("k: %ld m: %ld\n\n", k, m);
Run Code Online (Sandbox Code Playgroud)
在这个愚蠢的例子中,条件运算符表达式是一个快捷方式:
if (i & 1) k = 99 - i; else m = 99 - i;
Run Code Online (Sandbox Code Playgroud)
我的编译器没有抱怨并执行这段代码给出了预期的输出
k: 98 m: 99
Run Code Online (Sandbox Code Playgroud)
不过,我的问题是,如果这是符合C标准的有效代码吗?我以前从未见过这样的东西.
这可能是一个傻瓜问题,但我无法弄清楚? exp : other_exp序列的调用.
例:
int result = (true) ? 1 : 0;
我已经尝试过使用谷歌机器了,但是在不知道它叫什么的情况下Googilize很难做到.
谢谢!
读完这篇文章后,我开始认为我已经学会了一个战利品printf().突然,我发现从下面的代码片段这本书:
int main()
{
char str[]="Hello";
int i=5,j=10;
printf(i>j?"%50s":"%s",str); //unable to understand this
return 0;
}
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,上面的代码运行没有错误,它打印Hello.根据我的知识,以下是语法printf():
int printf(const char *format,argument_list);
Run Code Online (Sandbox Code Playgroud)
所以根据这种语法,printf()应该从格式字符串开始.但正如您在上面的代码中printf()所看到的那样i>j.
这是否意味着我在解释printf()的语法时出错了?将三元运算符放在printf()中是一种特殊情况吗?
编辑
我知道三元运算符我问的第一个参数printf()应该是const char*,我似乎不在我的例子中.
这在eclipse IDE中出错.(错误符号出现在行号附近)
String[] allText = null;
Run Code Online (Sandbox Code Playgroud)
在此之后我做了一些事情,比如初始化数组等等.但根据一些条件.所以我想使用如下的条件运算符.
List<String> finalText = (allText != null) ?
Arrays.asList(allText) : (List<String>) Collections.emptyList();
Run Code Online (Sandbox Code Playgroud)
如果我在等号之后放置我的铸件,它运行良好.(包装完整的三元操作)这个错误的目的是什么?
List<String> allHotels = (List<String>) ((allText != null) ?
Arrays.asList(allText) : Collections.emptyList());
Run Code Online (Sandbox Code Playgroud) 这段代码今天抓住了我:
clientFile.ReviewMonth == null ? null : MonthNames.AllValues[clientFile.ReviewMonth.Value]
Run Code Online (Sandbox Code Playgroud)
clientFile.Review月是一个字节?在失败的情况下,它的值为null.预期的结果类型是字符串.
此代码中的例外情况
public static implicit operator string(LookupCode<T> code)
{
if (code != null) return code.Description;
throw new InvalidOperationException();
}
Run Code Online (Sandbox Code Playgroud)
正在评估评估的右侧,然后隐式转换为字符串.
但我的问题是,为什么右手边被评估,显然只应评估左手边?(文档说明"只评估两个表达式中的一个.")
顺便说一句,解决方案是将null转换为字符串 - 这有效但Resharper告诉我演员是多余的(我同意)
编辑:这与"为什么我需要在编译之前添加一个强制转换"类型的三元运算符问题不同.这里的要点是不需要强制转换来使其编译 - 只是为了使其正常工作.