473,387 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Smart pointer dumping core and specialization question

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

Jul 22 '05 #1
0 957

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
10
by: ken | last post by:
hello, i'm writing a c program on a linux system. i'm debugging a segmentation fault but i don't want it to dump a core file because the memory footprint of the program is over 300Mb and i don't...
14
by: Ian | last post by:
I am looking at porting code from a C++ application to C#. This requires implementing data sharing functionality similar to what is provided by a smart pointer in C++. I have only recently begun...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
14
by: siddhu | last post by:
Dear Experts, In the following Program class A { public: void print() { cout<<"Hello World"<<endl;
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
25
by: Jrdman | last post by:
is it possible to use a pointer to see the data stored in a given address in the memory ? if no what's the appropriate way to do that? thanks.
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.