const不适用于指针? [英] const does not apply to pointers?

查看:63
本文介绍了const不适用于指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当访问指针成员变量时,即使实例是const,也不会将它们视为const

。这对我来说是一个完全的惊喜,我会很感激为什么会有这样的反馈。

这里有一个片段:


class MyClass {

public:

char * pChar;

};

void ChangePointedTo( char * ch)

{

* ch = 0;

}

void DoStuff(const MyClass& constClass)

{

ChangePointedTo(constClass.pChar); //< - 为什么这样做?

}


我本以为编译错误告诉我

" constClass.pChar"是const char *类型但是相反它被传递给了函数而没有任何问题,突然MyClass不再如此

const了......

When accessing pointer member variables these are not treated as const
even though the instance is const. This came as a complete surprise to
me and I would appreciate some feedback why that is the case.
Here''s a snippet:

class MyClass {
public:
char *pChar;
};
void ChangePointedTo (char *ch)
{
*ch = 0;
}
void DoStuff (const MyClass &constClass)
{
ChangePointedTo ( constClass.pChar ); // <-- why does this work?
}

I would have expected a compiler error telling me that
"constClass.pChar" is of type "const char *" but instead it gets passed
to the function without any problems and suddenly MyClass is not so
const anymore...

推荐答案

gr******@gmail.com 写道:
gr******@gmail.com wrote:

访问指针成员变量时,即使实例为const,也不会将它们视为const

。这对我来说是一个完全的惊喜,我会很感激为什么会有这样的反馈。

这里有一个片段:


class MyClass {

public:

char * pChar;

};

void ChangePointedTo( char * ch)

{

* ch = 0;

}

void DoStuff(const MyClass& constClass)

{

ChangePointedTo(constClass.pChar); //< - 为什么这样做?

}


我本以为编译错误告诉我

" constClass.pChar"是const char *类型但相反它被传递给函数没有任何问题,突然MyClass不是那么

const了...
When accessing pointer member variables these are not treated as const
even though the instance is const. This came as a complete surprise to
me and I would appreciate some feedback why that is the case.
Here''s a snippet:

class MyClass {
public:
char *pChar;
};
void ChangePointedTo (char *ch)
{
*ch = 0;
}
void DoStuff (const MyClass &constClass)
{
ChangePointedTo ( constClass.pChar ); // <-- why does this work?
}

I would have expected a compiler error telling me that
"constClass.pChar" is of type "const char *" but instead it gets passed
to the function without any problems and suddenly MyClass is not so
const anymore...



你混淆了一个const char *用char * const。请查看常见问题解答:

http://www.parashift.com/c++-faq-lit....html#faq-18.5


祝你好运,


Tom

You have confused a "const char*" with "char* const". Please take a
look at the FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-18.5

Best regards,

Tom


文章< 11 **** *****************@75g2000cwc.googlegroups.c om>,
gr******@gmail.com 写道:
In article <11*********************@75g2000cwc.googlegroups.c om>,
gr******@gmail.com wrote:

访问指针成员变量时,这些变量不会被视为const

即使实例是const。这对我来说是一个完全的惊喜,我会很感激为什么会有这样的反馈。

这里有一个片段:


class MyClass {

public:

char * pChar;

};

void ChangePointedTo( char * ch)

{

* ch = 0;

}

void DoStuff(const MyClass& constClass)

{

ChangePointedTo(constClass.pChar); //< - 为什么这样做?

}


我本以为编译错误告诉我

" constClass.pChar"是const char *类型
When accessing pointer member variables these are not treated as const
even though the instance is const. This came as a complete surprise to
me and I would appreciate some feedback why that is the case.
Here''s a snippet:

class MyClass {
public:
char *pChar;
};
void ChangePointedTo (char *ch)
{
*ch = 0;
}
void DoStuff (const MyClass &constClass)
{
ChangePointedTo ( constClass.pChar ); // <-- why does this work?
}

I would have expected a compiler error telling me that
"constClass.pChar" is of type "const char *"



