t****t 发帖数: 6806 | 1 since non-const -> const conversion is almost always implicitly there, you
can say the object will be const when you call it. but of course, it doens't
have to be already const. |
|
X****r 发帖数: 3557 | 2 Just don't do it.
7.1.5.1
4. Except that any class member declared mutable can be modified,
any attempt to modify a const object during its lifetime results
in undefined behavior.
If you're still curious, under the hood compiler just assumed
the cost variable doesn't get changed at all and directly use
value 0 instead of reading from it. You can add keyword
'volatile' to force the read, e.g.
const volatile int x = 0;
const int *py = const_cast (&x);
(rest of code unch... 阅读全帖 |
|
i**********e 发帖数: 1145 | 3 http://www.parashift.com/c++-faq-lite/const-correctness.html#fa
Fred const* p means "p points to a constant Fred": the Fred object can't be
changed via p.
In your case, you are still trying to change the data p is pointing to, even
though you use p+1. What is the point of const-correctness if you can
increment the pointer and changing the data that is declared as const? |
|
S*******s 发帖数: 13043 | 4 a typical copy assignment operator declaration is like following:
A & operator= (const A& other);
why its return type is non-const, instead of like following:
const A & operator= (const A& other);
will there be situantion that the operator be the L value, like
(a = b) = c ? |
|
b***i 发帖数: 3043 | 5 const A& rhs 等价于 A const & rhs 就是rhs 是不改变A值的引用
我觉得,你说得有道理。第二点,就是thrust提到的,temporary 能bind 到const
reference,而不能非const。他不应该说你错,而应该是不完全。新标准可能对此有变
动,我得研究一下。 |
|
h**k 发帖数: 3368 | 6 Const 离那个部分更近,就是那个部分是const
Const int *
Int * const |
|
k**********g 发帖数: 989 | 7
To give a simple "mental" approach: (not necessarily correct)
(In both thought exercise below, ignore for a moment whether they actually
exists in STL.)
For the first line of code, is it permissible to define this?
std::pair<...> map::insert(...) const;
(Namely an insert method that's marked const - what does it do?)
For the second line of code, is it permissible to define this?
map& map::operator= (map&
other) const;
(Namely an assig... 阅读全帖 |
|
m*m 发帖数: 47 | 8 grades 是个const pointer 所指向的是个const char*. 从右往左读就好理解了。 |
|
l*********s 发帖数: 5409 | 9 the standard requires custom ctor for const class, probably thee compiler
has a bug to let you go through with vCB
quote/
8.5]
9. If no initializer is specified for an object, and the object is of
(possibly cv-qualified) non-POD class type (or array thereof), the object
shall be default-initialized; if the object is of const-qualified type,
the underlying class type shall have a user-declared default constructor.
Otherwise, if no initializer is specified for an object, the object and
its subobje... 阅读全帖 |
|
m*********a 发帖数: 3299 | 10 int i=1;
const int *const ptr=&i;
i=10;//*ptr=10
ptr指的不是const,还是不允许*ptr=10这样赋值
const不是常量的意思,而是不能赋值的意思
如果你有其他变量来改变这个值的话,没有问题
这和大家要的常量相差太大了
真个有点儿搞 |
|
h**l 发帖数: 168 | 11 不用const对基本类型(比如 int)是可以,对user defined type, 如果你想避免这种奇
怪的assignment, 你需要return type加const. 当然如果你要支持move semantics,就
不要这样做。
我不是说return type是const Obj&这种情况, 是 Obj 或者 const Obj |
|
r*e 发帖数: 112 | 12 I am try to use one function.
int execvp (const char *file, char *const arv[]);
but , I am confused by 'const char *' and 'char *const'
what is the differnce between them?
Tx! |
|
b*******y 发帖数: 239 | 13 原题也是在这个版上有人发的C++的其中一题,已copy下来:
class Person{
public:
Person(const char* szName);
const char* GetName() const;
/*put a function here*/
private:
char *m_szName;
};
int main()
{
Person person("John");
std::cout << Person;
return 0;
}
Referring to the sample code above, which one of the following member
functions do you add at the comment to support std::cout << person
statement?
A. std::string operator() { return GetName(); }
B. std::string ToString() { retur |
|
s*********t 发帖数: 1663 | 14 const表示该函数承诺不会修改该object
const引用不能修改object,所以不能传进非const的函数里 |
|
E*******0 发帖数: 465 | 15 讨论个iterator, reverse_iterator, const iterator and const reverse_iterator
之间转换。 |
|
s****A 发帖数: 80 | 16 谢谢,其实我主要是不理解书上的"non const variables are extern by default."这
句话,要是如此的话,non const variable前面加不加extern不都一样吗?总是相当于
有extern |
|
a**********3 发帖数: 64 | 17 傻傻分不清。比如 function(const Class & c); 这个const什么情况下要定义成const
? 还有就是pass by reference这个,一般是不是function都最好写成pass by
reference的用&? 以前用java,改成c++还是比较糊涂,求教。 |
|
a**********3 发帖数: 64 | 18 多谢大牛。还有个问题就是关于你说的这两条:
1.如果加了const,那么在定义Class的时候怎么写?怎么体现出根据这个Class生成的c
是个const?
比如我写constructor
void Class::Class(); 怎么保证生成的c是个const?
2.同样的问题,在定义Class的时候,怎么体现出c是个reference? |
|
m***t 发帖数: 254 | 19 In BJ's book, Sec 10.2.7.2, there are following codes:
struct cache{
bool valid;
string rep;
};
class Date {
cache& c;
void compute_cache_value();
//...
public:
//...
string string_rep() const;
};
string Date::string_rep() const {
if (!c.valid) {
compute_cache_value();
c.valid=true;
}
return c.rep;
}
My question is, if we have:
const Date d4;
string s4=d4.string_rep();
will this be undefined behavior? Thanks. |
|
t****n 发帖数: 15 | 20 const char ** means a pointer to a pointer to a const char.
The loophole is the const char is modified if the conversion is allowed. |
|
t****t 发帖数: 6806 | 21 你吹牛
除非myClass是个macro,
否则任何情况下, const myClass和myClass const都是一样的 |
|
c******a 发帖数: 18 | 22 const int foo() 返回一个const int
int foo() const 作为类成员函数,不改变类的成员变量 |
|
v****c 发帖数: 32 | 23 " const int * " 和 “ int * const " 的区别是什么?
谢谢! |
|
b***y 发帖数: 2799 | 24 ☆─────────────────────────────────────☆
qxc (睡觉自然醒, 数钱手抽筋) 于 (Tue Aug 23 18:18:35 2005) 提到:
compiler 老抱怨说我不能把一个 const 的 string 设置成为 static member,
VS2005.
这里面有什么逻辑原因吗? 多谢。
☆─────────────────────────────────────☆
aZhu (a+zhu) 于 (Tue Aug 23 18:23:43 2005) 提到:
I guess you initialize it inside the class.
i think you can't
though
class A{
static const int = 7;
// ..
}
is legal,
class A{
static const string a = "aaa";
//...
}
is not ba?
☆─────────────────────────────────────☆
yunhai (飞纵 |
|
d****n 发帖数: 130 | 25 多谢了,很有用。
还有一个问题,从一个函数返回一const指针的意义是什么?
比如const int *fun(void);
为什么要const呢?返回的内容为什么不应改变? |
|
E*****7 发帖数: 128 | 26
"INTERVIEW的时候最好不要这么写。" 为什么不?
"为什么用const? " 用const是为了(1)对CLASS而言,保护OBJECT的DATA MEMBER的状
态不能被随意改动,要改动必须使用const函数。(2)对变量而言,一旦付值,不能再改动。
“仅仅因为在函数中是 |
|
f*****Q 发帖数: 1912 | 27 俺是土人,没看出来有什么实际意义。也许传回一个数组又不让人家改?
例如这个
#include
int intArray[] = {1, 2, 3};
const int * fun(){
return intArray;
}
int main (int argc, char * const argv[]) {
const int * a = fun();
//a[ 1 ]=0; !!error!!
std::cout<
return 0;
} |
|
c*******3 发帖数: 21 | 28
The first answer is why not. Because if you assign the return value to some
other variable a=f(); it does not
hurt if f() return a const (just like you can use a=5).
A further point is that returning const can avoid things like: f() = 1.
Although one may never want to do
this intentionally, it may happen if one has a typo in if (f() == 1).
Returning a const can let the compiler
find that typo. |
|
b***y 发帖数: 2799 | 29 ☆─────────────────────────────────────☆
oOOo (\_/o!o\_/) 于 (Wed Sep 7 01:50:59 2005) 提到:
they are the same
1;"?
☆─────────────────────────────────────☆
cdr (可读可写) 于 (Wed Sep 7 02:18:37 2005) 提到:
1;"?
The second one has a symbal "A" in the symbal table.
☆─────────────────────────────────────☆
thrust (Thrust Jaeina) 于 (Wed Sep 7 13:16:53 2005) 提到:
const char* and char const* are the same. (if you use gdb, gdb always use the
later form.)
#define A 1 and const int A=1 are NOT the |
|
z****e 发帖数: 2024 | 30 返回值:
1.return by value
2.return by reference
3.return by const reference
我不知道理解的对不对,
2,比1高效对吧?
3,就是为了不改2对吧?
那么这个帖子里的程序,是3.而你给的方法是1.
1,比3,效率低对吧?
那为什么不用3? |
|
t****t 发帖数: 6806 | 31 you may declare const T& or const T*. for const T&, you must initialize it
in the initializer list for all constructors. |
|
b*******s 发帖数: 5216 | 32 const_cast按照我的理解
只是用来去掉指针带的const属性的
比如const int * p 里面的const
你的用法属于误用 |
|
r*******y 发帖数: 1081 | 33 for example
char a[] = "hello";
const char * p = a;
p[1] = '3'; //is this ok?
I know it is not ok for this: *p = '3'
since p point to a const char.
But it is not saying p+1 is pointing to a const char.
so p[1] ='3' would have been ok.
But I find that it is not ok to do this: p[1] = '3' after I compile it
Any comments? seems a stupid question. thanks |
|
X****r 发帖数: 3557 | 34 type of p is const char *, so type of p+1 is also const char *,
thus type of p[1], which is *(p+1), is const char, and you can't
assign to it. |
|
P********e 发帖数: 2610 | 35 const implies local linkage
extern means external linkage
combining them together won't work.
Solution:
define: const int i = 1; in every compilation unit, and linker will merge
the duplicates.
It has the same effect as extern const. |
|
a***y 发帖数: 2803 | 36 你知道const的意思是什么吗?const就是说这个地址的内容不能更改.
注意,返回的不是一个值,而是一个reference,*this.*this意味着当前object.因为当前
object的值可以任意更改,所以返回值是A&,而不是const A&. |
|
j*******e 发帖数: 674 | 37 C++ const is more of a "logical constant", but not "bit by bit constant".
you can certainly return a const reference for the assignment opreator, no
matter it is "*this" or "const *this". |
|
r*******y 发帖数: 1081 | 38 //1.cpp
class A{ };
int main(){
const A a;
}
this will give compile error:uninitialized const ‘a’
//2.cpp
class A{
public:
A(){}
}
int main(){
const A a;
}
It is ok now. What is the matter to define a default constructor here?
The compiler will give us one default constructor if we don't define
any constructor.
Thanks. |
|
f*******n 发帖数: 12623 | 39 He is wrong.
References are always constant. You cannot make a reference point to
something else after it is defined.
Even for pointers, the syntax "const A *rhs" or "A const *rhs" means pointer
to constant object. Only "A *const rhs" means "constant pointer". |
|
|
m****s 发帖数: 1481 | 41 顺便再问下,按照从右往左读,这里这个static是指const char* 是静态的?也就是那
些字符串只初始化一次就不再变了。但是如果这一句是在一个function里,每次call这
个function,letters array里面存的地址还是重新从那些字符串那里初始化一次? |
|
n*****t 发帖数: 22014 | 42 C 是把这些全放在 data segment 里的。array 存指针,编译的时候就初始化了,运行
时候不会再动了。static const 你就理解成 define 常量好了。
C++ 应该差不多吧?俺不懂胡说的 |
|
m*********a 发帖数: 3299 | 43 const char i='x';
const char *cp=&i;
char *p;
p=const_cast(cp);
std::cout<<&i<<" "<
address
*p='y';
std::cout<<*p<
std::cout<<*cp<
std::cout<
同样的
char i='x';
const char *cp=&i;
char *p;
p=const_cast(cp);
std::cout<<&i<<" "<阅读全帖 |
|
D***n 发帖数: 6804 | 44 你可以看看这个打出来什么:
const char i='x';
const char *cp=&i;
char *p;
p=const_cast(cp);
*p='y';
std::cout<<*p<
std::cout<<*cp<
std::cout<
const char *t = &i;
std::cout<<*t<
我这里的结果是 y y x y |
|
b***i 发帖数: 3043 | 45 不用const也行吧?
比如
int foo()
{
return 5;
}
也不能foo()=6啊
我说过我理解const Obj& foo(),不理解const int foo() |
|
f****4 发帖数: 1359 | 46 conversion Operator :)
// code explains itself
class Test{
public:
operator const char*(){cout<<"Test"<
};
void f(const char*)
{
cout<<"f()"<
}
int main(){
Test t;
f(t);
return 0;
} |
|
o***e 发帖数: 497 | 47 #include
int main()
{
int i=37;
int &ref = i;
const int * const cRef = &ref;
i++;
printf("%d\n", *cRef);
}
这个结果是多少?为什么?
谢谢! |
|
w***h 发帖数: 415 | 48 Sure not right. *a is const, a is not const.
Why don't you code, compile and run. Just do it first, and think secondly.
My simple advice. |
|
|
m*********2 发帖数: 701 | 50 really?
1) non-const will automatically convert to const when passing?
2) using dynamic_cast<> instead of const_cast<>?
change
reference
the |
|