S**I 发帖数: 15689 | 1 no, const member can only be initialized
cons |
|
c**********e 发帖数: 2007 | 2 So there is no assignment operator for a class with a const data member? |
|
B******5 发帖数: 4676 | 3 如果const可以被assign不就违背本来的意愿了? |
|
t****t 发帖数: 6806 | 4 if you want to assign it, why do you make it const in the first place? |
|
g*******o 发帖数: 156 | 5 i didn't get what u really wanna last time, since it's kinda rare and not
that good practice in C++.
Here's my solution for you and i've tested it.
Like my last post, do the operator= overload. In operator=, define a int*pa=
(int*)&a; (or using reference).
Now we've escaped the compiler and can do assignmemt to const: *pa=xxx, xxx
may be member of object you passed from parameters at RHS of '='
Just like the other posts said, this is not a good way in C++, somehow
common in C... |
|
p****n 发帖数: 69 | 6 比如实现BST的时候,内部节点用auto_ptr或者shared_ptr封装起来,避免naked
pointer;用copy_and_swap idiom(from effective c++)实现exception safety;
member function尽量用const,etc
还是说为了突出BST的算法而尽量简化代码?
Thanks |
|
h*******e 发帖数: 1377 | 7 看了几个人的没发现太好的解法,自己的O(n) time const space又有点长46行 大家有
没有
好点的解法。 |
|
t**r 发帖数: 3428 | 8 c++最变态的是 头文件,模板,const, static 这几件事情。
可惜c++11, 14, 17没有在这几方面改进。
没有任何理由回到c++的世界。
底层编程c足以。linus 用c写linux也不需要cpp.
上层编程就是jvm的天下。
客户端就是js.
写测试用python.
写机器学习用scala。
c++只会慢慢衰退 |
|
a********m 发帖数: 15480 | 9 前两个不争论。const和static有啥问题。。。。 |
|
k****f 发帖数: 3794 | 10 你编译都有问题吧
带const的string_rep不能修改普通的成员c。 |
|
L*********r 发帖数: 92 | 11 reference must be initialized.
const must be initialized by a ctor. |
|
S*****n 发帖数: 227 | 12 换句话说,如果他的Date class里
cache& c; 的声明 换成
cache* pc; 当然,后面的所有实现里的c.换成pc->
这个玩意儿编译好象还能过。
总之Date::string_rep()的const看起来很没有道理。
懒得看了。 |
|
L*********r 发帖数: 92 | 13 Hehe,
const Date d4;
is what I am talking |
|
m***t 发帖数: 254 | 14 谢谢回答, 这就是我不明白的地方, 如果是const &c, c reference的内容也不能变
吧, 而不是说c本身这个
reference不能变。 在这里貌似说c reference的内容可以随便变, 所以不太明白。
这是讲mutable的时候提到的,貌似BJ认为用这种reference和用mutable等效。 |
|
S*****n 发帖数: 227 | 15 不过话也难说,Date的ctor没给出code,没准里面有个没参数的ctor,里面
构造个古怪的cache初值,然后把c指向之,所以
const Date d4也许能过编译,haha.. |
|
|
|
c***d 发帖数: 996 | 18 ☆─────────────────────────────────────☆
Foxwell (大熊星座) 于 (Mon Mar 5 23:02:56 2007) 提到:
const int i=125;
int *j = const_cast (&i);
(*j) = 256;
printf("%d %d\n", i, *j);
以上代码为什么输出的是:
125 256
而不是:
256 256
另外,i到底有没有分配空间?如果没有,(*j)志向的是哪里?
谢谢!
☆─────────────────────────────────────☆
Pontiff (树) 于 (Mon Mar 5 23:07:19 2007) 提到:
你把优化去掉看看?
第一个输出是125应该是constant propagation优化
i应该分配空间,因为需要保证有&i的操作语义
☆─────────────────────────────────────☆
Pontiff (树) 于 (Mon Mar 5 23:13:28 2007) 提到:
S |
|
a******e 发帖数: 95 | 19 For fun only. Not portable.
void Test() const
{
int *b = (int*)(int)(&m_num);
*b++;
} |
|
c***d 发帖数: 996 | 20 ☆─────────────────────────────────────☆
blueivan (bl.ue) 于 (Mon Nov 6 16:48:51 2006) 提到:
编译器到底是用值还是用变量???
☆─────────────────────────────────────☆
NeverLearn (Nice people finish last) 于 (Mon Nov 6 17:02:23 2006) 提到:
It has to be variable-oriented since you can type cast away the
constness of a variable, so you can NOT assume it never changes.
☆─────────────────────────────────────☆
Pontiff (需要一点运气--to candidacy) 于 (Mon Nov 6 17:51:11 2006) 提到:
variable. just as static storage s |
|
d*********1 发帖数: 25 | 21 In the 4th version of C++ primer, regarding smart pointer, it invokes the
HasPtr as const reference, how could it changes the value of rhs?
++rhs.ptr->use
and --ptr->use |
|
d*********1 发帖数: 25 | 22 I have done a test that
rhs.ptr can't be changed , eg rhs.ptr=NULL //illegal
so, in this case, the pointers in the class of HasPtr are defined as:
datatype * const pointer;
so the value of pointer can be changed.
it is a bit of tricky. |
|
E*****7 发帖数: 128 | 23 WRONG: const data member of a class can be static!
(Comment: there are many misleading answers given by other persons in your original post.They are really misleading!) |
|
|
d****n 发帖数: 130 | 25 对,我的语言不严谨。那么这函数返回指向const int的指针的意义是什么?
int |
|
|
S**I 发帖数: 15689 | 27 vector a(10);
for(...)
a[i] = ...;
const vector b(a); |
|
t****t 发帖数: 6806 | 28 他要全局的吧, 不能这么干的
in fact, before c++0x is out, there is almost not point declaring a
static const vector<...> xxx
since there is almost no way to initialize it.
如果很大的话, 还不如自动生成一个.h然后include 进来呢 |
|
n****g 发帖数: 150 | 29 定义普通数组;return 一个 const ref 不久行了。 |
|
|
J*******g 发帖数: 381 | 31 我还是没写对。 请问一下,指向const的 pointer能够直接赋一个常数初始化吗? |
|
S**I 发帖数: 15689 | 32 const double *cptr = new double(3.14); |
|
t****t 发帖数: 6806 | 33 const double xxx = 3.14; |
|
l*******r 发帖数: 511 | 34 是哪个的长度啊?可以这样写的吗?
monthtable == 指向长度为3的const char array的pointer? |
|
X****r 发帖数: 3557 | 35 []比*优先级更高,所以要这么看:
monthTable[3] => monthTable是一个长度为三的数组
*monthTable[3] => 这个数组的每个元素都是一个指针
const char *monthTable[3] => 这些指针指向不可变的字符
所以monthTable就是一个长度为三的不可变字符指针数组 |
|
l*******r 发帖数: 511 | 36 所以我可以
monthtable t1={"January","Feb","Mar"};?
但是一般不是typedef A B;然后B就相当于A的type吗?
[]比*优先级更高,所以要这么看:
monthTable[3] => monthTable是一个长度为三的数组
*monthTable[3] => 这个数组的每个元素都是一个指针
const char *monthTable[3] => 这些指针指向不可变的字符
所以monthTable就是一个长度为三的不可变字符指针数组 |
|
s***t 发帖数: 70 | 37 const int n=10;
int a[n];
the above is not part of ANSI C, but is it part of C standard 99? |
|
a********r 发帖数: 58 | 38 Why default linkage is static for const global variable in C++, but extern
in C? Thanks! |
|
t****t 发帖数: 6806 | 39 yes, the object will be implicitly "converted" to const. |
|
S**I 发帖数: 15689 | 40 const_cast does not allow you to change value of a constant; cast away
constness of a constant value and try to change it is undefined behavior,
which you should never do. |
|
g*********s 发帖数: 1782 | 41 is the output undefined? how to initialize this array?
const double* pc = new double[10];
cout << (*pc) << endl; |
|
|
G****A 发帖数: 4160 | 43 Suppose the default value is 0.
there are two ways (as far as I know) to handle that:
1. const double* pc = new double [SIZE]();
2. std::vector vec(SIZE, 0); --if a vector is working for your
case. |
|
r*******y 发帖数: 1081 | 44 //1.cpp
extern const int i = 1;
//2.cpp
#include
using namespace std;
extern int i;
int main(){
i = 2;
cout <
}
Nothing wrong to compile but segmentation fault when running |
|
S*******s 发帖数: 13043 | 45 why is it designed like that? what advantage compared to returning const? |
|
S*******s 发帖数: 13043 | 46 no need to use arrogant tone. anyway, can you give an example that const
reference return type will fail? do you understand my last question in the OP? |
|
t****t 发帖数: 6806 | 47 yes you can declare like this. then (a=b)=c will fail to compile. but some p
pl think overloaded operators should mimic built-in type, which includes (a=
b)=c. in fact, as a convention, all STL class returns (non-const) reference
to *this in assignment operator. scott meyers mentioned this in
++>. |
|
t****t 发帖数: 6806 | 48 you just need to provide a ctor for const object, that's the rule.
since |
|
r****t 发帖数: 10904 | 49 point here is to allow copy construct from a const object |
|
S*******s 发帖数: 13043 | 50 const map& a=GetMap("XXX");
是说不能对a进行任何写操作,还是说不能把a指向另一个map?就是说以下哪个会出错?
a.insert(pair("aaa","bbbb");
a=GetMap("YYY"); |
|