它不是const char *,它是char * const这当然*可以*

传递给ChangePointerTo。

It''s not "const char *", it''s "char * const" which of course *can* be
passed to ChangePointerTo.


gr ****** @ gmail.com 写道:

访问指针成员变量时,这些变量不会被视为const

即使实例是const。这对我来说是一个完全的惊喜,我会很感激为什么会有这样的反馈。

这里有一个片段:


class MyClass {

public:

char * pChar;

};

void ChangePointedTo( char * ch)

{

* ch = 0;

}

void DoStuff(const MyClass& constClass)

{

ChangePointedTo(constClass.pChar); //< - 为什么这样做?

}


我本以为编译错误告诉我

" constClass.pChar"是const char *类型但是相反它被传递给了函数而没有任何问题,突然MyClass不再如此

const了......
When accessing pointer member variables these are not treated as const
even though the instance is const. This came as a complete surprise to
me and I would appreciate some feedback why that is the case.
Here''s a snippet:

class MyClass {
public:
char *pChar;
};
void ChangePointedTo (char *ch)
{
*ch = 0;
}
void DoStuff (const MyClass &constClass)
{
ChangePointedTo ( constClass.pChar ); // <-- why does this work?
}

I would have expected a compiler error telling me that
"constClass.pChar" is of type "const char *" but instead it gets passed
to the function without any problems and suddenly MyClass is not so
const anymore...



这是C ++的缺点之一。由于编译器只能确保对象的二进制表示不会改变,所以你可能会改变指向的对象(因为你不是改变

指针的值,满足你的常量对象的'二进制''常量。)


这个逻辑只是预期的,因为它使得编译器这么多

更简单。你可以通过为你的对象提供一个合适的接口来避免这个陷阱(允许访问一个成员变量被认为是坏的大多数C ++程序员的b $ b风格):


#include< stdio.h>


class MyClass {

protected:

char * pChar; //会员变量现在受到保护。

public:

char * GetpChar(){// pChar的访问者。

返回pChar; < br $>
}

const char * GetpChar()const {// pChar的另一个访问者

返回pChar; //为const对象调用。

}

};


void ChangePointedTo(char * ch)

{

* ch = 0;

}


void UsePointedTo(const char * ch)

{

printf(ch);

}


void DoStuff(const MyClass& constClass)

{

ChangePointedTo(constClass.GetpChar()); //这不再有用了

}


无效DoStuff2(MyClass& non_constClass)

{

ChangePointedTo(non_constClass.GetpChar()); //这个有用

}


无效DoStuff3(const MyClass& constClass)

{

UsePointedTo(constClass.GetpChar()); //这也有效

}


问候,

Stuart

This is one of the drawbacks of C++. Since the compiler can only ensure
that the binary representation of the object doesn''t change, you may
change objects that are pointed to (since you don''t change values of the
pointer, the ''binary'' constness of your constant object is satisfied).

This logic is only expected, since it makes the compiler so much
simpler. You can avoid this pitfall by providing a proper interface to
your object (permitting access to a member variable is considered bad
style by the majority of C++ programmers):

#include <stdio.h>

class MyClass {
protected:
char *pChar; // Member variable is now protected.
public:
char *GetpChar () { // Accessor for pChar.
return pChar;
}
const char* GetpChar () const { // Another accessor for pChar
return pChar; // that gets invoked for const objects.
}
};

void ChangePointedTo (char *ch)
{
*ch = 0;
}

void UsePointedTo (const char* ch)
{
printf (ch);
}

void DoStuff (const MyClass& constClass)
{
ChangePointedTo ( constClass.GetpChar ()); // this doesn''t work anymore
}

void DoStuff2 (MyClass& non_constClass)
{
ChangePointedTo ( non_constClass.GetpChar ()); // this works
}

void DoStuff3 (const MyClass& constClass)
{
UsePointedTo ( constClass.GetpChar ()); // this works also
}

Regards,
Stuart


这篇关于const不适用于指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