" Effective C++ "第3项说"尽可能使用const",它给出了一个例子:
const Rational operator*(const Rational& lhs,
const Rational& rhs);
Run Code Online (Sandbox Code Playgroud)
防止客户端犯下这样的暴行:
Rational a, b, c;
...
(a * b) = c; // invoke operator= on the result of a*b!
Run Code Online (Sandbox Code Playgroud)
但是,函数的非参考返回值是否已经是一个rvalue?那么为什么还要这么做呢?
我正在扩展的程序使用std::pair<>了很多.
在我的代码中有一点,编译器抛出一个相当大的:
非静态const成员'const Ptr std :: pair,const double*> :: first'不能使用默认赋值运算符
我不确定这指的是什么?Ptr类缺少哪些方法?
导致此问题的原始调用如下:
vector_of_connections.pushback(pair(Ptr<double,double>,WeightValue*));
Run Code Online (Sandbox Code Playgroud)
它放在std::Pair<Ptr<double,double>, WeightValue*>一个向量上的位置,其中WeightValue*是一个来自大约3个函数的const变量,Ptr<double,double>它取自一个在另一个向量上工作的迭代器.
供将来参考,Ptr<double,double>是指向Node对象的指针.
除了确保它们无法更改(编译器错误的调整)之外,JIT是否对const本地进行任何优化?
例如.
public static int Main(string[] args)
{
const int timesToLoop = 50;
for (int i=0; i<timesToLoop; i++)
{
// ...
}
}
Run Code Online (Sandbox Code Playgroud) 如何检查PHP类中是否定义了常量?
class Foo {
const BAR = 1;
}
Run Code Online (Sandbox Code Playgroud)
有类似于property_exists()或method_exists()类常量的东西吗?或者我可以使用defined("Foo::BAR")?
我在使用特定的代码时遇到了一些问题,如果有人能够就此问题启发我,我将非常感激,我已经在以下示例中将问题排除在外:
#include <iostream>
using namespace std;
class testing{
int test();
int test1(const testing& test2);
};
int testing::test(){
return 1;
}
int testing::test1(const testing& test2){
test2.test();
return 1;
}
Run Code Online (Sandbox Code Playgroud)
那么可能导致以下错误:
test.cpp:15:错误:将'const testing'作为'int testing :: test()'的'this'参数传递,丢弃限定符
非常感谢!
我很好奇为什么const构件可以在构造函数中修改.
初始化中是否有任何标准规则可以覆盖成员的"常量"?
struct Bar {
const int b = 5; // default member initialization
Bar(int c):b(c) {}
};
Bar *b = new Bar(2); // Problem: Bar::b is modified to 2
// was expecting it to be an error
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我在类中有一些容器,例如vector或map,它包含生活在堆上的对象的shared_ptr.
例如
template <typename T>
class MyExample
{
public:
private:
vector<shared_ptr<T> > vec_;
map<shared_ptr<T>, int> map_;
};
Run Code Online (Sandbox Code Playgroud)
我希望有一个这个类的公共接口,有时会将shared_ptrs返回给const对象(via shared_ptr<const T>),有时shared_ptr<T>我允许调用者改变对象.我想要逻辑const正确性,所以如果我将方法标记为const,它就不能更改堆上的对象.
问题:
1)我对shared_ptr<const T>和的可互换性感到困惑shared_ptr<T>.当有人经过一个shared_ptr<const T>shared_ptr的进级,做我把它存储为一个shared_ptr<T>或shared_ptr<const T>载体内,映射或修改我的地图,矢量类型(例如insert_elemeent(shared_ptr<const T>OBJ)?
2)如下实例化类更好MyExample<const int> 吗?这看起来过于严格,因为我永远无法回归shared_ptr<int>?
对于读取复杂指针声明,有左右规则.
但是这条规则没有提到如何阅读const修饰语.
例如,在简单的指针声明中,const可以通过多种方式应用:
char *buffer; // non-const pointer to non-const memory
const char *buffer; // non-const pointer to const memory
char const *buffer; // equivalent to previous declartion
char * const buffer = {0}; // const pointer to non-const memory
char * buffer const = {0}; // error
const char * const buffer = {0}; // const pointer to const memory
Run Code Online (Sandbox Code Playgroud)
现在用const指针声明指针怎么样?
char **x; // no const;
const char **x;
char * …Run Code Online (Sandbox Code Playgroud) 为什么不能将常量设置为变量本身的对象的属性?
const a = 'constant' // all is well
// set constant property of variable object
const window.b = 'constant' // throws Exception
// OR
var App = {}; // want to be able to extend
const App.goldenRatio= 1.6180339887 // throws Exception
Run Code Online (Sandbox Code Playgroud)
为什么通过引用传递的常数突然变得可变?编辑:我知道应用程序不会(或者更确切地说......)不可变; 这只是一个观察......
(function() {
const App;
// bunch of code
window.com_namespace = App;
}());
window.com_namespace; // App
window.com_namespace = 'something else';
window.com_namespace; // 'something else'
Run Code Online (Sandbox Code Playgroud)
如何使用这些限制来构建一个包含常量的组织良好,可扩展,面向对象的单一命名空间的库?
编辑:我相信zi42,但我只想问为什么
const ×10
c++ ×6
c++11 ×2
php ×2
c ×1
c# ×1
c++14 ×1
coding-style ×1
declaration ×1
javascript ×1
jit ×1
object ×1
properties ×1
rvalue ×1
shared-ptr ×1