我有一个带有a const和非const重载的类的C ++成员函数。
Class Example {
public:
int const & Access() const;
int & Access();
[...]
};
Run Code Online (Sandbox Code Playgroud)
我希望首选const版本,因为在我的代码中性能要好得多,非const版本会导致创建基础共享对象的副本以允许修改。
现在,如果我的调用者有一个非常量Example对象,则即使未修改结果int,也会使用非常量Access()方法。
Example example;
if ( modify ) {
example.Access() = 23; // Need to perform expensive copy here.
} else {
cout << example.Access(); // No need to copy, read-only access.
}
Run Code Online (Sandbox Code Playgroud)
有没有一种方法,例如区分返回值的左值和右值使用,也许使用带有模板的完美转发,以在C ++ 17中创建类似的机制,从而允许调用者拥有一种语法,而编译器仅使用非语法。 const版本是否修改了返回值?
我需要这样做的另一个示例是operator -> ()我有一个const和非const版本的运算符。当调用方法时,const我希望编译器更喜欢的const版本operator -> ()。
Class Shared {
public: …Run Code Online (Sandbox Code Playgroud) 在 Qt 4 中,默认情况下,在对话框中使用 Tab 键会将默认按钮更改为使用 Tab 键的按钮。这使得标签看起来有点丑陋和分散注意力,因为在标签导航期间会发生额外的重绘,这会分散用户的注意力。
有没有办法禁用此 Qt 功能并保留选项卡导航未修改的默认按钮?
我有一个我在C++中编译的文件,其中我希望有一个字符串,其值是编译时文件的内容.
换句话说,我想#include文件,但有双引号.
我怎么能用C++做到这一点?
现在如果该文件包含双引号作为其文本的一部分,如何将这些文件转义?
在Botan v1.8.8上运行gcc v3.4.6在成功构建Botan并运行自检后,我得到以下编译时错误构建我的应用程序:
../../src/Botan-1.8.8/build/include/botan/secmem.h: In member function `Botan::MemoryVector<T>& Botan::MemoryVector<T>::operator=(const Botan::MemoryRegion<T>&)':
../../src/Botan-1.8.8/build/include/botan/secmem.h:310: error: missing template arguments before '(' token
Run Code Online (Sandbox Code Playgroud)
这个编译器错误告诉我什么?这是secmem.h的一个片段,包括第310行:
[...]
/**
* This class represents variable length buffers that do not
* make use of memory locking.
*/
template<typename T>
class MemoryVector : public MemoryRegion<T>
{
public:
/**
* Copy the contents of another buffer into this buffer.
* @param in the buffer to copy the contents from
* @return a reference to *this
*/
MemoryVector<T>& operator=(const MemoryRegion<T>& in)
{ …Run Code Online (Sandbox Code Playgroud) C++是否有内置的部分STL来交换两个数值而不是:
int tmp = var1;
var1 = var2;
var2 = tmp;
Run Code Online (Sandbox Code Playgroud)
像这样的东西:
std::swapValues(var1, var2);
Run Code Online (Sandbox Code Playgroud)
swapValues是一个模板.
我为我定义了 mem_malloc() 和 mem_free(),我想用它们来替换 malloc() 和 free() 以及 C++ 的 new 和 delete。
我将它们定义如下:
extern "C" {
extern void *mem_malloc(size_t);
extern void mem_free(void *);
void *
malloc(size_t size) {
return mem_malloc(size);
}
void
free(void *memory) {
mem_free(memory);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到两个链接错误:
[user@machine test]$ g++ -m32 -pthread main.cpp -static libmemnmf-O.a
/usr/lib/../lib/libc.a(malloc.o): In function `free':
(.text+0x153c): multiple definition of `free'
/tmp/ccD2Mgln.o:main.cpp:(.text+0x842): first defined here
/usr/lib/../lib/libc.a(malloc.o): In function `malloc':
(.text+0x3084): multiple definition of `malloc'
/tmp/ccD2Mgln.o:main.cpp:(.text+0x856): first defined here
libmemnmf-O.a(mem_debug.o): In function `mem_init_debug_routines':
mem_debug.c:(.text+0x83c): undefined …Run Code Online (Sandbox Code Playgroud) 什么是linux命令来获取正在运行的进程的堆栈而不必在调试器中附加到它?
我以前见过有人这样做过,但是不要记得他们以前做过的命令.
这非常方便,可以快速查看程序正在执行的操作,而无需在调试器中附加到它的开销,以获取堆栈跟踪以查看它当前所处的位置.
当没有待处理的异常在堆栈中处理更高时,C++标准对于以下代码应该说什么呢?
try {
throw;
} catch (...) {
cerr << "Caught exception." << endl;
}
Run Code Online (Sandbox Code Playgroud)
没有任何对象的投掷会被抓住吗?
如何使用Android的MapView对象在Google地图中添加搜索栏?
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mapmain);
mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView,
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
}
Run Code Online (Sandbox Code Playgroud) 我想在C接口函数中使用'const'来注意某些char*参数不被函数修改.
将此代码移植到各种平台会导致什么问题?在C代码中支持'const'是否相当标准?这什么时候正式成为C标准?