x::const_ref and x::const_ptr
x::const_ref
and
x::const_ptr
are versions of
x::ref and x::ptr
whose * and -> operators
resolve to a constant object,
similar to the relation between std::iterator
and std::const_iterator in the C++ library.
They are subclasses of x::ref and
x::ptr (actually they're superclasses, but
it's easier to think of them as subclasses).
Passing an x::ref or an x::ptr to a function that takes an equivalent const
argument does not require the creation of a temporary object:
class buttonObj : virtual public x::obj { // ... }; typedef x::ref<buttonObj> button; typedef x::ptr<buttonObj> buttonptr; typedef x::const_ref<buttonObj> const_button; typedef x::const_ptr<buttonObj> const_buttonptr; // ... void draw(const const_button &b); // ... button buttonref=button::create(); // ... draw(buttonref);
In this example, “draw()” does not create a temporary.
It just passes a reference to the x::const_ref
superclass of the x::ref, with the contract
that the x::const_ref gives access only to
const class methods and members.
The above example shows LibCXX's naming convention for reference-counted objects and reference types:
| Name | What it is |
|---|---|
class |
Virtually derived from x::obj, a
reference-counted class.
|
typedef x::ref<> |
A non-nullptr reference to a reference-counted
object.
(args)
calls new ,
and returns a reference to the newly-constructed object.
|
typedef x::const_ref<> const_ |
A non-nullptr reference to a
constant reference-counted object.
* and -> operators
resolve to a constant object.
|
typedef x::ptr<> |
A reference-counted pointer, which may be a
nullptr.
(args)
calls new ,
and returns a pointer reference to the newly-constructed object.
Dereferencing a nullptr
throws an exception.
|
typedef x::const_ptr<const_> const_ |
A reference-counted pointer to a constant object, which may be a
nullptr.
* and -> operators
resolve to a constant object.
Dereferencing a nullptr
const_
throws an exception.
|