- Examples

In this lesson, we will take a look at different examples of friends.

Example 1

Press + to interact
// templateClassTemplateGeneralFriendship.cpp
#include <iostream>
template <typename T> void myFriendFunction(T);
template <typename U> class MyFriend;
class GrantingFriendshipAsClass{
template <typename U> friend void myFriendFunction(U);
template <typename U> friend class MyFriend;
private:
std::string secret{"My secret from GrantingFriendshipAsClass."};
};
template <typename T>
class GrantingFriendshipAsClassTemplate{
template <typename U> friend void myFriendFunction(U);
template <typename U> friend class MyFriend;
private:
std::string secret{"My secret from GrantingFriendshipAsClassTemplate."};
};
template <typename T>
void myFriendFunction(T){
GrantingFriendshipAsClass myFriend;
std::cout << myFriend.secret << std::endl;
GrantingFriendshipAsClassTemplate<double> myFriend1;
std::cout << myFriend1.secret << std::endl;
}
template <typename T>
class MyFriend{
public:
MyFriend(){
GrantingFriendshipAsClass myFriend;
std::cout << myFriend.secret << std::endl;
GrantingFriendshipAsClassTemplate<T> myFriend1;
std::cout << myFriend1.secret << std::endl;
}
};
int main(){
std::cout << std::endl;
int a{2011};
myFriendFunction(a);
MyFriend<double> myFriend;
std::cout << std::endl;
}

Explanation

  • In the above example, we created a function myFriendFunction and a class MyFriend. We have defined two classes: ...

Access this course and 1400+ top-rated courses and projects.