473,399 Members | 3,603 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,399 software developers and data experts.

Grantig friend status to template

Hello,

I'm trying to created base class and template for "smart" pointer
pointing to it. It should do simple reference counting and clean up when
the reference count reaches zero. The header looks like this:

class base_t {
int _ref_count;
public:
base_t(void): _ref_count(0) {};
}

template <typename T>
class ptr_t {
T *_target;
public:
ptr_t(void): _target(NULL) {};
ptr_t(T *);
ptr_t(ptr_t &);
virtual ~ptr_t(void);
T *operator->(void) {return _target;};
T &operator*(void) {return *_target;};
const T &operator*(void) const {return *_target;};
ptr_t &operator=(ptr_t &);
operator bool(void) const {return bool(_target);};
};

My intention is to create descendant classes like:

class child_t: public base_t
{
// ... members here
public:
// ... more members here
};

typedef ptr_t<base_t> child_p;

I have following questions:

1) Because base_t::_ref_count is private I have to make ptr_t it's
friend - but it is template. How can I specify that all classes created
with ptr_t<T> are friends of base_t?

2) Is there way to specify that type T in ptr_t<T> must be direct (or
indirect) descendant of base_t type?

Thank you all in advance.
Ales
Jul 19 '05 #1
5 2002
On Sun, 20 Jul 2003 23:01:21 +0200
Ales DOLECEK <al****@seznam.cz> wrote:
My intention is to create descendant classes like:

class child_t: public base_t
{
// ... members here
public:
// ... more members here
};

typedef ptr_t<base_t> child_p;


Should be

typedef ptr_t<child_t> child_p;

of course.
Ales
Jul 19 '05 #2
On Sun, 20 Jul 2003 23:01:21 +0200
Ales DOLECEK <al****@seznam.cz> wrote:
Hello,

I'm trying to created base class and template for "smart" pointer
pointing to it. It should do simple reference counting and clean up
when the reference count reaches zero. The header looks like this:

class base_t {
int _ref_count;
public:
base_t(void): _ref_count(0) {};
}

template <typename T>
class ptr_t {
T *_target;
public:
ptr_t(void): _target(NULL) {};
ptr_t(T *);
ptr_t(ptr_t &);
virtual ~ptr_t(void);
T *operator->(void) {return _target;};
T &operator*(void) {return *_target;};
const T &operator*(void) const {return *_target;};
ptr_t &operator=(ptr_t &);
operator bool(void) const {return bool(_target);};
};

My intention is to create descendant classes like:

class child_t: public base_t
{
// ... members here
public:
// ... more members here
};

typedef ptr_t<base_t> child_p;

I have following questions:

1) Because base_t::_ref_count is private I have to make ptr_t it's
friend - but it is template. How can I specify that all classes
created with ptr_t<T> are friends of base_t?

2) Is there way to specify that type T in ptr_t<T> must be direct (or
indirect) descendant of base_t type?

Thank you all in advance.
Ales


I could also declare the smart pointer as inner class of base_t

class base_t
{
int _ref_count;
protected:
class pointer
{
...
}
public:
base_t(viod);
}

but then I don't know how could I create descendants of base_t::pointer.

Ales
Jul 19 '05 #3
Ales DOLECEK wrote in news:20****************************@seznam.cz:
Hello,

I'm trying to created base class and template for "smart" pointer
pointing to it. It should do simple reference counting and clean up
when the reference count reaches zero. The header looks like this:

1st a forward declaration:

template <typename T> class ptr_t;
class base_t {
int _ref_count;
public:
base_t(void): _ref_count(0) {};
template <typename T>
friend class ptr_t;

Are you're missing a virtual dtor here ?:

virtual ~base_t{} {}
}

template <typename T>
class ptr_t {
T *_target;
public:
ptr_t(void): _target(NULL) {};
ptr_t(T *);
ptr_t(ptr_t &);
Are you intending to derive from this class ?
virtual ~ptr_t(void); T *operator->(void) {return _target;};
T &operator*(void) {return *_target;};
const T &operator*(void) const {return *_target;};
ptr_t &operator=(ptr_t &);
operator bool(void) const {return bool(_target);};
};

My intention is to create descendant classes like:

class child_t: public base_t
{
// ... members here
public:
// ... more members here
};

typedef ptr_t<base_t> child_p;

