int a, b, c;
//do stuff. For e.g., cin >> b >> c;
c = a + b; //works
c = operator+(a,b); //fails to compile, 'operator+' not defined.
Run Code Online (Sandbox Code Playgroud)
这另一方面起作用 -
class Foo
{
int x;
public:
Foo(int x):x(x) {}
Foo friend operator+(const Foo& f, const Foo& g)
{
return Foo(f.x + g.x);
}
};
Foo l(5), m(10);
Foo n = operator+(l,m); //compiles ok!
Run Code Online (Sandbox Code Playgroud)
可能重复:
重载运算符 - >
嗨,
我已经看到operator->()它在被评估后被链接(重新应用),例如:
struct Bar
{
Bar() : m_str("Hello world!") {}
const string* operator->() const { return &m_str; }
string m_str;
};
struct Foo
{
const Bar& operator->() const { return m_bar; }
Bar m_bar;
};
int main()
{
Foo f;
cout << f->c_str() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
工作得很好,这需要三个operator->()进行评估- Foo::operator->(),Bar::operator->()和普通指针分辨率.
但它不适用于中间的Foo::operator->()指针- 如果返回指向Bar而不是引用的指针,它就不会编译.auto_ptr<auto_ptr<string>> 例如,同样如此.
它是否特定于非重载,operator->()因此它只应用一次而不会导致链接?可以在不使用的情况下使代码低于工作(*ptr2)-> ...吗?
int …Run Code Online (Sandbox Code Playgroud) 我很难形成一个我想问的问题,所以让我举个例子:
假设我们正在开发一个3d系统,我们已经定义了一个矢量类Vec3.我们重载一些算术运算符以获得明显的重复.其中我们重载*运算符,以便它返回两个向量的点积.现在我们的代码看起来像这样:
class Vec3{
private:
float x, y, z;
public:
float operator*(const Vec3&) const; //dot product
.
.
.
Run Code Online (Sandbox Code Playgroud)
现在说我们希望能够通过使用*运算符来扩展我们的向量,比如浮点数.这可以通过声明以下内容来完成:
Vec3 operator*(const float) const;
friend Vec3 operator*(const float, const Vec3&);
Run Code Online (Sandbox Code Playgroud)
这有两个重载,我想知道是否有办法只用一个,即我们声明这而不是上面两行:
friend Vec3 operator*(const Vec3&, const Vec3&);
Run Code Online (Sandbox Code Playgroud)
然后为Vec3 ctor添加默认值以处理来自float的转换.
最后一个例子适用于:
Vec3 someVec3 = 1.0 * otherVec3; //through friend Vec3 operator*(const float, const Vec3&)
Run Code Online (Sandbox Code Playgroud)
但不是为了这个:
Vec3 someVec3 = otherVec3 * 1.0;
Run Code Online (Sandbox Code Playgroud)
因为编译器不知道要使用哪两个:
friend Vec3 operator*(const float, const Vec3&); //the one we want to use
or
float operator*(const Vec3&) const; …Run Code Online (Sandbox Code Playgroud) 我正在学习Java编程的第一个学期,我们刚刚介绍了条件运算符(?:)条件.我有两个问题,似乎是希望我在另一个中"嵌套"条件运算符,这些我可以轻松(但又繁琐)使用if-else-if语句.
1)"假设月份是一个int变量,其值为1或2或3或5 ...或11或12.写一个值为"jan"或"feb"或"mar"或"apr"的表达式或"may"或"jun"或"jul"或"aug"或"sep"或"oct"或"nov"或"dec"基于月份的值.(因此,如果月份的值为4,那么表达式的值将是"apr".)."
我的想法看起来像这样:
(month==1)?"jan":(month==2)?"feb": (month==3)?"mar": (month==4)?"apr":
(month==5)?"may":(month==6)?"jun": (month==7)?"jul":(month==8)?"aug":
(month==9)?"sep": (month==10)?"oct": (month==11)?"nov": (month==12)?"dec":
Run Code Online (Sandbox Code Playgroud)
(我知道这不是一个完整的表达,但我不确定如何用操作符来表达处理这么多条件.)
2)假设信用是一个int值变量,其值为0或正数.根据学分的价值写出一个价值为"新生"或"二年级"或"初级"或"高级"的表达.特别是:如果学分的值小于30,则表达式的值为"新生"; 30-59将是"二年级",60-89将是"大三",90或更多将是"高级".
再一次,我一直在玩弄,我能想到的最好的东西就像(并且我的probs缺少一些必要的括号):
credits < 30 ? "freshman": credits >= 30 && <=59 ?
"sophomore": credits >= 60 && <= 89 ? "junior": "senior"
Run Code Online (Sandbox Code Playgroud)
我在谷歌上搜索了数据库,但我并不认为这个问题确实存在问题.如果我错了,请原谅我.程序(CodeLab)不会采用Switch-Case或if-else-if解决方案,总是建议我应该使用条件?:运营商,但我看到的每个地方我都没有想出如何操作操作员来处理这么多条件.我们在这本书中已经不远了,所以如果你们能帮助我找到解决方案,那么如果它能够与我迄今为止所学到的一点点相媲美,那就太棒了.
我们有以下课程.我需要解释一些代码部分.
class CPoint3D
{
public:
double x, y, z;
CPoint3D (double dX = 0.0, double dY = 0.0, double dZ = 0.0)
: x(dX), y(dY), z(dZ) {}
//what means these lines of code?
CPoint3D operator + (const CPoint3D& point) const;
CPoint3D operator - (const CPoint3D& point) const;
CPoint3D operator * (double dFactor) const;
CPoint3D operator / (double dFactor) const;
};
Run Code Online (Sandbox Code Playgroud)
我想用
CPoint3D operator + (const CPoint3D& point) const;
函数我可以轻松地添加/减去/乘/除除CPoint3D类的实例?
有人可以用例子解释一下吗?谢谢!
当我在C#中处理一些较旧的代码时,我遇到了一个令我烦恼的代码.
没有太多的麻烦,它是这样的:
private string foo(string _text)
{
/* some manipulation on _text */
return _text = Server.HtmlDecode(_text);
}
Run Code Online (Sandbox Code Playgroud)
这是让我烦恼的最后一句话; 我来自C背景,我可以理解代码实际上是试图返回一个已解码的_text变量.赋值运算符的值也是左操作数,所以我可以看到它.
然而,我仍然觉得它很令人厌烦.
它是C#中的纵向练习,我需要习惯吗?
对我来说,最后一行应该是
return Server.HtmlDecode(_text);
Run Code Online (Sandbox Code Playgroud)
而不是作业表达.是否有更深入的C#功能,我不知道?
我正在阅读增强文档,并在http://www.boost.org/doc/libs/1_54_0/doc/html/lambda/le_in_details.html上查看以下内容:
无法重载的运算符
有些运算符根本不能重载(::,.,.*).对于某些运算符,对返回类型的要求会阻止它们重载以创建lambda仿函数.这些运算符是 - >., - >,new,new [],delete,delete []和?:(条件运算符).
那么运营商是->.什么?我尝试了谷歌和http://www.symbolhound.com/,但没有得到任何有用的东西,在N3337上搜索得到1 ->个句子结尾的结果,并且Visual Studio 2012将无法编译:
class xT {
bool operator ->. () {} /* fail */
};
std::string* p;
p->.size(); /* fail */
std::auto_ptr<std::string> a;
a->.size(); /* fail */
Run Code Online (Sandbox Code Playgroud)
我相信,笔者特意写了->.,因为->和.也包括在内,但什么是->.,为什么它是在这里?
以下编译在VS 2005中,但未在2010年或2012年编译:
#include <string>
template <typename TT> TT getAs();
template <> std::string getAs() { return "bye"; }
template <> int getAs() { return 123; }
class Foo
{
public:
template <typename TT>
TT getAs() const { return ::getAs<TT>(); }
template <typename TT>
operator TT() const { return ::getAs<TT>(); }
};
Foo tempFoo() { return Foo(); }
int main()
{
Foo foo;
std::string testStringLocal = foo; // OK in 2010, FAIL in 2012
int testIntTemp = tempFoo(); // OK
std::string testStringTemp …Run Code Online (Sandbox Code Playgroud) 我被许多程序员警告不要使用平方根功能,而是将数字提升到半功率.我的问题是双重的:
这样做的感知/真实表现有什么好处?为什么它更快?
如果它真的更快,为什么平方根函数甚至存在?
#[derive(Default)]
struct SomeOptions {
foo: i32,
bar: f32,
}
fn main() {
let options = SomeOptions { foo: 42, ..Default::default() };
}
Run Code Online (Sandbox Code Playgroud)
..返回值的前缀Default::default()是什么?为什么这里需要它?它几乎看起来像是一个传播运营商,但我不确定.我理解..Default::default()正在做什么 - 用默认值填充剩余的struct参数SomeOptions,但不是如何..工作.这个运营商的名字是什么?
operator-keyword ×10
c++ ×6
c# ×2
string ×2
boost ×1
conditional ×1
default ×1
function ×1
java ×1
math ×1
return ×1
rust ×1
square-root ×1
templates ×1
vb.net ×1