标签: operator-keyword

17
推荐指数
6
解决办法
5万
查看次数

@ error suppression operator和set_error_handler

我遵循良好的编程习惯,我将PHP错误记录到文件而不是将其显示给用户.我用set_error_handler()它.

现在问题.例如,我有一个地方:

@file_exists('/some/file/that/is/outside/openbasedir.txt');
Run Code Online (Sandbox Code Playgroud)

但是,尽管有错误抑制操作符,错误消息仍会记录.我不希望这样.我希望抑制错误不要传递给我的错误处理程序.

php logging error-reporting operator-keyword

17
推荐指数
3
解决办法
2246
查看次数

+运算符,类类型和内置类型之间的区别?

我是C++的新手.我读过的这本书告诉我,如果+为某个类对象(比如类)重载了plus()运算符,那么string这个问题会更加具体.

#include<iostream>
#include<string>
using namespace std;

int main()
{
    string s1("abc");
    string s2("def");

    string s3("def");

    cout<<(s1+s2=s3)<<endl;

    int x=1;
    int y=2
    int z=3;
    cout<<(x+y=z)<<endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

正如您所料,第一个cout陈述是正确的,而第二个陈述是错误的.编译器投诉x+y不是可修改的左值.我的问题是为什么+操作符为string对象返回可修改的左值但不是为了int

c++ const function object operator-keyword

17
推荐指数
2
解决办法
1093
查看次数

三元运算符中的多个条件

首先,问题是"编写一个Java程序,使用三元运算符找到三个最小的数字."

这是我的代码:

class questionNine
{
    public static void main(String args[])
    {
        int x = 1, y = 2, z = 3;
        int smallestNum;

        smallestNum = (x<y && x<z) ? x : (y<x && y<z) ? y : (z<y && z<x) ? z;
        System.out.println(smallestNum + " is the smallest of the three numbers.");
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试在三元运算符中使用多个条件,但这不起作用.几天我不在,所以我不确定该做什么,老师的电话已关闭.有帮助吗?

java ternary operator-keyword

16
推荐指数
4
解决办法
8万
查看次数

为什么在条件运算符(?:)中,第二个和第三个操作数必须具有相同的类型?

为什么在条件运算符(?:),第二和第三个操作数必须具有相同的类型?

我的代码是这样的:

#include <iostream>
using std::cout;

int main()
{
    int a=2, b=3;
    cout << ( a>b ? "a is greater\n" : b );  /* expression ONE */
    a>b? "a is greater\n" : b;               /* expression TWO */

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用g ++编译时,会发出错误:

main.cpp:7:36: error: operands to ?: have different types ‘const char*’ and ‘int’
main.cpp:8:28: error: operands to ?: have different types ‘const char*’ and ‘int’
Run Code Online (Sandbox Code Playgroud)

我想知道为什么他们必须有相同的类型?

(1)在我看来,如果(a>b)是,那么表达式( a>b ? "a is …

c c++ operator-keyword

16
推荐指数
3
解决办法
7296
查看次数

为什么我不能直接在临时对象上调用operator()?

我想做的事情可归纳为以下代码:

struct A{};

struct B{
    A& a;
    B(A& a) noexcept : a(a){}
    int operator()(int) {}
};

int main(){
    A a;
    B(a)(2);
}
Run Code Online (Sandbox Code Playgroud)

我的编译器(g++ 6)拒绝了代码抱怨a阴影参数.但是,如果我尝试显式调用operator(),它会按预期工作.

似乎g++忽略括号并将声明视为声明.

这是指定的还是预期的行为?

c++ compiler-errors g++ operator-keyword

16
推荐指数
1
解决办法
971
查看次数

是否无法手动调用C++运算符?

我正在尝试更仔细地理解C++中的运算符.

我知道C++中的运算符基本上只是函数.我没有得到的是,该功能是什么样的?

举个例子:

int x = 1;
int y = 2;
int z = x + y;
Run Code Online (Sandbox Code Playgroud)

最后一行如何翻译?是吗:

1. int z = operator+(x,y);

要么

2. int z = x.operator+(y);

当我尝试了它们时,编译错误.我称他们是错的还是不允许直接调用C++中的运算符?

c++ operator-keyword

15
推荐指数
3
解决办法
7551
查看次数

为什么不提供运营商?:在斯卡拉

? :Java中有一个运算符,可用于根据布尔表达式选择值.例如,表达式3 > 2 ? "true" : false将返回一个字符串"true".我知道我们可以使用if表达式来做到这一点,但我更喜欢这种风格,因为它简洁而优雅.

java scala operator-keyword

15
推荐指数
3
解决办法
8729
查看次数

decltype和C++中的作用域运算符

我需要获取实例化模板时提供的类型.请考虑以下示例:

template <typename T> struct Foo
{
  typedef T TUnderlying;
};

static Foo<int> FooInt;

class Bar
{
public:
  auto Automatic() -> decltype(FooInt)::TUnderlying
  {
    return decltype(FooInt)::TUnderlying();
  }
};

int main()
{
  Bar bar;
  auto v = bar.Automatic();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

此代码的问题是将范围运算符与decltype一起使用.Visual C++ 2010抱怨如下:

错误C2039:'TUnderlying':不是'`global namespace''的成员

我在维基百科上收集了一些关于这个主题的信息:

在评论C++ 0x的正式委员会草案时,日本ISO成员机构指出"范围运算符(::)不能应用于decltype,但它应该是.在获取成员类型的情况下将是有用的(嵌套类型)来自实例,如下所示":[16]

vector<int> v;
decltype(v)::value_type i = 0; // int i = 0;
Run Code Online (Sandbox Code Playgroud)

David Vandevoorde解决了这个和类似的问题,并于2010年3月投票进入了工作文件.

所以我认为Visual C++ 2010没有实现这一点.我想出了这个解决方法:

template <typename T> struct ScopeOperatorWorkaroundWrapper
{
  typedef typename T::TUnderlying TTypedeffedUnderlying;
};

auto Automatic() -> ScopeOperatorWorkaroundWrapper<decltype(FooInt)>::TTypedeffedUnderlying
{
  return …
Run Code Online (Sandbox Code Playgroud)

c++ scope decltype operator-keyword c++11

15
推荐指数
1
解决办法
1998
查看次数

为什么1 .__添加__(2)不能解决?

可能重复:
访问python int literals方法

在Python中everything is an object.

但话又说回来,为什么下面的代码片段不起作用?

1.__add__(2)
Run Code Online (Sandbox Code Playgroud)

但是,这确实有效:

n = 1
n.__add__(2)
Run Code Online (Sandbox Code Playgroud)

n和之间有什么区别1

这不是一个设计失败,它不起作用?例如,它也适用于string文字.

"one".__add__("two")
Run Code Online (Sandbox Code Playgroud)

相比之下,它也适用于其他纯面向对象的语言.

让我们仔细看看这个编译c#示例:

Console.WriteLine(100.ToString());
Run Code Online (Sandbox Code Playgroud)

那么,Python从什么C#角度来区别everything is an object

python literals operator-keyword

15
推荐指数
2
解决办法
668
查看次数