The Artima Developer Community
Sponsored Link

Weblogs Forum
Overloading operator&() in C++

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Overloading operator&() in C++ (View in Weblogs)
Posted: Apr 8, 2005 7:18 PM
Reply to this message Reply
Summary
My colleague Matthew Wilson made a post recently suggesting that overloading operator&() should be avoided. While I agree with much of what he says, I would like to suggest a valid use for overloading operator&() which enables writing safer code.
Advertisement
In response to Matthew Wilson's recent post at the C++ Source about overloading operator&(), I would like to submit a valid use case. Consider the following code:

  SomeObject o;
  SomeObject* p;
  p = &o;
  delete p; // dumb but legal
I know you are thinking that the code example is simply asinine, but it is intended as a purely artificial example of what can and does happen in real world scenarios. This is due to what I percieve as a flaw in the C++ type system, which has only one kind of pointer. If you think about it from, it does seem silly for a strongly typed language to allow you to so easily delete stack allocated objects. This can be easily fixed by writing some trivial pointer classes which I call unsmart pointers.

You can use an undeletable unsmart pointer to represent the return type of a call to operator&(). An unsmart pointer is a pointer class which does not automatically destroy the resource, and undeletable means that the pointer does not provide any method to delete the resource it references.

Consider the following implementation of SomeObject which overloads operator&().

  #include <ootl/ootl_ptr.hpp>

  struct SomeObject {
    typedef ootl::ptr<SomeObject, !ootl::deletable> undeletable_ptr;
    undeletable_ptr operator&() {
      return this;
    }
  };

  int main() {
    SomeObject o;
    SomeObject* p;
    p = &o; // compiler error; undeletable pointers can not be assigned to raw pointers.
    delete p;
    return 0;
  }
Today I just sent in my final revision on make next installment of Agile C++ for the C++ Users Journal, Unsmart Pointers Part 1, which not surprisingly covers undeletable and their complement deletable pointers in more depth.

You can download the unsmart pointer source code as part of the most recent ootl release at http://www.ootl.org or view the source online at http://www.ootl.org/ootl/ootl_ptr.hpp.htm.

I would like to point out that even if I disagree on this small point that I own and heartily recommend Matthew's Book Imperfect C++.

Topic: Bob Martin's Agile Programming book Previous Topic   Next Topic Topic: My Nerd Score: 99

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use