472,353 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

template method question

Hi all,

The following (test) program is not working as I expected.

Note: The code is a bit long but is easy to understand: There is a
class Object implementing ref counting. A Ref<T> template (with a full
Ref<Object> specialization) that works like a smart pointer to the
Object-rooted hierarchy. The only way to create Object instances is
via a static factory that returns a Ref<Object> to a heap allocated
object.

#include <iostream>

//Macros.
#ifndef NULL
#define NULL 0
#endif

//Templates.
template<typename T>
class Ref {
public:
//Constructors.
explicit Ref(T* ptr) {
if (NULL == ptr) {
throw "NULL pointer.";
};
this->ptr = ptr;
ptr->incCount();
};

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

//Destructor.
~Ref() {
this->ptr->decCount();
if (0 == this->ptr->getCount()) {
delete this->ptr;
};
this->ptr = NULL;
};

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

//Operator protocol.
T& operator*() const {
return *(this->ptr);
}

T* operator->() const {
return this->ptr;
};

private:
T* ptr;
};

//Forward declaration.
class Object;

//Full specialization.
template<>
class Ref<Object> {
public:
//Constructors.
explicit Ref(Object* ptr) {
if (NULL == ptr) {
throw "NULL pointer.";
};
this->ptr = ptr;
ptr->incCount();
};

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

//Destructor.
~Ref() {
this->ptr->decCount();
if (0 == this->ptr->getCount()) {
delete this->ptr;
};
this->ptr = NULL;
};

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

//Operator protocol.
Object& operator*() const {
return *(this->ptr);
}

Object* operator->() const {
return this->ptr;
};

private:
Object* ptr;
};

class Object {
public:
static Ref<Object> create() {
return Ref<Object>(new Object);
};

virtual ~Object() {};

//Reference count helpers.
long getCount() {
return this->count;
};

void incCount() {
this->count++;
};

void decCount() {
this->count--;
};

protected:
//Protect constructors.
Object() : count(0) {};

private:
long count;

//Disallow.
Object(const Object& obj);
Object& operator=(const Object& rhs);
};

int main() {
Ref<Object> ref = Object::create();
std::cout << "Object count: " << ref->getCount() << std::endl;
std::cout << "Object pointer: " << (long)ref.getPtr() <<
std::endl;
Ref<Object> ref1(ref);
std::cout << "Object count: " << ref1->getCount() <<
std::endl;
std::cout << "Object pointer: " << (long)ref1.getPtr() <<
std::endl;
};

Compiling and running is fine and dandy. I get:

Object count: 1
Object pointer: 3082872
Object count: 2
Object pointer: 3082872

But once I replace the copy-constructor in the full specialization
Ref<Object> by the template method

template<typename U>
Ref(const Ref<U>& ref) : ptr(ref.getPtr()) { //Upcast here.
this->ptr->incCount();
};

I get the output

Object count: 1
Object pointer: 3082872
Object count: 1
Object pointer: 3082872

The reference counter is not being updated - although the pointer is
getting assigned. Anyone knows what is wrong with the above template
method? Any ideas?

TIA, best regards,
G. Rodrigues
Jul 22 '05 #1
6 1614
Gonçalo Rodrigues wrote in news:of********************************@4ax.com
in comp.lang.c++:
But once I replace the copy-constructor in the full specialization
Ref<Object> by the template method

template<typename U>
Ref(const Ref<U>& ref) : ptr(ref.getPtr()) { //Upcast here.
this->ptr->incCount();
};


Its called a converting constructor and it doesn't implement
the copy constructor, when the compiler need to copy it will
use the implicitly generated (memberwise) copy constructor.

Leave the copy constructor in place and *add* this converting
constructor.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
On 19 Nov 2004 19:12:21 GMT, Rob Williscroft <rt*@freenet.co.uk>
wrote:
Gonçalo Rodrigues wrote in news:of********************************@4ax.com
in comp.lang.c++:
But once I replace the copy-constructor in the full specialization
Ref<Object> by the template method

template<typename U>
Ref(const Ref<U>& ref) : ptr(ref.getPtr()) { //Upcast here.
this->ptr->incCount();
};

Its called a converting constructor and it doesn't implement
the copy constructor, when the compiler need to copy it will
use the implicitly generated (memberwise) copy constructor.


Ahhh... understood.
Leave the copy constructor in place and *add* this converting
constructor.


Thanks, adding the template method (instead of replacing) does make
the output right.

Another question: I'd assume that, since if not implemented the
compiler also synthesizes an operator assignment for you, then I have
to do the same thing, that is, implement an assignment operator then
add the template method operator=, right?

TIA, With my best regards,
G. Rodrigues

Jul 22 '05 #3
Gonçalo Rodrigues wrote in news:ih********************************@4ax.com
in comp.lang.c++:

Thanks, adding the template method (instead of replacing) does make
the output right.

Another question: I'd assume that, since if not implemented the
compiler also synthesizes an operator assignment for you, then I have
to do the same thing, that is, implement an assignment operator then
add the template method operator=, right?


Yep.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4
I've looked through the code that the OP gave.
Now I've a question after trying the code.
Why is the specialisation needed at all?
Am I missing a vital reason for it?
Jul 22 '05 #5
On 22 Nov 2004 05:53:48 -0800, ve*********@hotmail.com (velthuijsen)
wrote:
I've looked through the code that the OP gave.
Now I've a question after trying the code.
Why is the specialisation needed at all?
Am I missing a vital reason for it?


I believe this is about a post of mine - but I am guessing here, you
should keep the quotes.

My answer: no you haven't missed nothing. I've pushed the
specialization code in Ref<Object> to the general template Ref<T> and
deleted the (full) specialization.

With my best regards,
G. Rodrigues
Jul 22 '05 #6
> >I've looked through the code that the OP gave.
Now I've a question after trying the code.
Why is the specialisation needed at all?
Am I missing a vital reason for it?


I believe this is about a post of mine - but I am guessing here, you
should keep the quotes.

My answer: no you haven't missed nothing. I've pushed the
specialization code in Ref<Object> to the general template Ref<T> and
deleted the (full) specialization.


Thank you.
It was (at least for me) not a wasted effort. I Learned a bit more
about how to work with templates from it.
Jul 22 '05 #7

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

Similar topics

2
by: Alex Vinokur | last post by:
========================================= Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927 (prerelease)...
11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo...
0
by: Bernd Fuhrmann | last post by:
Hi! I have two (or more) templates for one certain tag: The first one: tobeimported.xsl: <?xml version="1.0" encoding="iso-8859-1"?>...
3
by: Microsoft | last post by:
Is there a way of implementing something like the Template Method design pattern, but only dealing with static methods ? First, I realized that I...
3
by: Yohan | last post by:
Hello, I have a question concerning the template classes and their parameters. Is it possible to set a condition on the template parameters in a...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does...
16
by: Jeroen | last post by:
Hi all, I have a question which is illustrated by the following piece of code: template <class T> class A { T my_value; }; In a list,...
3
by: Thomas Pajor | last post by:
Hey everybody, I got into serious trouble with template programming. I have a class which uses three template arguments, say ...
4
by: David Sanders | last post by:
Hi, I have a class depending on a template parameter: template<int n> class MyClass { }; I want the template parameter to be chosen...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

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.