草庐IT

c++ - friend 功能在这里得到继承吗?

Derived类中的方法fun()是私有(private)的。当我们通过运行时多态调用函数ptr->fun()时,它正在执行。但这违反了派生类的封装属性。#includeusingnamespacestd;classDerived;classBase{private:virtualvoidfun(){coutfun();return0;}谁能解释一下发生了什么? 最佳答案 首先,你的Derived::fun()也是virtual,因为如果派生类中的函数与派生类中的虚函数具有相同的声明基类,派生类中的函数自动获得virtual,即使没

友元函数的 C++ 内联定义

在C++标准的当前草案中(2019年3月)[class.friend]p.6状态(强调我的):Afunctioncanbedefinedinafrienddeclarationofaclassifandonlyiftheclassisanon-localclass([class.local]),thefunctionnameisunqualified,andthefunctionhasnamespacescope.[...]“函数具有命名空间作用域”是什么意思?我能想到的函数没有命名空间作用域的唯一情况如下:structA{staticvoidf();structB{friendvoi

c++ - 与 friend 一起上课而不是前向声明,: which compiler is correct

这个问题在这里已经有了答案:Friendmethod"notdeclaredinthisscope"inC++(1个回答)Error:'FriendMemberFunctionName'wasnotdeclaredinthisscope(3个答案)关闭3年前。我有这个简单的C++程序:#includestructobj{friendintf(int);voidm(intx){std::cout如果我使用GNUC++编译器g++进行编译,我会得到错误prog.cpp:7:55:error:'f'wasnotdeclaredinthisscope但是,如果我使用cl(和/W4)编译它,它会

c++ - ADL 和 friend 注入(inject)

考虑这段代码:templatestructX{friendvoidf(X*){}};intmain(){f((X*)0);//Error?}编译器似乎非常不同意。(MSVC08/10说不是,GCC根据“C++模板-完整指南”:...itisassumedthatacallinvolvingalookupforfriendsinassociatedclassesactuallycausestheclasstobeinstantiated...AlthoughthiswasclearlyintendedbythosewhowrotetheC++standard,itisnotclearly

c++ - 友元函数和静态数据成员

如何在friend的函数体内对using名称执行非限定名称查找?让我们考虑以下代码:#includevoidfoo();classA{friendvoidfoo(){std::coutDEMON4296::7.3.1.2/3[namespace.memdef]中的标准声明:Ifafrienddeclarationinanon-localclassfirstdeclaresaclass,function,classtemplateorfunctiontemplatethefriendisamemberoftheinnermostenclosingnamespace.所以,我预计不合格的名

c++ - C++ 中的 friend 和模板

我的C++代码示例中有一个大问题。“friend”和"template"有问题。错误信息:Matrix.h:26:79:警告:frienddeclaration'std::ostream&matrixClass::operatorMatrix.h:26:79:注意:(ifthisisnotwhatyouintended,makesurethefunctiontemplatehasalreadybeendeclaredandaddafterthefunctionnamehere)Matrix.h:28:77:警告:frienddeclaration'matrixClass::Matrix

c++ - 限制多个模板参数友元函数可访问的类实例的范围

我想知道我的目标是否可行。我有一个这样的类#includetemplateclassClass;templateClassf(Class&C,constClass&D);templateclassClass{protected://thiscouldbeprivateTm_t;public:Class():m_t(T()){}Class(Tt):m_t(t){}T&getT(){returnm_t;}templatefriendClassf(Class&C,constClass&D);};templateClassf(Class&C,constClass&D){C.m_t+=D.m_t

c++ - operator << friend 函数和模板

这是我的代码:动画.h#includetemplateclassMovie{public:Movie(Tin){a=in;}friendstd::ostream&operator&movie);private:Ta;};templatestd::ostream&operator&movie){returnos;}主要.cpp#include"mov.h"intmain(){Moviemovie1(1);std::cout我尝试编译这段代码,但出现错误:Error1errorLNK2019:unresolvedexternalsymbol"classstd::basic_ostream>

c++ - 如何定义在两个类之外的模板类内部的非模板类中声明的友元函数?

我找到了“如何在其声明之外定义模板类的友元模板函数”(SO/cppreference),但如果我们在混合中添加另一个内部非模板类,该怎么做?即如何(外部)定义operator在classInternal中声明来自以下示例:#includetemplateclassExternal{public:explicitExternal(Tinitial):value{initial}{}classInternal{public:Internal(constExternal&e):internal_value{e.value}{}private:friendstd::ostream&operat

c++ - 如何与模板化类的构造函数成为 friend ?

为什么classA;templateclassB{private:A*a;public:B();};classA:publicB{private:friendB::B();intx;};templateB::B(){a=newA;a->x=5;}intmain(){return0;}结果../src/main.cpp:15:error:invaliduseofconstructorasatemplate../src/main.cpp:15:note:use‘B::B’insteadof‘B::classB’tonametheconstructorinaqualifiedname还在改变