Like Tree1Likes
  • 1 Post By Elysia

const_cast and invalid objects

This is a discussion on const_cast and invalid objects within the C++ Programming forums, part of the General Programming Boards category; Hi there, I am given a pointer: T* (where T is a non-CV type). I do not know whether it ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682

    const_cast and invalid objects

    Hi there,

    I am given a pointer: T* (where T is a non-CV type). I do not know whether it points to a valid object or not. It can be given any value and it may be valid or not. I would like to add constness to it using const_cast:

    Code:
    T* t1 = <possible invalid address, not necessarily null>;
    const T* t2 = const_cast<const T*>(t1);
    Is this allowed? Notice that I do not access this object. The object is accessed later (proper validation is done at run-time).

    I have been looking in the standard and I could not find the answer (const_cast for NULL is defined, but for other addresses?).

    I know that in practice it is allowed (since it does not access any v-table or such). Boost's weak_ptr also makes such an assumption even for other casts, so I am going to implement it too, but anyway, I would like to know the answer.

    Thanks!
    I never put signature, but I decided to make an exception.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,828
    You do not need const_cast to add const. You only need it to remove const.
    If you have a pointer, p, of type T*, then const T* p2 = p will do what you want.
    Btw, it's bad practice to have any pointer that may be invalid. Make sure the pointers are always valid.
    iMalc likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    This is an implementation of weak references and it is ensured that they are always valid when accessed. The difference is that regular cast allows casting from a derieved type (which would be fine if it were defined too).
    I never put signature, but I decided to make an exception.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MSDN const_cast sample
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 12-17-2007, 07:32 AM
  2. const_cast operator
    By vb.bajpai in forum C++ Programming
    Replies: 7
    Last Post: 06-25-2007, 01:24 PM
  3. Insight Into const_cast on const data-members
    By shiv_tech_quest in forum C++ Programming
    Replies: 11
    Last Post: 01-21-2003, 04:53 AM
  4. Information Regarding const_cast on DataMembers
    By shiv_tech_quest in forum C++ Programming
    Replies: 0
    Last Post: 01-15-2003, 09:25 PM
  5. const_cast: what is it good for?
    By mavroprovato in forum C++ Programming
    Replies: 13
    Last Post: 01-13-2003, 04:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21