帮助我理解这个简单的c++类定义、构造函数和初始化
这是类定义(用于异常处理):
class MyException {
public:
MyException(const char * pTxt) : pReason(pTxt){};
const char * pReason;
};
后来使用如下:
throw MyException("file too short");
throw之后,是否创建并初始化了一个新对象?不管怎样,我不明白类定义如何允许它用文本字符串初始化。它需要一个指向文本字符串的指针,不是吗?然后将 pReason 设置为该指针,对吗?这与 const char * pReason 行有何关系?我很困惑,有人至少可以向我解释一下类定义吗?我可能只是忽略了一些显而易见的事情。顺便说一句,我从“游戏程序员的 C++”第 90 页复制了上述代码。
here is the class definition (it's for exception handling):
class MyException {
public:
MyException(const char * pTxt) : pReason(pTxt){};
const char * pReason;
};
And later it is used as follows:
throw MyException("file too short");
after throw, is a new object created and initialized? Whatever the case, I don't understand how the class definition allows it to be initialized with a text string. It takes a pointer to a text string, doesn't it? And then sets pReason to that pointer, right? And how does this involve the line const char * pReason? I am confused, can anyone at least explain the class definition to me? I might be just looking past something obvious. I have copied the above code from "c++ for game programmers" page 90, btw.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
C++ 中的文字字符串是一个指向字符串的指针……实际上,是这样的。更迂腐一点,它是一个字符数组,后跟一个整数值零的字符。一个字符是一个整数。呃...在 C++ 中,字符串文字不是 STL 字符串类 std::string 的实例。至少可以说,这在面向对象语言中是相当不寻常的。它是 C++ 狂野而鲁莽的青年人的产物。
如果将字符串文字分配给任何内容(除非将其用作数组的初始值设定项),或将其传递给函数,则分配或传递的是数组中第一个字符的地址 - 指向字符串的指针。这就是您在调用构造函数时看到的内容:传递给构造函数的是字符串文字中第一个字符的地址,它存储在...编译器认为的任何位置属于.那不关我们的事。
这一行将 pReason 声明为该类的成员变量。 const 部分意味着您不能更改它指向的字符串(除非您不遗余力地这样做,但您确实不应该这样做)。
在 C++ 中,这就是告诉编译器您的类将有一个具有该类型和该名称的成员的方式。
A literal string in C++ IS a pointer to a string... in effect, sort of. A bit more pedantically, it's an array of characters, followed by a character of integer value zero. A character is an integer. Erm... In C++, a string literal is not an instance of the STL string class, std::string. This is fairly unusual among object-oriented languages, to say the least. It's an artifact of C++'s wild and reckless youth.
If you assign the string literal to anything (unless you use it as an initializer for an array), or pass it to a function, what gets assigned or passed is the address of the first character in the array -- a pointer to the string. And that's what you're seeing in the call to your constructor: What's being passed to the constructor is the address of the first character in the string literal, which is stored... wherever the compiler thinks it belongs. None of our business where that is.
This line declares pReason as a member variable of the class. The const part means you can't alter the string it points to (unless you go out of your way to do it, but you really shouldn't).
In C++, that's how you tell the compiler that your class will have a member with that type and that name.
const char* pReason 是字段的声明。构造函数包含初始化器 pReason(pTxt)。
编辑:我根据评论中的评论编辑这篇文章。
const char* pReason is a declaration of a field. The constructor contains the initializer pReason(pTxt).
EDIT: I edit this post following the remark in the comment.