Smart pointer dumping core and specialization question

From: Gonçalo Rodrigues (op73418_at_mail.telepac.pt)
Date: 11/19/04


Date: Thu, 18 Nov 2004 23:47:31 +0000

Hi all,

I'm working around with smart pointers mostly as a learning exercise:
I have a single-rooted hierarchy with the root class Object. Every
Object has a member field count, the number of references to the
Object, and a few methods for dealing with reference counts: getCount,
incCount, decCount with the obvious meanings.

To handle these guys I have made a smart pointer template in the usual
way

template<typename T>
class Ref {
    private:
        T* ptr;

    public:
        //Descriptors.
        T* getPtr() const {
            return this->ptr;
        };

    <etc...>
};

The thingy compiled OK, I wrote a bunch of tests and things were going
OK.

Now, since my hierarchy is single-rooted I thought, hmmm, maybe I
could add up some functionality at Ref<Object> so I started by writing
a full specialization

template<>
class Ref<Object> {
    <etc...>
};

And here I had my first surprise: I add to rewrite every single method
for the full specialization even if in most cases it was nothing more
than picking in the Ref<T> general code and replacing T by Object. So
here goes my first question:

Is there any reason why the compiler just doesn't do the obvious
thing? e.g. if a method is not reimplemented in the full
specialization just use the template code replacing the type
parameters?

But my biggest surprise came afterwards. I thought, gee wouldn't be
nice if Derived derived from Object then there is an automatic
conversion Ref<Derived> to Ref<Object>. So in the full specialization
I just changed the copy-constructor from:

Ref(const Ref& ref) : ptr(ref.getPtr()) {
    ref->incCount();
};

to

template<typename T>
Ref(const Ref<T>& ref) : ptr(ref.getPtr()) {
    ref->incCount();
};

The thing still compiled OK but the first time I ran my tests, a
little dialog box popped up saying that I tried to write to
non-writable memory (this in win2k, the compiler is the one from the
free VC++ toolkit).

Anyone has an inkling of what might be going wrong?

TIA, with my best regards,
G. Rodrigues



Relevant Pages

  • Re: ref vs. out: same at runtime, same/different at compiler time?
    ... This is what I'm referring to and this is what I ... If I'm writing code that calls the method, the compiler would not be able to ... tell the difference between the ref and the out method, ... static void Foo ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: reading a declaration
    ... > argument by pointer with its address, or by reference. ... What I mean is: #define ref x ... does EXCEPT with define there is no scope or type checking. ... couldn't a compiler implement the references by precompiling them ...
    (comp.lang.cpp)
  • Re: Incorrect "variable might not have been initialized"
    ... String ref; ... It's clearly Boolean;-) And true. ... are formally constant. ... (or if the compiler is incorrect then so is the JLS in this matter). ...
    (comp.lang.java.programmer)
  • Re: please help me understand ref and out
    ... "out" is a more specific form of "ref". ... By giving the compiler more ... the opportunity to find bugs at compile time rather than at run time. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Smart pointer template question
    ... >> Suppose you have a hierarchy of objects that you deal with via smart ... >> pointer to a derived class, so what is the best way to write the Ref ... smart pointers are conceptually distinct things. ...
    (comp.lang.cpp)