Re: Only reference parameters
On Dec 14, 12:46 pm, Frank Birbacher <bloodymir.c...@gmx.net> wrote:
Hi!
pfultz2 schrieb:
A deep const pointer doesnt allow a copy from a const reference.
Why is that? I expect I can copy things which are declared "const".
Copying should not modify the source, or it wouldn't be called "copy".
What are the semantics of your deep const pointer class in this regard?
It doesnt allow a copy because then you could break the deep const
semantics. For example if i have this:
//its const so i dont want to modify the pointer or what it references
void foo(const Pointer<T>& x)
{
Pointer<T> y = x;
y->modifyObjectMethod();//Since i made a copy i can now modify it,
because x is const but y is not
}
Using a deep
const pointer is interesting, because it is copy constructible, but
its not const copyable, in order to perserve the deep constness of the
reference.
I don't understand the difference between "copy constructible" and
"copyable". To me both refer to the copy constructor of a class:
struct Copyable {
Copyable(Copyable const&); // copy constructor
};
The difference is with a pointer that have deep const semantics. In
this sense non-const gives certain access right to the object. And
these access rights are copied with pointer. When you have const
reference to the pointer, the access rights are limited so a different
object is used, so if i need a copy of the pointer i use a different
pointer with limited access rights, like my previous example:
void foo(const Pointer<T>& x)
{
ReadOnlyPointer<T> y = x;//This is ok, since ReadOnlyPointer limits
my access to const only
}
So i can always copy the object it just depends what privileges i have
been given. So the deep pointer is always copyable, but not always
const copyable. It still is copy constructible, i can still do this:
Pointer<T> x;
Pointer<T> y = Pointer<T>(x);
This is because it has a copy constructor and a move constructor:
Pointer<T>(Pointer<T>& rhs);
Pointer<T>(Pointer<T>&&rhs);
but no const copyable constructor
Now i can put this in an STL container because it is copy-
constructible, but i have other problems since it is not const
copyable. So instead i have created my own container type to handle
these that either have deep const semantics or are reference only
types.
paul
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]