friend

template friend functions of template class

情到浓时终转凉″ 提交于 2020-07-15 07:46:47
问题 I have the following template class and template function which intends to access the class' private data member: #include <iostream> template<class T> class MyVar { int x; }; template<class T> void printVar(const MyVar<T>& var) { std::cout << var.x << std::endl; } template<class T> void scanVar(MyVar<T>& var) { std::cin >> var.x; } struct Foo {}; int main(void) { MyVar<Foo> a; scanVar(a); printVar(a); return 0; } To declare the two functions as MyVar<T> 's friend functions, I've tried the

Why can't forward declared friend class be referred in the class?

我与影子孤独终老i 提交于 2020-05-10 03:44:50
问题 The following code doesn't compile: struct X { friend class Y; Y* ptr; }; The cppreference describes the situation as ... If the name of the class that is used in the friend declaration is not yet declared, it is forward declared on the spot. If the "spot" means where the friend relationship is declared, then it should be fine to declare the member Y* ptr . Why doesn't it compile? Where in the standard prohibits this? 回答1: This is a mistake on the site. It contradicts the standard, which says

Why can't forward declared friend class be referred in the class?

时光总嘲笑我的痴心妄想 提交于 2020-05-10 03:44:27
问题 The following code doesn't compile: struct X { friend class Y; Y* ptr; }; The cppreference describes the situation as ... If the name of the class that is used in the friend declaration is not yet declared, it is forward declared on the spot. If the "spot" means where the friend relationship is declared, then it should be fine to declare the member Y* ptr . Why doesn't it compile? Where in the standard prohibits this? 回答1: This is a mistake on the site. It contradicts the standard, which says

Why can't forward declared friend class be referred in the class?

有些话、适合烂在心里 提交于 2020-05-10 03:42:59
问题 The following code doesn't compile: struct X { friend class Y; Y* ptr; }; The cppreference describes the situation as ... If the name of the class that is used in the friend declaration is not yet declared, it is forward declared on the spot. If the "spot" means where the friend relationship is declared, then it should be fine to declare the member Y* ptr . Why doesn't it compile? Where in the standard prohibits this? 回答1: This is a mistake on the site. It contradicts the standard, which says

C++ 关键字——friend

▼魔方 西西 提交于 2020-03-30 17:07:19
友元是指: 采用类的机制后实现了数据的隐藏与封装,类的数据成员一般定义为私有成员,成员函数一般定义为公有的,依此提供类与外界 间的通信接口。但是,有时需要定义一些函数,这些函数不是类的一部分(注意友元函数不是类的一部分),但又需要频繁地 访问类的数据成员,这时可以将这些函数定义为该函数的友元函数。除了友元函数外,还有友元类,两者统称为友元。友元的作用是提高了程序的运行效率(即减少了类型检查和安全性检查等都需要时间开销),但它破坏了类的封装性和隐藏性,使得非成员函数可以访问类的私有成员。 class Data{ public: ... friend int set(int &m);//友元函数friend class peop; //友元类 ... } 友元分为友元函数和友元类,两种具有不同的调用形式: A友元函数: 友元函数是可以直接访问类的私有成员的非成员函数。它是定义在类外的普通函数,它不属于任何类,但需要在类的定义中加以 声明,声明时只需在友元的名称前加上关键字friend,其格式如下: friend 类型 函数名(形式参数); 1.友元函数的声明可以放在类的私有部分,也可以放在公有部分,它们是没有区别的,都说明是该类的一个友元函数。 2.一个函数可以是多个类的友元函数,只需要在各个类中分别声明。友元函数的调用与一般函数的调用方式和原理一致。 B友元类