#define ROW 3
#define COL 4
class Matrix
{
private:
int mat[ROW][COL];
//.....
//.....
};
int main()
{
Matrix m;
int a = m[0][1]; // reading
m[0][2] = m[1][1]; // writing
}
Run Code Online (Sandbox Code Playgroud)
我认为直接不能超载[] [].
我想我必须间接地做,但如何实现它?
在C++中,我可以通过执行以下操作来更改特定类的运算符:
MyClass::operator==/*Or some other operator such as =, >, etc.*/(Const MyClass rhs) {
/* Do Stuff*/;
}
Run Code Online (Sandbox Code Playgroud)
但是由于C中没有类(默认内置),所以,我怎样才能对一般函数进行运算符重载?
例如,如果我没记错的话,导入stdlib.h会给你 - >运算符,它只是(*strcut_name).struct_element的语法糖.
那么我怎么能在C中做到这一点?
谢谢.
我有重载operator <<为模板类的问题.我使用的是Visual Studio 2010,这是我的代码.
#ifndef _FINITEFIELD
#define _FINITEFIELD
#include<iostream>
namespace Polyff{
template <class T, T& n> class FiniteField;
template <class T, T& n> std::ostream& operator<< (std::ostream&, const FiniteField<T,n>&);
template <class T, T& n> class FiniteField {
public:
//some other functions
private:
friend std::ostream& operator<< <T,n>(std::ostream& out, const FiniteField<T,n>& obj);
T _val;
};
template <class T, T& n>
std::ostream& operator<< (std::ostream& out, const FiniteField<T,n>& f) {
return out<<f._val;
}
//some other definitions
}
#endif
Run Code Online (Sandbox Code Playgroud)
在主要我只是
#include"FiniteField.h"
#include"Integer.h"
#include<iostream>
using std::cout;
using …
Run Code Online (Sandbox Code Playgroud) c++ templates operator-overloading visual-studio-2010 friend-function
作为一个玩具示例,我有一个类,它简单地将一个向量或矩阵包装在一个对象中,并包含一个创建时间的时间戳.我试图超载subsref
这样
()
引用的工作方式与标准向量和矩阵类型完全相同{}
引用的工作方式与引用完全相同()
(换句话说,与单元格无关).
引用允许我访问对象的私有属性以及非技术属性的其他字段.码:
classdef TimeStampValue
properties (Access = private)
time;
values;
end
methods
%% Constructor
function x = TimeStampValue(values)
x.time = now();
x.values = values;
end
%% Subscripted reference
function x = subsref(B, S)
switch S.type
case '()'
v = builtin('subsref', B.values, S);
x = TimeStampValue(v);
case '{}'
S.type = '()';
v = builtin('subsref', B.values, S);
x = TimeStampValue(v);
case '.'
switch S.subs
case 'time'
x …
Run Code Online (Sandbox Code Playgroud) 是否有可能在Swift中重载+ =运算符来接受例如CGFloat
参数?如果是这样,怎么样?
我的方法(下面)不起作用.
infix operator += { associativity left precedence 140 }
public func +=(inout left: CGFloat, right: CGFloat) {
left = left + right
}
Run Code Online (Sandbox Code Playgroud)
我有以下代码:
#include <iostream>
#include <boost\lexical_cast.hpp>
struct vec2_t
{
float x;
float y;
};
std::istream& operator>>(std::istream& istream, vec2_t& v)
{
istream >> v.x >> v.y;
return istream;
}
int main()
{
auto v = boost::lexical_cast<vec2_t>("1231.2 152.9");
std::cout << v.x << " " << v.y;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我从Boost收到以下编译错误:
错误1错误C2338:目标类型既不是std :: istream
able nor std::wistream
能够的
这看起来很简单,过去一小时我一直在桌子上打我的头.任何帮助,将不胜感激!
编辑:我正在使用Visual Studio 2013.
我将所有重载的比较运算符写入类是一个常见的任务,所以我编写了一个模板类,如果派生类实现了==和<,则实现<,<=,> =,!=.它工作但有很多演员和不那么明显的"奇怪的重复模板模式",所以我想知道是否有更简单的解决方案?
template <class Derived>
class Comparable
{
public:
bool operator!=(const Comparable<Derived>& other) {
return !(static_cast<Derived*>(this)->operator==
(*static_cast<const Derived*>(&other)));
}
bool operator<=(const Comparable<Derived>& other) {
return (static_cast<Derived*>(this)->operator==
(*static_cast<const Derived*>(&other)))
|| (static_cast<Derived*>(this)->operator<
(*static_cast<const Derived*>(&other)));
}
bool operator>(const Comparable<Derived>& other) {
return !(static_cast<Derived*>(this)->operator==
(*static_cast<const Derived*>(&other)))
&& !(static_cast<Derived*>(this)->operator<
(*static_cast<const Derived*>(&other)));
}
bool operator>=(const Comparable<Derived>& other) {
return !(static_cast<Derived*>(this)->operator<
(*static_cast<const Derived*>(&other)));
}
};
Run Code Online (Sandbox Code Playgroud) Delphi文档说可能会使Inc和Dec运算符超载; 我认为没有有效的方法可以做到这一点.以下是尝试重载Inc运算符; 一些尝试导致编译错误,一些尝试导致运行时访问冲突(Delphi XE):
program OverloadInc;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
TMyInt = record
FValue: Integer;
// class operator Inc(var A: TMyInt); DCC error E2023
class operator Inc(var A: TMyInt): TMyInt;
property Value: Integer read FValue write FValue;
end;
class operator TMyInt.Inc(var A: TMyInt): TMyInt;
begin
Inc(A.FValue);
Result:= A;
end;
type
TMyInt2 = record
FValue: Integer;
class operator Inc(A: TMyInt2): TMyInt2;
property Value: Integer read FValue write FValue;
end;
class operator TMyInt2.Inc(A: TMyInt2): TMyInt2;
begin
Result.FValue:= A.FValue + 1; …
Run Code Online (Sandbox Code Playgroud) 我很难理解stroustrup对于必须遇到什么困难的解释,如果运算符重载'.' 曾经被允许.
请参阅Bjarne Stroustrup的这句话:
运营商.(dot)原则上可以使用与 - >相同的技术重载.但是,这样做会导致对操作是否意味着对象重载的问题.或者提到的对象.例如:
class Y {
public:
void f();
// ...
};
class X { // assume that you can overload .
Y* p;
Y& operator.() { return *p; }
void f();
// ...
};
void g(X& x)
{
x.f(); // X::f or Y::f or error?
}
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,为什么在执行时会有任何混淆x.f()
?
Y& operator.() { return *p; }
Run Code Online (Sandbox Code Playgroud)
这是我的想法:
Y& operator.()( return *p; }
不应该被称为obivous和直觉吗?*p
指向Y类型的对象,因此Y::f()
应该最终调用(不是X::f()
)我在stroustup的解释中缺少什么?为什么不直截了当?
想象一下这样的代码:
struct Foo
{
int foo{0};
};
Foo operator+(const Foo& lhs, const Foo& rhs)
{
Foo ret;
ret.foo = lhs.foo + rhs.foo;
return ret;
}
struct Bar
{
int bar{0};
};
Bar operator+(const Bar& lhs, const Bar& rhs)
{
Bar ret;
ret.bar = lhs.bar + rhs.bar;
return ret;
}
template<typename... Ts>
struct Fooz : public Ts...
{
};
template<typename... Ts>
Fooz<Ts...> operator+(const Fooz<Ts...>& lhs, const Fooz<Ts...>& rhs)
{
// how can you call base class's operator+ here?
}
int main(int …
Run Code Online (Sandbox Code Playgroud)