为什么C#不允许在同一行上使用const和static?在Java中,您必须将字段声明为"static"和"final"以充当常量.为什么C#不允许你将const声明为final?
我进一步区分在Java中,每个接口都是公共的和抽象的,无论是否显式声明.const本质上不是有效的静态吗?为什么C#对此不以为然?
我正在研究我的代码的const正确性,只是想知道为什么这个代码编译:
class X
{
int x;
int& y;
public:
X(int& _y):y(_y)
{
}
void f(int& newY) const
{
//x = 3; would not work, that's fine
y = newY; //does compile. Why?
}
};
int main(int argc, char **argv)
{
int i1=0, i2=0;
X myX(i1);
myX.f(i2);
...
}
Run Code Online (Sandbox Code Playgroud)
据我所知,f()正在改变对象myX,尽管它说是const.当我分配给y时,如何确保我的编译器抱怨?(Visual C++ 2008)
非常感谢!
我试图为我的目的包装类似于Qt的共享数据指针的东西,并且经过测试我发现当应该调用const函数时,选择了它的非const版本.
我正在使用C++ 0x选项进行编译,这里是一个最小的代码:
struct Data {
int x() const {
return 1;
}
};
template <class T>
struct container
{
container() {
ptr = new T();
}
T & operator*() {
puts("non const data ptr");
return *ptr;
}
T * operator->() {
puts("non const data ptr");
return ptr;
}
const T & operator*() const {
puts("const data ptr");
return *ptr;
}
const T * operator->() const {
puts("const data ptr");
return ptr;
}
T* ptr;
};
typedef container<Data> testType;
void …Run Code Online (Sandbox Code Playgroud) 为什么C中的以下代码有效?
const char* str = NULL;
str = "test";
str = "test2";
Run Code Online (Sandbox Code Playgroud)
由于str是指向常量字符的指针,为什么我们允许为它分配不同的字符串文字?此外,我们如何保护str不被修改?看起来这可能是一个问题,例如,我们后来将str分配给一个更长的字符串,最后写入另一部分内存.
我应该补充一点,在我的测试中,我在每个作业之前和之后打印出str的内存地址,但它从未改变过.因此,尽管str是指向const char的指针,但内存实际上正在被修改.我想知道这可能是C的遗留问题吗?
在以下C++函数中:
void MyFunction(int age, House &purchased_house)
{
...
}
void MyFunction(const int age, House &purchased_house)
{
...
}
Run Code Online (Sandbox Code Playgroud)
哪个更好?
在两者中,'年龄'都是按价值传递的.我想知道'const'关键字是否有必要:对我来说似乎是多余的,但也很有用(作为额外的指示,变量不会改变).
有没有人对上述哪个(如果有的话)更好有任何意见?
考虑一下
void f(vector<const T*>& p)
{
}
int main()
{
vector<T*> nonConstVec;
f(nonConstVec);
}
Run Code Online (Sandbox Code Playgroud)
下列情况不compile.The事情是vector<T*>不能转换到vector <const T*>,这不合逻辑对我来说,因为存在从隐式转换T*到const T*.为什么是这样 ?
vector<const T*>也不能转换为vector <T*>,但这是预期的,因为const T*无法隐式转换为T*.
在Delphi中,您可以通过传递参数来加速代码const,例如
function A(const AStr: string): integer;
//or
function B(AStr: string): integer;
Run Code Online (Sandbox Code Playgroud)
假设两个函数内部具有相同的代码,它们之间的速度差异可以忽略不计,我怀疑它甚至可以用循环计数器测量,如:
function RDTSC: comp;
var
TimeStamp: record case byte of
1: (Whole: comp);
2: (Lo, Hi: Longint);
end;
begin
asm
db $0F; db $31;
mov [TimeStamp.Lo], eax
mov [TimeStamp.Hi], edx
end;
Result := TimeStamp.Whole;
end;
Run Code Online (Sandbox Code Playgroud)
其原因是const函数A 中的所有操作都是为了防止引用计数AStr增加.
但是增量只需要我的多核CPU的一个核心的一个周期,所以......
我为什么要打扰const?
我读过Scott Meyers编写的Effective C++ 3rd Edition.
本书的第3项" const尽可能使用",如果我们想要防止rvalues被意外地分配给函数的返回值,那么返回类型应该是const.
例如,增量函数iterator:
const iterator iterator::operator++(int) {
...
}
Run Code Online (Sandbox Code Playgroud)
然后,防止了一些事故.
iterator it;
// error in the following, same as primitive pointer
// I wanted to compare iterators
if (it++ = iterator()) {
...
}
Run Code Online (Sandbox Code Playgroud)
但是,std::vector::iteratorGCC中的迭代器不返回const值.
vector<int> v;
v.begin()++ = v.begin(); // pass compiler check
Run Code Online (Sandbox Code Playgroud)
这有什么理由吗?
我想以线程安全的方式为名为order的类生成标识符.下面的代码无法编译.我知道原子类型没有复制构造函数,我认为这解释了为什么这段代码不起作用.有没有人知道实际让这段代码工作的方法?我还在学习,所以如果我在错误的轨道上也请告诉我(如果是的话,如果你能指出我的替代方法,我将不胜感激).谢谢!
#include <atomic>
#include <iostream>
class order {
public:
order() { id=c.fetch_add(1); }
int id;
private:
static std::atomic<int> c;
};
std::atomic<int> order::c = std::atomic<int>(0);
int main() {
order *o1 = new order();
order *o2 = new order();
std::cout << o1->id << std::endl; // Expect 0
std::cout << o2->id << std::endl; // Expect 1
}
Run Code Online (Sandbox Code Playgroud)
编译以上结果会出现以下错误:
order.cpp:45:51: error: use of deleted function
‘std::atomic<int>::atomic(const std::atomic<int>&)’
In file included from order.cpp:3:0:
/usr/include/c++/4.7/atomic:594:7: error: declared here
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码段:
class A
{
public:
void nonConstFun()
{
}
};
class B
{
private:
A a_;
A * pA_;
public:
void fun() const
{
pA_->nonConstFun();
//a_.nonConstFun(); // Gives const related error
}
};
int main()
{
B b;
b.fun();
}
Run Code Online (Sandbox Code Playgroud)
在这里,我期望编译器在编译时因为缺少常量来调用A::nonConstFun()内部,B::fun()而不管A对象的类型如何.
但是编译器会抱怨该对象,而不是指针.为什么?我在Windows 10上使用VS2017.