object initialized to zero(not a pure virtual function)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • babu198649
    New Member
    • Sep 2007
    • 22

    object initialized to zero(not a pure virtual function)

    hi
    i saw a program which inside its class initializes another class object to zero.
    also if i initialize to some other number other than zero ,an error occurs.

    Code:
    Circle* cp = 0;
    this is not related with pure virtual function since it is an object not a function.

    can some one could explain what it is.
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    Originally posted by babu198649
    hi
    i saw a program which inside its class initializes another class object to zero.
    also if i initialize to some other number other than zero ,an error occurs.

    Code:
    Circle* cp = 0;
    this is not related with pure virtual function since it is an object not a function.

    can some one could explain what it is.
    A pointer with a value of zero is called a null pointer; it means the pointer points to nothing.
    In C++ it's more common to use Circle* cp = NULL; to make the intention more clear. Either way, pointers should always be initialized to NULL (or 0); otherwise they may point to garbage and produce difficult-to-find bugs.

    Comment

    • babu198649
      New Member
      • Sep 2007
      • 22

      #3
      thanks......... ...........

      Comment

      • babu198649
        New Member
        • Sep 2007
        • 22

        #4
        does pointing to 0(NULL) will not point to data in 0th location

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by scruggsy
          Circle* cp = NULL;
          This is C. There is no NULL in C++. Use 0.

          Comment

          • scruggsy
            New Member
            • Mar 2007
            • 147

            #6
            Originally posted by weaknessforcats
            This is C. There is no NULL in C++. Use 0.
            Really? My mistake. Thanks.
            In any case, babu, if you try to dereference a pointer with a value of 0, most operating systems generate an exception. If you dereference an uninitialized pointer, on the other hand, the OS can't tell that you've made a mistake, which makes the error much more difficult for you to track down.

            Comment

            Working...