Making a friend of a template class function

How do I make a friend out of this template function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template <class T> class Handle
{
public:
	Handle(): p(0) { }
	Handle(const Handle& s): p(0) { if (s.p) p = s.p->clone(); }
	Handle(T* t): p(t) { }
	Handle& operator=(const Handle&);
	~Handle() { delete p; }

	operator bool() const { return p; }
	T& operator*() const;
	T* operator->() const;

private:
	T* p;
};


Is
friend class Handle;
sufficient enough? Or what do I do?
That will be a lot of friends.
I don't think you could. You will need to specialize Handle (or templatize the other class).

Edit: I think that I misread it. Where do you pretend to use the friendship?
Last edited on
So how do I specialize it? Like this?
friend class Handle<Core>;

I get a syntax error for the '<'.

Here's where I'm adding the friendship:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifndef GUARD_Core_h

#define GUARD_Core_h

#include <iostream>
#include <string>
#include <vector>

class Core
{
	// Friends
	friend double grade(const Core&);
	friend class Student_info;
	friend class Handle<Core>;
public:
	// Constructors
	Core(): midterm(0), final(0) { }
	Core(std::istream& is) { read(is); }
	// Virtual destructor
	virtual ~Core() {}
	// Virtual functions
	virtual std::istream& read(std::istream&);
	virtual double grade() const;
	/*Virtual function: If the argument is a Grad object,
	it will run the Grad::grade function; if the argument is a Core object, it will run the Core::grade
	function.*/
	/*This run-time selection of the virtual function to execute is relevant only when the function is called
	through a reference or a pointer.*/
	/*In contrast, a reference or pointer
	to a base-class object may refer or point to a base-class object, or to an object of a type derived
	from the base class, meaning that the type of the reference or pointer and the type of the object to
	which a reference or pointer is bound may differ at run time. It is in this case that the virtual
	mechanism makes a difference.*/

	std::string name() const;
	bool valid() const { return !homework.empty(); }
protected:
	virtual Core* clone() const { return new Core(*this); }
	// Accessible to derived classes
	std::istream& read_common(std::istream&);
	double midterm, final;
	std::vector<double> homework;
private:
	// Accessible only to Core or friends
	std::string n;
};

bool compare(const Core&, const Core&);
bool compare_Core_ptrs(const Core*, const Core*);
std::istream& read_hw(std::istream&, std::vector<double>&);

#endif 
Last edited on
How do I get my class to make a template class a friend though? Like I want that any type that's used in the template function to be a friend of Core.

friend template<class T> class Handle;

Doesn't work.


I know I can search this up but I got dial-up, so please bare with me here.
Last edited on
I fixed it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef GUARD_Core_h

#define GUARD_Core_h

#include <iostream>
#include <string>
#include <vector>

template<class T> class Handle;  // Had to add this to fix it

class Core
{
	// Friends
	friend double grade(const Core&);
	friend class Student_info;
	template<class T> friend class Handle;  // Had to add this to fix it
public:
	// Constructors
	Core(): midterm(0), final(0) { }
	Core(std::istream& is) { read(is); }
	// Virtual destructor
	virtual ~Core() {}
	// Virtual functions
	virtual std::istream& read(std::istream&);
	virtual double grade() const;
	/*Virtual function: If the argument is a Grad object,
	it will run the Grad::grade function; if the argument is a Core object, it will run the Core::grade
	function.*/
	/*This run-time selection of the virtual function to execute is relevant only when the function is called
	through a reference or a pointer.*/
	/*In contrast, a reference or pointer
	to a base-class object may refer or point to a base-class object, or to an object of a type derived
	from the base class, meaning that the type of the reference or pointer and the type of the object to
	which a reference or pointer is bound may differ at run time. It is in this case that the virtual
	mechanism makes a difference.*/

	std::string name() const;
	bool valid() const { return !homework.empty(); }
protected:
	virtual Core* clone() const { return new Core(*this); }
	// Accessible to derived classes
	std::istream& read_common(std::istream&);
	double midterm, final;
	std::vector<double> homework;
private:
	// Accessible only to Core or friends
	std::string n;
};

bool compare(const Core&, const Core&);
bool compare_Core_ptrs(const Core*, const Core*);
std::istream& read_hw(std::istream&, std::vector<double>&);

#endif 
The developer office 2010 efforts behind LibreOffice Office 2007 now co e under the Office 2010 purview of The Document Windows 7 Foundation, Office Professional 2007 reflecting a split from the Office 2007 Ultimate open Ultimate source OpenOffice.org Office 2007 Enterprise project Enterprise back in September. The split Office 2010 Key happened not long after office professional plus 2010 Oracle announced the acquisition Office 2010 Pro of Sun Microsystems, which had overseen Windows 7 Home Premium the open source OpenOffice.Buy org Buy Windows 7 project for a decade MS Office 2007 or Home Premium more, basing the code on its StarOffice acquisition.
Last edited on
Topic archived. No new replies allowed.