是否可以创建一个类型(比如说degrees
)并为它定义特定的运算符?如:=, +, *, -, /, +=, *=, -=, /=
.
我想知道这是因为我需要为我的一个程序使用度数而我不想使用一个float
对象,因为使用它degrees a; a.value = 120; a.value = b.value + a.value;
是一个简单的冗余degrees a = 120; a = b+a;
.
现在我为什么不使用:
typedef float degrees;
Run Code Online (Sandbox Code Playgroud)
?好吧,因为我还需要一件事.当我写作
degrees a;
a = 120;
a += 300;
Run Code Online (Sandbox Code Playgroud)
a
应该等于60
(420-360),因为我真的不需要a = 6150
什么时候能有a = 30
相同的效果.所以我会重载这些运算符以保持0到360之间的值.
可能吗?如果是这样,怎么样?
我正在尝试编写C++ 11冒名顶替者(最好由@jrok称为,因为这些类没有像包装器那样的字段),对于一堆C"类",类似于:
extern "C" {
struct cfoo;
cfoo * cfoo_new();
void cfoo_free(cfoo *);
int cfoo_bar(cfoo *, int);
} // extern "C" {
class Foo final {
Foo() = delete; // Prevents
Foo(Foo &&) = delete; // construction
Foo(const Foo &) = delete; // of this
Foo & operator=(Foo &&) = delete; // C++
Foo & operator=(const Foo &) = delete; // object
public: /* Methods: */
int bar(int v) noexcept { return cfoo_bar(cPtr(), v); }
cfoo * cPtr() noexcept { …
Run Code Online (Sandbox Code Playgroud) 我想知道这是否是重载post和pre increment运算符的正确代码.
如何在main()中调用这些运算符.
class fix{
int x;
int y;
public:fix(int = 0, int = 0);
fix operator++(){//prefix increment
fix a;
++a.x;
++a.y;
return a;
}
fix operator++(int){ //post fix increment
fix c;
c.x = x;
c.y = y;
x++; y++;
return c;
}
};
Run Code Online (Sandbox Code Playgroud) 我有Class NumDays
如下所示:
Class NumDays
{
private:
double hours;
public:
NumDays() { hours = 0.0; } //default constructor
NumDays(double hr) { hr = hours; } //initializing constructor
//Large class, nothing of importance, rest of class omitted
//overloading << operator
friend ostream &operator<<(ostream &out, NumDays a);
}
Run Code Online (Sandbox Code Playgroud)
我有NumDay.cpp
这包括:
ostream &operator<<(ostream& out, NumDays a)
{
// takes amount of hours, computes to work days
int temp = a.hours / 8;
//gives remainder of hours after full 8 hr workday. …
Run Code Online (Sandbox Code Playgroud) 如何传递this
给operator<<
在C++类?或者我只是做错了(可能).
例如,在下面的类中,我只有一个循环重复请求并打印一个整数.但是,cout<<this
只打印实例的地址,但我想使用定义的运算符重载.
#include<iostream>
using std::cout; using std::endl; using std::cin;
class C {
int n;
public:
C(int n) : n(n) {};
friend std::ostream& operator<<(std::ostream&, const C&);
void set_n(int i) { n = i; }
void play() {
int input;
while (true) {
cout << this;
cin >> input;
set_n(input);
}
}
};
std::ostream& operator<<(std::ostream& os, const C& c) {
cout << c.n << "\n";
return os;
}
int main(int argc, char *argv[]) {
C c …
Run Code Online (Sandbox Code Playgroud) 我在C++方面有一点经验.我知道如何重载加号和什么不是,但想重载空间操作符.
例如:
MyObject obj();
result = obj - foo; // This would be treated as a normal '-' operation.
result = obj-foo; // This would invoke code which throws an assert at runtime
Run Code Online (Sandbox Code Playgroud)
这将允许我执行我试图为我的团队制定的某些风格指南.
谢谢!
有没有办法让&&&表现为一个可以将操作连接到一个代码块的运算符?
这是编码时我想到的东西,并且想知道我是否可以以某种方式实现它.
我想要实现的是使代码更简洁.例:
for (i=0;i<n;++i)
arr[i][0]+=2; &&& arr[i][1]+=3; &&& arr[i][2]=45;
Run Code Online (Sandbox Code Playgroud)
我不想听到如何使用额外的for循环或使用{}.
我想知道在ruby中是否可以使用c ++,javascript或(如果不是其中之一)这样的运算符
编辑:我知道这是一件坏事,但我想知道是否有可能完成.我虽然这是在编码时获得乐趣的重点.