在C++中,我不清楚从复制赋值运算符返回引用的概念.为什么复制赋值运算符不能返回新对象的副本?另外,如果我上课A,还有以下内容:
A a1(param);
A a2 = a1;
A a3;
a3 = a2; //<--- this is the problematic line
Run Code Online (Sandbox Code Playgroud)
的operator=定义如下:
A A::operator=(const A& a)
{
if (this == &a)
{
return *this;
}
param = a.param;
return *this;
}
Run Code Online (Sandbox Code Playgroud) c++ operator-overloading copy-constructor assignment-operator
我知道=操作员不能超载,但必须有办法在我这里做我想要的:
我只是创建代表定量单位的类,因为我正在做一些物理学.显然我不能只从一个原语继承,但我希望我的类的行为与原语完全一样 - 我只是希望它们的输入方式不同.
所以我可以去,
Velocity ms = 0;
ms = 17.4;
ms += 9.8;
Run Code Online (Sandbox Code Playgroud)
等等
我不知道该怎么做.我想我会写一些像这样的类:
class Power
{
private Double Value { get; set; }
//operator overloads for +, -, /, *, =, etc
}
Run Code Online (Sandbox Code Playgroud)
但显然我不能重载赋值运算符.我有什么方法可以得到这种行为吗?
template <typename CRTP>
struct Pre {
CRTP & operator++();
};
template <typename CRTP>
struct Post {
CRTP operator++(int);
};
struct Derived
: Pre<Derived>
, Post<Derived>
{};
int main() {
Derived d;
d++;
++d;
}
Run Code Online (Sandbox Code Playgroud)
我从GCC得到这些错误:
<source>: In function 'int main()':
<source>:18:10: error: request for member 'operator++' is ambiguous
d++;
^~
<source>:8:14: note: candidates are: CRTP Post<CRTP>::operator++(int) [with CRTP = Derived]
CRTP operator++(int);
^~~~~~~~
<source>:3:16: note: CRTP& Pre<CRTP>::operator++() [with CRTP = Derived]
CRTP & operator++();
^~~~~~~~
<source>:19:11: error: request for member …Run Code Online (Sandbox Code Playgroud) 我重载了运算符<<
template <Typename T>
UIStream& operator<<(const T);
UIStream my_stream;
my_stream << 10 << " heads";
Run Code Online (Sandbox Code Playgroud)
工作但是:
my_stream << endl;
Run Code Online (Sandbox Code Playgroud)
给出编译错误:
错误C2678:二进制'<<':找不到哪个运算符带有'UIStream'类型的左操作数(或者没有可接受的转换)
做my_stream << endl工作的工作是什么?
我有时structs在地图中使用小键作为键,因此我必须operator<为它们定义一个.通常,这最终看起来像这样:
struct MyStruct
{
A a;
B b;
C c;
bool operator<(const MyStruct& rhs) const
{
if (a < rhs.a)
{
return true;
}
else if (a == rhs.a)
{
if (b < rhs.b)
{
return true;
}
else if (b == rhs.b)
{
return c < rhs.c;
}
}
return false;
}
};
Run Code Online (Sandbox Code Playgroud)
这看起来非常冗长且容易出错.有没有更好的方法,或一些简单的方法来自动定义operator<一个struct或class?
我知道有些人喜欢使用类似的东西memcmp(this, &rhs, sizeof(MyStruct)) < 0,但是如果成员之间存在填充字节,或者如果char在null终止符之后存在可能包含垃圾的字符串数组,则这可能无法正常工作.
我现在已经在StackOverflow.com上阅读了几个关于我的问题的问题,但似乎没有一个能解决我的问题.或者我可能做错了... <<如果我将其转换为内联函数,则重载会起作用.但是我如何让它在我的情况下工作?
warning: friend declaration std::ostream& operator<<(std::ostream&, const D<classT>&)' declares a non-template function
warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
/tmp/cc6VTWdv.o:uppgift4.cc:(.text+0x180): undefined reference to operator<<(std::basic_ostream<char, std::char_traits<char> >&, D<int> const&)' collect2: ld returned 1 exit status
代码:
template <class T>
T my_max(T a, T b)
{
if(a > b)
return a;
else
return b;
}
template <class classT> …Run Code Online (Sandbox Code Playgroud) 考虑一下这段代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
struct MyStruct
{
int key;
std::string stringValue;
MyStruct(int k, const std::string& s) : key(k), stringValue(s) {}
bool operator < (const MyStruct& other) {
return (key < other.key);
}
};
int main() {
std::vector < MyStruct > vec;
vec.push_back(MyStruct(2, "is"));
vec.push_back(MyStruct(1, "this"));
vec.push_back(MyStruct(4, "test"));
vec.push_back(MyStruct(3, "a"));
std::sort(vec.begin(), vec.end());
for (const MyStruct& a : vec) {
cout << a.key << ": " << a.stringValue << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
它编译得很好并且给出了预期的输出.但是,如果我尝试按降序排序结构:
#include …Run Code Online (Sandbox Code Playgroud) 一个代码块有效,但另一个代码没有.哪个是有意义的,除了第二个块与第一个块相同,只是用速记写的操作.它们实际上是相同的操作.
l = ['table']
i = []
Run Code Online (Sandbox Code Playgroud)
for n in l:
i += n
print(i)
Run Code Online (Sandbox Code Playgroud)
输出: ['t', 'a', 'b', 'l', 'e']
for n in l:
i = i + n
print(i)
Run Code Online (Sandbox Code Playgroud)
输出:
TypeError:只能将列表(不是"str")连接到列表
是什么导致了这个奇怪的错误?
说我正在上课:
class Foo{
public:
std:string name;
/*...*/
}/*end Foo*/
Run Code Online (Sandbox Code Playgroud)
我提供了一个重载 operator==
bool operator==(const Foo& fooObj, const std::string& strObj) {
return (fooObj.name == strObj);
}
Run Code Online (Sandbox Code Playgroud)
我是否还需要反向重新实现相同的逻辑?
bool operator==(const std::string& strObj, const Foo& fooObj) {
return (strObj == fooObj.name);
}
Run Code Online (Sandbox Code Playgroud) 是否可以覆盖Objective-C中的操作符使用?
例如
myClassInstance + myClassInstance
Run Code Online (Sandbox Code Playgroud)
调用自定义函数来添加两个.