在Effective C++
第03项中,尽可能使用const.
class Bigint
{
int _data[MAXLEN];
//...
public:
int& operator[](const int index) { return _data[index]; }
const int operator[](const int index) const { return _data[index]; }
//...
};
Run Code Online (Sandbox Code Playgroud)
const int operator[]
确实有所作为int& operator[]
.
但是关于:
int foo() { }
Run Code Online (Sandbox Code Playgroud)
和
const int foo() { }
Run Code Online (Sandbox Code Playgroud)
似乎他们是一样的.
我的问题是,为什么我们用const int operator[](const int index) const
而不是int operator[](const int index) const
?
在阅读用C++编写的教程和代码时,我经常偶然发现const
关键字.
我看到它使用如下:
const int x = 5;
Run Code Online (Sandbox Code Playgroud)
我知道这意味着它x
是一个常量变量,可能存储在只读内存中.
但究竟是什么
void myfunc( const char x );
Run Code Online (Sandbox Code Playgroud)
和
int myfunc( ) const;
Run Code Online (Sandbox Code Playgroud)
?
我在文档中有一个这样定义的方法.
const T & QList::at(int i) const
Run Code Online (Sandbox Code Playgroud)
基于这个答案, 我将其描述为:
QList :: at返回对const对象T的引用,而不修改QList.
但是如果这种解释是正确的,那么它将返回对我不应该改变的对象的引用.但是这是在没有错误传递的情况下编译的(currentTime()修改属性returnedStation.time):
T referenceToReturnedObject = myQlist.at(1);
referenceToReturnedObject.time.currentTime();
Run Code Online (Sandbox Code Playgroud)
第一次const
引用是什么,它在返回时发布了什么限制T &
?
我开始使用"使用C++编程:实践和原理"一书开始学习C++
在我的第一次练习中,它给了我一些代码来运行,并说我应该得到一个错误,我应该向别人寻求帮助.这就是我正在做的事情.
错误(如Xcode所示,位于头文件中):
No matching function for call to object of type 'hash<char">'
Run Code Online (Sandbox Code Playgroud)
这是.cpp文件
#include "std_lib_facilities.h"
int main ()
{
cout << "Hello, World!\n"; //output "Hello, World!"
keep_window_open(); //wait for a character to be entered
return 0;
}
Run Code Online (Sandbox Code Playgroud)
头文件:(在Xcode中,分歧与行return hash<char*>()(s.c_str());
在线43左右.
/*
simple "Programming: Principles and Practice using C++" course header to
be used for the first few weeks.
It provides the most common standard headers (in the global namespace)
and minimal exception/error support.
Students: please don't try to …
Run Code Online (Sandbox Code Playgroud) 我在函数参数中使用pass引用传递给const字符串,如下所示:
class Super
{
void alignment(const string & str);
};
void Super::alignment(const string & str)
{
//do some stuff
}
Run Code Online (Sandbox Code Playgroud)
现在如果我使用如下,它会得到相同的结果:
void Super::alignment(string const & str)
{
//do some stuff
}
Run Code Online (Sandbox Code Playgroud)
这两种c ++定义有什么区别?
我写了这样的东西:
#define Parent C*
class C{
public:
Parent parent;
C():parent(NULL){}
};
void fun(Parent &p){
//condition..return;
fun(p->parent);
}
Run Code Online (Sandbox Code Playgroud)
当我试图使引用参数保持不变以避免对引用对象的任何意外更改时,发生了某些事情.
第一:
我说const
befor Parent
,像这样:void fun(const Parent &p)
.但它不能在这一行编译fun(p->parent);
.错误信息是:
无效的参数'候选者是:void fun(const C*&)'
然后:
我改变了这样的立场const
:void fun(Parent const &p)
突然之间,问题就消失了.为什么???有什么不同?
我正在尝试ShadeRec
使用其构造函数初始化我的类的实例:
ShadeRec(World& world);
Run Code Online (Sandbox Code Playgroud)
所以我转到它:
ShadeRec sr(*this);
Run Code Online (Sandbox Code Playgroud)
其中"this"是World类的一个实例.
我收到以下错误:
World.cpp: In member function ‘ShadeRec World::hitObjects(const Ray&) const’:
World.cpp:52: error: no matching function for call to ‘ShadeRec::ShadeRec(const World&)’
ShadeRec.h:17: note: candidates are: ShadeRec::ShadeRec(const ShadeRec&)
ShadeRec.h:15: note: ShadeRec::ShadeRec(World&)
Run Code Online (Sandbox Code Playgroud)
假设问题只是World实例具有属性const
,我该如何摆脱此错误消息?
我是学生,学习C++并发编程.我想知道如何通过我的C++程序访问功能性图形卡芯片和声卡芯片.还有另一种方法连接各自的驱动程序和我的应用程序,而无需使用外部库,如OpenGL, OpenAL等.您可以通过示例简要描述解决方案.谢谢.
我的问题标题几乎要求一切.我最近发现在C++中通过const引用传递许多值并将类中的某些方法标记为常量方法是很好的编程习惯.现在,我有一个库,我已经为自己写了一段时间,现在完全没有正确性,所以我想开始一点一点地纠正.
在哪些具体情况下,我应该使方法不变?到目前为止,我知道"getter"方法通常应该是常量的(因为一个代码中的代码不应该修改任何类变量),但我是否对所有被认为是getter的方法,或者只是特定的方法呢?除了getter方法之外,还应该使其他方法的方法保持不变?