可能重复:
非成员运算符重载应放在何处?
在浏览SO时,我经常会发现涉及重载/定义a std::ostream& operator<<(std::ostream& os, const Foo& foo)或a的问题或答案Foo operator+(const Foo& l, const Foo& r).
虽然我知道如何以及何时(不)编写这些操作符,但我对namespace此事感到困惑.
如果我有以下课程:
namespace bar
{
class Foo {};
}
Run Code Online (Sandbox Code Playgroud)
namespace我应该在哪个中编写不同的运算符定义?
// Should it be this
namespace bar
{
std::ostream& operator<<(std::ostream& os, const Foo& foo);
}
// Or this ?
namespace std
{
ostream& operator<<(ostream& os, const bar::Foo& foo);
}
// Or this ?
std::ostream& operator<<(std::ostream& os, const bar::Foo& foo);
Run Code Online (Sandbox Code Playgroud)
同样的问题适用于operator+.那么,这里的好习惯是什么?为什么?
对不起,如果之前有人询问过.我找不到明确的答案.如果我的查询包含$或运算符,我可以在mongodb索引上查询吗?我的查询看起来像这样:
// find everything in the collection
$cursor = $collection->find(array(
'$or' => array(
array('album_id' => array(
'$in' => $this->my_album_ids
),
'type' => array(
'$in' => array('like','comment')
)
),
array(
'user_id' => (int)session_item('user_id'),
'type' => 'message',
'reply' => 'no'
)
),
'timestamp' => array('$gt' => (int)$since)))->sort(array('timestamp'=>-1))->skip($start)->limit($amount);
Run Code Online (Sandbox Code Playgroud)
示例是在PHP中,但我想这适用于任何语言.
更新:
以下是我的索引,但上面的查询不使用它们.虽然看起来对我来说.
$collection->ensureIndex(array(
'album_id' => 1,
'type' => 1,
'timestamp' => -1,
));
$collection->ensureIndex(array(
'user_id' => 1,
'type' => 1,
'reply' => 1,
'timestamp' => -1,
));
Run Code Online (Sandbox Code Playgroud)
这是我的解释()
Array
(
[cursor] => BasicCursor
[nscanned] …Run Code Online (Sandbox Code Playgroud) 仅当变量的先前值为零时,以下带有调试选项'set -e -v'的脚本才会在增量运算符处失败.
#!/bin/bash
set -e -v
i=1; let i++; echo "I am still here"
i=0; let i++; echo "I am still here"
i=0; ((i++)); echo "I am still here"
Run Code Online (Sandbox Code Playgroud)
bash(GNU bash,版本4.0.33(1)-release(x86_64-apple-darwin10),还有GNU bash,版本4.2.4(1)-release(x86_64-unknown-linux-gnu))
有任何想法吗?
我使用相同的参数列表重载操作符两次.但具有不同的返回类型:
T& operator()(par_list){blablabla}
const T& operator()(par_list){blablabla}
Run Code Online (Sandbox Code Playgroud)
所以当我调用()运算符时,将根据什么偏好或情况调用哪个函数?我知道如果我在const函数下调用()它必须是const T&one.
我只是好奇C++如何处理这种情况以及默认偏好如何工作.
谢谢
我有类CMatrix,其中是值的数组"双指针".
class CMatrix {
public:
int rows, cols;
int **arr;
};
Run Code Online (Sandbox Code Playgroud)
我只需要输入以下内容来访问矩阵的值:
CMatrix x;
x[0][0] = 23;
Run Code Online (Sandbox Code Playgroud)
我知道如何使用:
x(0,0) = 23;
Run Code Online (Sandbox Code Playgroud)
但我真的需要这样做.任何人都可以帮助我吗?请?
谢谢大家帮忙,我这样做了...
class CMatrix {
public:
int rows, cols;
int **arr;
public:
int const* operator[]( int const y ) const
{
return &arr[0][y];
}
int* operator[]( int const y )
{
return &arr[0][y];
}
....
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助我真的很感激!
我在这里得到了一些PHP代码:
<?php
echo 'hello ' . 1 + 2 . '34';
?>
Run Code Online (Sandbox Code Playgroud)
输出234,
但是当我在"你好"之前添加一个数字11时:
<?php
echo '11hello ' . 1 + 2 . '34';
?>
Run Code Online (Sandbox Code Playgroud)
它输出1334而不是245(我预期它),为什么呢?
php string concatenation operator-precedence operator-keyword
我有3个依赖的Rest API资源(比如说observables),如下所示:
1st observable生成一个项目作为用户数组,如下所示:
getUsers(): Observable<User[]>
[
{
"id": 1,
"name": "Peter",
"surname": "Smith"
},
{
"id": 2,
"name": "John",
"surname": "Wayne"
},
...
]
Run Code Online (Sandbox Code Playgroud)
第二个observable可用于获取分配给用户的地址,因此输入参数是User ID,并返回一个作为地址数组的项:
getUserAddresses(user_id: string): Observable<Address[]>
[
{
"id": 1,
"city": "London",
"street": "Wicombe 34"
},
{
"id": 2,
"city": "Paris",
"street": "La fever 20"
},
...
]
Run Code Online (Sandbox Code Playgroud)
第3个observable可用于获取分配给用户的公司,因此输入参数是User ID,并返回一个项目作为公司数组:
getUserCompanies(user_id: string): Observable<Company[]>
[
{
"id": 1,
"name": "Fintch CO",
"state": "France"
},
{
"id": 2,
"name": "C&C inc.",
"state": "USA"
},
...
]
Run Code Online (Sandbox Code Playgroud)
我想将这3个observable链接成一个将再次产生结果作为一个项目,它将包含其附加地址数组和公司数组的用户数组,如下所示:
[ …Run Code Online (Sandbox Code Playgroud) 我试图通过将double包装到struct中来获得我称之为测量单位系统的东西.我有C#结构,如Meter,Second,Degree等.我最初的想法是,在编译器内联所有内容后,我将获得与使用double时相同的性能.
我的显式和隐式运算符简单明了,编译器实际上内联它们,但是Meter和Second的代码比使用double的相同代码慢10倍.
我的问题是:为什么C#编译器不能使用Second作为使用double的代码的最佳代码,如果它无论如何都要内联?
第二个定义如下:
struct Second
{
double _value; // no more fields.
public static Second operator + (Second left, Second right)
{
return left._value + right._value;
}
public static implicit Second operator (double value)
{
// This seems to be faster than having constructor :)
return new Second { _value = value };
}
// plenty of similar operators
}
Run Code Online (Sandbox Code Playgroud)
更新:
我没有问结构是否适合这里.确实如此.
我没有询问代码是否会被内联.JIT确实内联它.
我检查了运行时发出的汇编操作.对于像这样的代码,它们是不同的:
var x = new double();
for (var i = 0; i < 1000000; i++)
{ …Run Code Online (Sandbox Code Playgroud) 我正在阅读underscore.js库,我找到了之前没有遇到过的东西:
if (obj.length === +obj.length) { ... }
Run Code Online (Sandbox Code Playgroud)
以下代码似乎让我感到困惑,因为它提供了两个不同的输出.代码在jdk 1.7上进行了测试.
public class NotEq {
public static void main(String[] args) {
ver1();
System.out.println();
ver2();
}
public static void ver1() {
Integer a = 128;
Integer b = 128;
if (a == b) {
System.out.println("Equal Object");
}
if (a != b) {
System.out.println("Different objects");
}
if (a.equals(b)) {
System.out.println("Meaningfully equal.");
}
}
public static void ver2() {
Integer i1 = 127;
Integer i2 = 127;
if (i1 == i2) {
System.out.println("Equal Object");
}
if (i1 != i2){
System.out.println("Different objects");
} …Run Code Online (Sandbox Code Playgroud) operator-keyword ×10
c++ ×3
angular ×1
bash ×1
c# ×1
const ×1
increment ×1
indexing ×1
integer ×1
java ×1
javascript ×1
matrix ×1
mongodb ×1
namespaces ×1
observable ×1
overloading ×1
performance ×1
php ×1
reference ×1
rxjs ×1
set ×1
shell ×1
string ×1
struct ×1
syntax ×1
wrapper ×1