UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C++
  • » How to implement class template as friend?

#1 2009-07-25 12:43 PM

tomcio
Member
From: Poland
Registered: 2006-08-14
Posts: 113

Re: How to implement class template as friend?

Hello!

I have a class with private constructor and destructor. In one method from this class I create instance of it using std::auto_ptr to avoid memory leaks. However std::Ptr cannot access my class destructor because it's private...

I thought, that I can make specialized std::auto_ptr template friend of my class, something like this:

class A
{
    friend class std::auto_ptr <A>;
};

But this doesn't compile :( How to implement this properly (if it's possible) ?
I'm still learning how to use templates in C++

Offline

#2 2009-07-28 09:17 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: How to implement class template as friend?

Hi

What erros do you get?
The code below compiles and works ok.

#include <memory>
#include <iostream>


class A
{
public:
  friend class std::auto_ptr<A>;
  
  static std::auto_ptr<A> Create()
    {
    return std::auto_ptr<A>(new A);
    }
    
private:
  A(){std::cout << "A ctor" << std::endl;}
  ~A(){std::cout << "A destrucor" << std::endl;}
};

int main()
{
std::auto_ptr<A> ptr = A::Create();
return 0;
}

Offline

#3 2009-07-28 11:07 AM

tomcio
Member
From: Poland
Registered: 2006-08-14
Posts: 113

Re: How to implement class template as friend?

I found bug, I included "memory.h" instead of "memory".

However I found something interesting. I'm testing new C++0x features introduced in GCC 4.4 (I use v4.4.1 on my Ubuntu). This code:

class A
{
public:
  friend class std::auto_ptr<A>;
  
  static void create (); // use 'std::auto_ptr <A>' somewhere inside
    
private:
  A ();
  ~A ();
};

works fine with auto_ptr, but when I replace auto_ptr with unique_ptr (recommended by C++0x) GCC fails to compile code and returns:

error: ‘virtual A::~A()’ is private
/usr/include/c++/4.4/bits/unique_ptr.h:64: error: within this context

What do you think about this error? I think it's GCC/libstdc++ bug...

Offline

#4 2009-07-28 11:33 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: How to implement class template as friend?

Actually i'm not familiar with C++0x and unique_ptr.
Maybe you find this link useful.

http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=400

Btw, you are not giving enough input information in your posts.

Offline

#5 2009-07-28 11:45 AM

tomcio
Member
From: Poland
Registered: 2006-08-14
Posts: 113

Re: How to implement class template as friend?

yurec;27144 wrote:

Btw, you are not giving enough input information in your posts.

Well, sorry for that, I was in rush... I realize, that i didn't proviede enough information.

Thanks for the link!

Offline

  • Index
  • » C++
  • » How to implement class template as friend?

Board footer

Powered by FluxBB