Thread: MSDN const_cast sample

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    MSDN const_cast sample

    Hello everyone,


    In MSDN sample for const_cast,

    http://msdn2.microsoft.com/en-us/lib...5h(VS.80).aspx

    There is a statement like this,

    --------------------
    On the line containing the const_cast, the data type of the this pointer is const CCTest *. The const_cast operator changes the data type of the this pointer to CCTest *
    --------------------

    I think in a const member function, like void printNumber() const, the type of this pointer is const pointer to current type, so we use this const_cast to remove its const properties.

    For a non-const member function, I think this pointer should not be a const pointer, right?

    So, conclusion is,

    1. in const member function, this pointer is a const pointer;
    2. in non-const member function, this pointer is a non-const pointer.

    Right?


    thanks in advance,
    George

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, conclusion is,

    1. in const member function, this pointer is a const pointer;
    2. in non-const member function, this pointer is a non-const pointer.
    More accurately:
    1. in const member function, this pointer is a pointer to const (i.e., it points to a const T);
    2. in non-const member function, this pointer is a pointer (i.e., it points to a T).

    Of course, in the MSDN example perhaps the number member variable should be declared mutable instead of resorting to a const_cast.

    EDIT:
    The this pointer is not itself const, according to the MSDN doc quoted. From the C++ Standard:
    In the body of a nonstatic (9.3) member function, the keyword this is a non-lvalue expression whose
    value is the address of the object for which the function is called. The type of this in a member function
    of a class X is X*. If the member function is declared const, the type of this is const X*, if the member
    function is declared volatile, the type of this is volatile X*, and if the member function is
    declared const volatile, the type of this is const volatile X*.
    Last edited by laserlight; 12-17-2007 at 03:00 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks laserlight,


    Why this is a non-lvalue in a non-static member function? I think this is a pointer variable and it should be addressable, it should be a lvalue?

    Quote Originally Posted by laserlight View Post
    More accurately:
    1. in const member function, this pointer is a pointer to const (i.e., it points to a const T);
    2. in non-const member function, this pointer is a pointer (i.e., it points to a T).

    Of course, in the MSDN example perhaps the number member variable should be declared mutable instead of resorting to a const_cast.

    EDIT:
    The this pointer is not itself const, according to the MSDN doc quoted. From the C++ Standard:

    regards,
    George

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why this is a non-lvalue in a non-static member function?
    I am curious about that too since the C++ standard makes it clear that "every expression is either an lvalue or an rvalue", so I do not understand why it does not simply say "the keyword this is an rvalue expression". The non-static member function part is obvious since static member functions do not have the this pointer.

    I think this is a pointer variable and it should be addressable, it should be a lvalue?
    A simple definition of lvalue is "an expression that may appear on the left-hand side of an assignment". So by saying that this is not an lvalue, the standard says that the this pointer cannot be on the left hand side of an assignment.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    In early versions of C++, it was actually possible to assign to the this pointer. The standard probably uses the wording it does to make it clear that this is not possible anymore.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi laserlight,


    Thanks for your clarification, I am still confused. Since lvalue can be modifiable and also can not be modifiable. This is const pointer, and addressable, and it should be a un-modifiable lvalue.

    :-)

    Any comments?

    Quote Originally Posted by laserlight View Post
    I am curious about that too since the C++ standard makes it clear that "every expression is either an lvalue or an rvalue", so I do not understand why it does not simply say "the keyword this is an rvalue expression". The non-static member function part is obvious since static member functions do not have the this pointer.


    A simple definition of lvalue is "an expression that may appear on the left-hand side of an assignment". So by saying that this is not an lvalue, the standard says that the this pointer cannot be on the left hand side of an assignment.

    regards,
    George

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is const pointer
    It is not a const pointer. It is a pointer to const.

    and addressable, and it should be a un-modifiable lvalue.
    What exactly do you mean by "addressable"? The standard says that it is a non-lvalue, so it is not an lvalue that cannot be modified, but an rvalue. Consequently, I do not think you can take the address of this.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You can't. Being able to take the address of something (using the native & operator, not an overloaded version) is the main mark of an lvalue - after all, there are const lvalues, which can't appear on the left side of an assignment.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MSDN template sample
    By George2 in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2008, 03:47 AM
  2. extern sample in MSDN
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 02-13-2008, 08:53 AM
  3. MSDN volatile sample
    By George2 in forum C++ Programming
    Replies: 38
    Last Post: 01-05-2008, 06:59 AM
  4. MSDN OLE DB Sample Provider entry point
    By George2 in forum C++ Programming
    Replies: 0
    Last Post: 07-21-2007, 07:30 AM
  5. MSDN Searching Tips
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-19-2004, 04:51 AM