c**********e 发帖数: 2007 | 1 如何对const data member做assignment?
class A{
const int a;
public:
A():a(0){};
A(int m_a):a(m_a){};
};
int main(){
A a(1);
A b;
b = a; //how to implement assignment for this?
} |
|
S**I 发帖数: 15689 | 2 no, casting away constness of a const variable is undefined behavior. |
|
s****A 发帖数: 80 | 3 看书上说nonconst variables are extern by default, but to make const variable
accessible to other files we must explicitly specify that it is extern.
这么说的话如果我有两个文件:
//file1.cpp
int a=10;
extern int b=11;
void func(){
extern int c=12;
}
//file2.cpp
#include
int main(){
extern int a,b,c;
std::cout<
}
输出会是什么?谢谢! |
|
d**********x 发帖数: 4083 | 4 1. 如果function不会改变c的值,则一定加const...如果要改变c的值,则function
name一定要很明确表示这个改变语义
2. 传ref和传指针其实是一回事,没有拷贝的消耗,除了built-in类型,一般都传引用
或者指针比较好,除非你明确需要一个副本。
const |
|
O*******d 发帖数: 20343 | 5 把函数变量分成两类,一类是in,另一类是out。 in的最好用const,对于object,最
好用ref。 out的最好用pointer。 这样在读函数时,很容易看出来那些是in,那些是
out。 一般把in放在前边,out放在后边。最好不要混放。
const |
|
m***t 发帖数: 254 | 6 That is absolutely correct. However, that was not the point of the question.
Unfortunately Stroustrup did
not give a ctor for this code snippet, but I assume the reference has been
safely initialized. The main
question i am having is if c is a regular const reference, then c.valid=true
in string_rep will be a violation
of constness, thus resulting in undefined behavior, right? |
|
m***t 发帖数: 254 | 7 So you think c.valid=true is ok? it will not violate the constness of string
_rep() or the constness of s4? The
code is from B. Stroustrup's C++ programming language, 3rd edition, page 233
. |
|
lz 发帖数: 23 | 8 I define the following class A
class A
{
A(const A& a)
{
int size=a.getsize();// line4
}
public:
int getsize();
}
I get the following error in line 4
"can not convert 'this' pointer from const A to A& "
Can you guys tell me the reason?
Thanks! |
|
lz 发帖数: 23 | 9 does this mean that (const A&) instance can not call its const function? |
|
t****t 发帖数: 6806 | 10 this means a const object can't be used to call non-const member function.
doh! |
|
t*****g 发帖数: 1275 | 11 It's a good practice to put const behind the variable it applies to.
In you case, if myClass is a simple class or basic datatype there is no
difference. However, if myClass is a datetype defined as a pointer, or, a
template which could potentially become a pointer, you will encounter
unexpected errors. |
|
c********e 发帖数: 383 | 12 这个c++ template, complete guide 的前言里面有讲到过,确实有一些不同。
template在这里就像是个macro一样。
作者强力推荐const方后面,我看过后忘记了。感觉对于一般人没什么大用 |
|
j****g 发帖数: 597 | 13 An interview question.
First he asked what is a const function. I said it's a function that doesn't
change a variable's value.
Then he asked what if you want to change that value in the const function? I
said use mutable. He told me there's other way.
Any idea what is the other way? ? |
|
r*********r 发帖数: 3195 | 14 比如 const char *what() { return "bad"; } . 这里必须返回 const char *. |
|
E*****7 发帖数: 128 | 15 const int *fun(void)
{
const int x, y, z, volume ;
x=1; y = 2, z=3 ;
volume = x * y * z ;
return &volume ;
}
I do not know if this makes sense or not. Here we do not want the volume
calculated from fun() function to be modified outside of fun() after it is
returned. |
|
d****n 发帖数: 130 | 16 这个理论上没大问题了,但INTERVIEW的时候最好不要这么写。
现在大家能不能回来回答一下我原来的问题,为什么用const? 仅仅因为在函数中是
const int吗?好象还有其它原因。 |
|
E*****7 发帖数: 128 | 17
LZ问const int *fun(void),你却建议const int fun(void)。答非所问了吧? |
|
i*****l 发帖数: 50 | 18 要把一个常数矩阵存在一个class里面
我知道可以把这个常数矩阵声明成static的
比如
class A
{
static const double Array[10][10];
}
const double Array={{.....},{....},{....}....}
但是因为矩阵太大乐,懒的一个个的输入,想用for循环(因为很多数剧是一样的)
应改怎么写呢?
3x, 用array和vector都无所谓 |
|
J*******g 发帖数: 381 | 19 const double *cptr =3.14159;
这句话为什么有问题? 等号右边的不是一个const literal吗? |
|
J*******g 发帖数: 381 | 20 Sorry,写错了。 应该是这样。
const double pi = 3.14;
const double *cptr = π
*cptr =3.14159;
最后一句出错了。 |
|
X****r 发帖数: 3557 | 21 typedef是这样的:
你要typedef某一个类型,就写出如果要声明该类型的变量的
语句,“变量名”处放入你想要的新的类型名,然后前面加上
typedef
比如const chat *monthTable[3]声明了一个
名叫monthTable的一个长度为三的不可变字符指针数组,
那typedef const chat *monthTable[3]就
定义了一个名叫monthTable的类型为长度为三的不可变
字符指针数组 |
|
z***9 发帖数: 696 | 22 my understanding is that function name() const should not modify any data
member in the class, however, it does not mean an object of this class has
to be const in order to call name(). |
|
d****p 发帖数: 685 | 23 name =
void name(const Person* person)
So when you call name() via aperson.name(), what happened is
name(&aperson) where &aperson is converted to const Person*
The conversion is OK since it is from less cv-qualified to more cv-qualified
. The other way around is not allowed unless you use const_cast |
|
c*******9 发帖数: 6411 | 24 void name(const Person* person)
So when you call name() via aperson.name(), what happened is
name(&aperson)
could you explain where the following come from? it is not in the signature
, it the following generated by the compiler?
void name(const Person* person)
thanks... |
|
z****e 发帖数: 2024 | 25 我这个例子是验证conversion用的。
如果一定要说const& 好不好, 是POD就没事。
const), |
|
mw 发帖数: 525 | 26 what i want to build is:
class My{
public:
My(const T& t_instance){
local_t <== t_instance
}
void doSomething()
{
local_T->doIt();
}
private:
member local_T;
// need to have a reference/pointer/whatever that can access the T instance
within the class
};
怎么样才能让一个成员变量保存一个const reference呢?
有没有比较经典的解决办法?
谢谢 |
|
O*******d 发帖数: 20343 | 27 写一个public的 const reference. 指到一个private member.
例如
class Foo
{
public:
Foo() : a(b) {}
const int & a;
private:
int b;
} |
|
c**y 发帖数: 172 | 28 Anyone can explain why the second output is 0, rather than 1.
========================================
#include
#include
#include
using namespace std;
int main() {
const int x = 0;
int const *py = &x;
int *px = const_cast(&x);
cout << *px << endl; // output 0
*px = 1;
cout << x << endl; // output 0, I don't understand
cout << *px << endl; // output 1
cout << *py << endl; ... 阅读全帖 |
|
r*******y 发帖数: 1081 | 29 that is my question: type of p is const char *, why type of p+1 is
also const char * ?
thanks |
|
S*******s 发帖数: 13043 | 30 I think the intuitive expected result after ((a=b)=c ) is that all three
variables shares same value.if we define the return type const, we can
prevent such usage.
but current non-const declaration failed to do so. Thus I called it bad
design.
=(
for
no |
|
a***y 发帖数: 2803 | 31 那你是说这个是对的?
const A & operator= (const A& other); |
|
t****t 发帖数: 6806 | 32 no no no, usually if g++ and VC has different result, VC is wrong.
according to standard 8.5 clause 9,
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 a nonstatic object, the object
and its subobjects, if any, ... 阅读全帖 |
|
T******r 发帖数: 257 | 33 more generally, to allow const object to bind.
for your class A,
const A a4;
A a5(a4); // wrong |
|
S*A 发帖数: 7142 | 34 你举的例子很好啊。
所以理解const的规则不是左边右边,
是当前parser作用的type node。
看看 C 的spec好了。C 其实比C++
简单实用多了。
const 是 modifier,只有几个modifier。
等你看看type attribute。例如 pure。
那些是C 比较复杂的。
Java 干不了C擅长干的事情。
出的 |
|
D***n 发帖数: 6804 | 35 这里已经触及到了C++一个很恶心的东西,就是非标实现。
非标实现说白了就是标准在这里没有明确的定义,但是实际上是非实现不可,而且还特
别重要。比如用指针强行修改const variable就是一个例子。
其实这个从编译器角度很容易理解:编译器会在编译期间有关const的东西尽量用代码
段的常量替换。但是它并没有运行程序,不知道指针会真正指向哪块内存。所以出现类
似 &i的内容,它还是会分配一个临时变量。这个临时变量如果被改了如何处理标准没
有定义,我刚才给的例子是GCC的G++出来的结果。
你这里可以看到C++的那些“防止程序员出错”的措施的巨大漏洞。这样的漏洞还有很
多,造成的伤害比带来的好处大多了。
对于这个问题,其实还是C的方法好。 |
|
b*******y 发帖数: 239 | 36 operator const char*()
一直不知道这个怎么理解,有人说说这个的特殊含义吗?
我知道一般的operator overload,但这个似乎连operator是什么都没有,所以很不理
解。
这个是C++的,请指点,非常感谢。 |
|
f*******5 发帖数: 52 | 37 抛砖引玉,cRef是指向常量的指针常量,第一个const表明"不能通过解引用"改变指向
对象的值,把
i++成(*cRef)++就会出错。但我猜如果不通过解引用cRef,而是直接改变cRef指向变量
i是允许的。
估计编译器在内部生成一个i的副本,不能改变此副本的值?等高人解答 |
|
j***i 发帖数: 1278 | 38 所以说const ref 不能保证 别的ref 不改变值。。 这题应该是38 |
|
c**y 发帖数: 172 | 39 函数声明如下
void function1(const char *a) {
...
...
return;
}
如何能在函数function1中访问a呢?我只想到用strcpy的方法把a复制到另外一个char
array中。有没有什么办法直接访问a,而不使用额外的内存? |
|
S***n 发帖数: 31 | 40 which do you want to access to? "a" or the object "a" pointing to?
If it's "a", use reference "const char& *a" in the argument
if it's the object "a" pointing to, just access by *a, eg. *(a++).
Personal opinion :) |
|
c**********e 发帖数: 2007 | 41 extern const int MaxElements=10;
Which one of the following statements about the declaration in the sample
area above is true?
a) It defines a global constant variable that can be referenced from other
modules.
b) It references the global constant variable MaxElements declared in
another module.
c) It initializes a variable declared in another module.
d) It retrieves the global constant variable MaxElements declared in another
module.
e) It is only legal within an unnamed namespace.
(9_33 a32_48 |
|
j***i 发帖数: 1278 | 42 as far as I remember, not sure
In c++ , all built-in declaration is a definition,
const will result internal linkage, but extern turn it to
external linkage. |
|
c**********e 发帖数: 2007 | 43 This question demonstrates the use of extern and const together.
a) It defines a global constant variable that can be referenced from other
modules.
This is correct (C++ Standard 7.1.1/6). It defines a global constant variable that can be referenced from other modules.
b) It references the global constant variable MaxElements declared in
another module.
This is incorrect. The initializer makes it a definition.See other comments.
d) It retrieves the global constant variable MaxElements declared |
|
M******q 发帖数: 94 | 44 I think extern is necessary. "const" by default has internal linkage by c++
rules. "extern" gives it external linkage so that other modules can access
the var. |
|
x***y 发帖数: 633 | 45 By default, the global variable can be shared by all the translate units
except you delcare it as static...
I verified in the programme again, and extern is not necessary....In my
opinion, extern is the storage specifier, as auto, register and static; and
const is the attribute specifier, as volatile, mutable.... |
|
t****t 发帖数: 6806 | 46 const makes namespace scope names internal linkage unless extern is
specified. (3.5 clause 3)
and |
|
x***y 发帖数: 633 | 47 Sorry, I made a mistake. You are right. when using const, we must have
extern to make it have the external linkage. |
|
m*****n 发帖数: 2152 | 48 A b; b = a; 好像不是copy constructor吧,又不是 A b = a; overload operator "=
"吧.
A & A::operator =(const A &other)
{
if(this == &other)
return *this;
const_cast(a) = other.a;
return *this;
} |
|
c**********e 发帖数: 2007 | 49 C++. Here we need an assignment operator. The problem is that b.a is a const. How to assign a value to it? |
|
m*******p 发帖数: 141 | 50 Is it possible to implement the assigment operator for a class having a cons
t member data?
I guess no.
呼唤大牛来答疑。。。
const. How to assign a value to it? |
|