我最近遇到了一个案例,我有一个const成员函数执行一个操作并返回一个结果.例如,
class Foo { ...
Foo add(Foo const & x) const;
}
Run Code Online (Sandbox Code Playgroud)
但其他人无意中称它为更新this对象(忽略结果):
Foo a = ...;
Foo b = ...;
a.add(b);
Run Code Online (Sandbox Code Playgroud)
(这个错误实际上是由一个不完美的重构引入的.)
有没有办法让上面的最后一行触发错误或警告?接下来最好的事情是运行时捕获,主要通过以下模板解决.但是,它会导致返回值优化,如计数器结果所示.
template<typename T>
class MustTake {
T & obj;
bool took;
public:
MustTake(T o) : obj(o), took(false) {}
~MustTake() { if (!took) throw "not taken"; }
operator T&() { took = true; return obj;}
};
struct Counter {
int n;
Counter() : n(0) {}
Counter(Counter const & c) : n(c.n+1) {}
~Counter() {}
};
Counter …Run Code Online (Sandbox Code Playgroud) 如果我从Excel(2007)复制图形并在另一个Office应用程序中选择"选择性粘贴",我会看到一个名为"Microsoft Office图形对象"的剪贴板格式,它允许与增强型图元文件不同的自定义.如何从我自己的(C++)应用程序创建这样的剪贴板对象,以便从Office应用程序获得一流的处理?
将图表数据链接到Office应用程序中并不重要 - 我只想提供另一个图表导出选项.
ClipSpy报告了几种二进制格式,包括"Excel 2007 Internal Shape","Embed Source"和"Art :: GVML ClipFormat".我想这些是专有的和私有的,但如果有人知道文档,请告诉我.
排序每个n字符串的最差复杂性是n什么?这是n它的平均时间吗?案件O(n log n)或别的什么......?
来自C#,其中类实例通过引用传递(即,当您调用函数而不是值的副本时传递引用的副本),我想知道它在C++中是如何工作的.在下面的例子中,_poly = poly,是否将poly的值复制到_poly,或者是什么?
#include <vector>
using namespace std;
class polynomial {
vector<int> _poly;
public:
void Set(vector<int> poly);
};
void polynomial::Set(vector<int> poly) {
_poly = poly; <----------------
}
Run Code Online (Sandbox Code Playgroud) push_backC++中方法名称的基本原理是什么std::vector?例如,是否存在基于堆栈的原点(push是常见的堆栈操作)?是否有预先存在的库使用这些术语来添加序列?
除了常见的术语,其他API使用append和add,insert_end似乎更内部自我一致(虽然front并back在其他地方存在).
在Stroustrup的C++编程语言(第4版)中,第27.4.2节展示了一种"线性化"菱形类层次结构的技术,以避免虚拟基类的开销.他从一个真实项目(数据代码分析器工具)的钻石图案开始:

线性版本绘制为:

和

代码大纲是:
namespace ipr {
struct Node { ... };
struct Expr : Node { ... };
struct Stmt : Expr { ... };
struct Decl : Stmt { ... };
struct Var : Decl { ... };
namespace impl {
template<class T> struct Node : T { ... };
template<class T> struct Expr : Node<T> { ... };
template<class S> struct Stmt : S { ... };
template<class D> struct Decl : Stmt<Expr<D>> …Run Code Online (Sandbox Code Playgroud) 我有一条近似曲线的折线,我想在PostScript中绘制它作为平滑曲线,支持Bezier曲线绘制.为此,我需要在每对折线点之间生成两个控制点.(我无法从源中获取原始曲线,只能获得折线.)
我从这个描述中使用了基数样条函数取得了一些成功,但其中的参数与维基百科和GDIPlus.DrawCurve不同,两者都涉及张力.MS没有细节,维基百科有不完整的细节(基数样条忽略x值?).
基于张力的控制点的公式是什么?
StringBuffer sb=null;
// Some more logic that conditionally assigns value to the StringBuffer
// Prints Value=null
System.out.println("Value="+sb);
// Throws NullPointerException
System.out.println("Value=" + sb != null ? sb.toString() : "Null");
Run Code Online (Sandbox Code Playgroud)
此问题的修复包括括号中的三元运算符:
// Works fine
System.out.println("Value=" + (sb != null ? sb.toString() : "Null"));
Run Code Online (Sandbox Code Playgroud)
这怎么可能?
String date = new java.text.SimpleDateFormat("MM-dd-yyyy").format(new java.util.date());
upload.uploadfile("192.168.0.210", "muruganp", "vm4snk", "/home/media/Desktop/FTP Upload/+date+"_RB.zip"", "/fileserver/filesbackup/Emac/+date+"_RB.zip"");
Run Code Online (Sandbox Code Playgroud)
uploadfile是将文件10-20-2010_RB.zip上传到服务器位置的功能.
但是因为我的路径中有字符串"date",所以很少出现非法表达式错误.
如果我尝试如下相同,该程序工作正常.
upload.uploadfile("192.168.0.210", "muruganp", "vm4snk", "/home/media/Desktop/FTP Upload/20-10-2010_RB.zip", "/fileserver/filesbackup/Emac/20-10-2010_RB.zip");
Run Code Online (Sandbox Code Playgroud)
出于某些原因,我强迫在文件路径中插入字符串.我怎样才能达到最终结果?好心提醒.
对于代码:
#define e 2.71828183;
double p ( int x )
{
return 1 / ( 1 + pow ( e, -1.0 * x ) );
}
Run Code Online (Sandbox Code Playgroud)
我明白了:
math.cpp: In function ‘double p(int)’:
math.cpp:11: error: expected ‘)’ before ‘;’ token
math.cpp:11: error: expected ‘)’ before ‘;’ token
math.cpp:11: error: expected primary-expression before ‘,’ token
math.cpp:11: error: expected ‘;’ before ‘)’ token
Run Code Online (Sandbox Code Playgroud)