MSDN const_cast sample

Discussion in 'C++' started by George2, Dec 17, 2007.

  1. George2

    George2 Guest

    Hello everyone,


    In MSDN sample for const_cast,

    http://msdn2.microsoft.com/en-us/library/bz6at95h(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
    George2, Dec 17, 2007
    #1
    1. Advertising

  2. George2

    Jim Langston Guest

    George2 wrote:
    > Hello everyone,
    >
    >
    > In MSDN sample for const_cast,
    >
    > http://msdn2.microsoft.com/en-us/library/bz6at95h(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?


    My simple test (which may or may not work in your implementation) seems to
    suggest this.

    Output is:
    class Foo *
    class Foo const *

    #include <iostream>

    class Foo
    {
    public:
    void ShowType() { std::cout << typeid(this).name() << "\n"; }
    void ShowTypeConst() const { std::cout << typeid(this).name() << "\n"; }
    private:
    };

    int main()
    {
    Foo Bar;
    Bar.ShowType();
    Bar.ShowTypeConst();
    }

    But, yes, in a const member function, the this* is const.
    --
    Jim Langston
    Jim Langston, Dec 17, 2007
    #2
    1. Advertising

  3. Jim Langston wrote:
    > [..]
    >
    > But, yes, in a const member function, the this* is const.


    I'd probably say that "the *this is const". I am sure you
    meant to dereference the pointer.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 17, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Catherine Jones

    Help Urgently!! MSDN sample not working!!

    Catherine Jones, Jan 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    474
    Paul Glavich
    Jan 3, 2004
  2. Andy

    sample survey msdn

    Andy, Aug 20, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    386
  3. =?Utf-8?B?Q2hhZA==?=

    MSDN Survey Sample

    =?Utf-8?B?Q2hhZA==?=, Feb 26, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    383
    =?Utf-8?B?Q2hhZA==?=
    Feb 26, 2004
  4. =?Utf-8?B?amV2ZQ==?=

    MSDN Survey Sample don't work!

    =?Utf-8?B?amV2ZQ==?=, Feb 26, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    397
    =?Utf-8?B?amV2ZQ==?=
    Feb 26, 2004
  5. Jéjé
    Replies:
    0
    Views:
    403
    Jéjé
    Oct 14, 2004
Loading...

Share This Page