473,563 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_co unt 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 2014
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_co unt 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_co unt 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_co unt 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_bas e_and_derived<b ase_t, T>::value

in conjunction with static asserts:

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

BOOST_STATIC_AS SERT( ::boost::is_bas e_and_derived<b ase_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.RE MOVE.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_co unt 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_co unt 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_bas e_and_derived<b ase_t, T>::value

in conjunction with static asserts:

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

BOOST_STATIC_AS SERT( ::boost::is_bas e_and_derived<b ase_t, T>::value );
HTH

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

Jul 19 '05 #5

"GUMP" <qi*********@ca sco.com.cn> wrote in message
news:bf******** ***@mail.cn99.c om...
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
10143
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 online at http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.15 Below are the two files that I compile with g++ foo.cpp -o...
1
3327
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 to turn a previously made String class that deals with char's into a templated String class that uses the template parameter C instead of char. I...
1
4430
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 'aFunc(A<char> const&)' ) in the following sample program. Tried it on GCC and Borland's compiler. Can somebody please show me the correct way to fix...
5
2690
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: //**************************************************** // testClass.h //**************************************************** #ifndef TESTCLASS_H
6
3313
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 my particular application. One of the advantages is that the _compiler_ can force inner matrix dimensions used in multiplication to agree. A...
8
1968
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 points data to the ostream. Here is the error message: objs/generalpolygons.o(.text+0xf08): In function...
3
3745
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 thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out....
2
11817
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
4024
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
7659
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7580
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7634
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6244
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5481
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5208
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3634
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2079
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.