Re: Is this portable? [static pointer casts, char* arithmetic]
* SG:
On 14 Apr., 19:27, SG <s.gesem...@gmail.com> wrote:
On 14 Apr., 18:03, "Alf P. Steinbach" <al...@start.no> wrote:
Why a member of?
Goals:
- copy-on-write wrapper "cow<>" for value types
- supports conversions from cow<U> to cow<T> in
case Convertible<U*,T*>
- manage the life-time of only one heap allocated object
I should add that I expected such a conversion not to create a new
copy (of the "pointee")
You can have value semantics or reference semantics but not both.
Choose poison! :-)
But one approach that I've investigated is the smart pointer that takes
responsibility for object creation, not just destruction, that is, you provide
the construction arguments to the smart pointer, which delegates. Conclusion
that as of C++98 it's doable but somewhat awful -- lots of macro stuff. And
there's a notational problem regarding default and copy construction.
but just to "work" polymorphically (which is
probably too much to ask for).
A simple example would look like this:
void test_string()
{
cow<string> x = string("hello");
cow<string> y = x;
cout << "*x --> " << *x << endl;
cout << "*y --> " << *y << endl;
cout << "shared = " << (&*x == &*y) << endl;
x.wref() += "123"; // "wref" for write access ...
cout << "*x --> " << *x << endl;
cout << "*y --> " << *y << endl;
cout << "shared = " << (&*x == &*y) << endl;
}
which is supposed to output:
*x --> hello
*y --> hello
shared = 1
*x --> hello123
*y --> hello
shared = 0
Uhm, I like your approach, at least the idea. When I've done this[1] I've always
abstracted the machinery as a building block to be used internally by a class,
while you put it around, which is nicer. :-) My main motivation has so far just
been to explore, though, and in particular having reference counting for an
array where the client code retains only a smart pointer to a part of it, such
as a pointer to a process argument (I believe example given in project [1]).
However, I didn't run into very much need for casting.
Without the polymorphism requirement this would be fairly easy to
achieve. But I also tried to make this code:
struct base
{
private:
virtual void greet_once() const
{ cout << "hello from base\n"; }
public:
void greet() const
{ for (int k=0; k<repeat; ++k) greet_once(); }
int repeat;
};
struct derived : base
{
private:
void greet_once() const
{ cout << "hello from derived\n"; }
public:
~derived()
{ cout << "this is ~derived speaking\n"; }
};
void test_polymorphism()
{
cow<base> x = derived();
For this to work that cow class would need a templated constructor that
dynamically allocated a clone of the derived argument.
x.wref().repeat = 3;
x->greet();
}
to output the following text:
this is ~derived speaking
hello from derived
hello from derived
hello from derived
this is ~derived speaking
where the first "~derived" message is due to the temporary in the copy-
initialization and the 2nd one comes from the copy of derived hidden
behind a type-agnostic abstract wrapper.
Yes, but what's the problem?
Cheers,
- Alf
Notes:
[1] E.g. search for "Alf's stringvalue" in SourceForge if interested. I
discovered later that design issues become thorny when adding into that mix a
vector replacement and support for intrusive ref counting for such vectors. I'll
probably take that up again but I just put it aside when it became "complex"...
--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!