I have following questions:

1) Because base_t::_ref_count is private I have to make ptr_t it's
friend - but it is template. How can I specify that all classes
created with ptr_t<T> are friends of base_t?

2) Is there way to specify that type T in ptr_t<T> must be direct (or
indirect) descendant of base_t type?


in ptr_t<> add:

base_t *as_base() { return _target; }

then when you need to reference base_t::_ref_count do it as:

as_base()->_ref_count

If you do this at least once in the destructor ~ptr_t() then
ptr_t<T> won't compile unless T is convertible to base_t.

Alternatively:

If you have (or can get) boost (www.boost.org) then use:

::boost::is_base_and_derived<base_t, T>::value

in conjunction with static asserts:

http://www.boost.org/libs/static_ass...tic_assert.htm

BOOST_STATIC_ASSERT( ::boost::is_base_and_derived<base_t, T>::value );
HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #4
The declaration

template <typename T>
friend class ptr_t;

cann't get through the Microsft C++ compiler(I use VC 6.0),
can you tell me why?
thank in advance.
"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.130...
Ales DOLECEK wrote in news:20****************************@seznam.cz:
Hello,

I'm trying to created base class and template for "smart" pointer
pointing to it. It should do simple reference counting and clean up
when the reference count reaches zero. The header looks like this:


1st a forward declaration:

template <typename T> class ptr_t;
class base_t {
int _ref_count;
public:
base_t(void): _ref_count(0) {};


template <typename T>
friend class ptr_t;

Are you're missing a virtual dtor here ?:

virtual ~base_t{} {}
}

template <typename T>
class ptr_t {
T *_target;
public:
ptr_t(void): _target(NULL) {};
ptr_t(T *);
ptr_t(ptr_t &);


Are you intending to derive from this class ?
virtual ~ptr_t(void);

T *operator->(void) {return _target;};
T &operator*(void) {return *_target;};
const T &operator*(void) const {return *_target;};
ptr_t &operator=(ptr_t &);
operator bool(void) const {return bool(_target);};
};

My intention is to create descendant classes like:

class child_t: public base_t
{
// ... members here
public:
// ... more members here
};

typedef ptr_t<base_t> child_p;

I have following questions:

1) Because base_t::_ref_count is private I have to make ptr_t it's
friend - but it is template. How can I specify that all classes
created with ptr_t<T> are friends of base_t?

2) Is there way to specify that type T in ptr_t<T> must be direct (or
indirect) descendant of base_t type?


in ptr_t<> add:

base_t *as_base() { return _target; }

then when you need to reference base_t::_ref_count do it as:

as_base()->_ref_count

If you do this at least once in the destructor ~ptr_t() then
ptr_t<T> won't compile unless T is convertible to base_t.

Alternatively:

If you have (or can get) boost (www.boost.org) then use:

::boost::is_base_and_derived<base_t, T>::value

in conjunction with static asserts:

http://www.boost.org/libs/static_ass...tic_assert.htm

BOOST_STATIC_ASSERT( ::boost::is_base_and_derived<base_t, T>::value );
HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/

Jul 19 '05 #5

"GUMP" <qi*********@casco.com.cn> wrote in message
news:bf***********@mail.cn99.com...
The declaration

template <typename T>
friend class ptr_t;

cann't get through the Microsft C++ compiler(I use VC 6.0),
can you tell me why?
thank in advance.


Because VC++ 6's handling of templates is v. poor.

john
Jul 19 '05 #6

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

Similar topics

2
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available...
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
1
by: Dmitry D | last post by:
Hi all, I'm having problems with declaring a template friend function. It seems like I've done everything as explained in C++ FAQ, but still, I'm getting the linker error (unresolved external...
5
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: ...
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
8
by: mast2as | last post by:
Hi guys, I think from what I found on the net that the code is correct and there's no problem when I compile the files. The problems occurs when I link them. I have a friend function which outputs...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
2
by: fdmfdmfdm | last post by:
I have the following code: #include <iostream> #include <cstdlib> #include <cassert> using namespace std; template <class T> class Stack{ public: enum{DefaultStack = 10, EmptyStack = -1};
9
by: rtalbot | last post by:
I've got a container that looks like this: template <class T> class Foo { public: Foo() : _data(), _status(1) { } Foo(T) : _data(T), _status(0) { } ~Foo() { }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.