0

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.

Troubadour
  • 13,334
  • 2
  • 38
  • 57
the_law
  • 215
  • 1
  • 5
  • 13
  • 3
    You class definition is a very basic one so it is clear you are at a very early stage in learning C++. I would suggest getting a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and working through that first. – Troubadour Mar 09 '11 at 22:24

2 Answers2

1

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).

const char * pReason;

In C++, that's how you tell the compiler that your class will have a member with that type and that name.

  • 4
    I would revert your edit. The `const` does not apply to the member here. Instead it means that you can't change the characters. You can still change the address stored in `pReason` as many times as you like. – Troubadour Mar 09 '11 at 22:05
  • 2
    A string literal isn't "a pointer to an array of characters." It is an array of characters and like all arrays, it is implicitly converted to a pointer to its initial element in most circumstances. – James McNellis Mar 09 '11 at 22:11
  • @Troubadour, @ed-j-plunkett a constant member would be `char * const pReason;` or maybe `const char * const pReason;` if you want to in fact initialize it with a string literal. Sorry, just now saw your comment below. – Tilman Vogel Mar 09 '11 at 22:33
  • @Troub - thx, I'm rustier on C++ than I thought. *** James McNellis -- When you "pass" a string literal to a constructor, or assign it to a pointer, what is passed or assigned is the address of the first character in the array. You're right, but IMHO somebody who's in the very first stages of learning the language is much better off thinking of string literals as pointers than trying to get his head around the distinction at this stage. – 15ee8f99-57ff-4f92-890c-b56153 Mar 10 '11 at 03:04
  • 1
    Thanks people. I'm glad that this stuff really IS confusing. – the_law Mar 10 '11 at 14:03
  • @the_law -- C++ (and C) type declaration syntax is one of the least user-friendly parts of the language, and that's really saying something! – 15ee8f99-57ff-4f92-890c-b56153 Mar 10 '11 at 14:16
0

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.

julx
  • 8,694
  • 6
  • 47
  • 86
  • 1
    -1: That's incorrect. You can assign freely to `pReason`. You are thinking of the case where it is declared `char*const pReason`. – Troubadour Mar 09 '11 at 22:01