草庐IT

成员方

全部标签

C++友元类和友元成员函数

我正在学习C++类中的友元函数、友元类和友元成员函数;现在,以下代码可以正常编译:#includeclassA{public:friendclassB;//friendvoidB::set(inti);//friendintB::get();friendintfunction(Aa);A(inti);voidset(inti);intget();private:inti;};A::A(inti):i(i){}voidA::set(inti){this->i=i;}intA::get(){returni;}classB{public:B(Aa);voidset(inti);intget(

c++ - 在类声明中初始化 const 成员变量时 Debug模式下的异常

#include#include#include#includeclassX{public:X(){std::cout>ValidatorType;constValidatorTypem_validators=ValidatorType{{"some-string",[](){//validationcodestd::cout以上代码使用Xcode7.2.1和Clang7.0.2在OSX上以调试和Release模式成功构建和运行。它还使用VisualStudioExpress2013forWindowsDesktop在Windows7上以Release模式成功构建和运行。但是,在Win

c++ - union 活跃成员背后的理由

C++的union比C的union更具限制性,因为它们引入了“事件成员”(最后分配给的成员)的概念,作为唯一可以安全访问的成员。在我看来,union的这种行为完全是负面的。有人可以解释一下这个限制有什么好处吗? 最佳答案 简答在C中,并集只是一个如何解释存储在给定位置的数据的问题。数据是被动的。在C++中,union可以拥有不同类的成员。而类对象不仅有数据,还有行为。由于您依赖于这种(可访问的)行为(甚至可能无法访问私有(private)成员和protected成员),因此必须确保对象从构造到销毁保持一致。事件成员的概念就是为了这个

c++ - 成员结构位域元素的初始化列表初始化导致 IAR ARM 中的错误

我在IAR中有以下类结构:classA{public:A(){}virtual~A(){};virtualvoidload(){};};classC{public:C(){//Cdoesotherstuff,notrelevant}};classD;classB:publicA{public:B():invert(false){};virtual~B(){};voidload(){//Irrelevantstuffdonehere}private:Cmember_c;std::vectorvector_of_d;struct{boolvar_1:1;boolvar_2:1;boolva

属于模板类的成员模板的 C++ 显式特化

在目前的C++标准草案中,thisparagraph中就有这个例子属于与模板的显式特化相关的部分:templatestructA{voidf(T);templatevoidg1(T,X1);templatevoidg2(T,X2);voidh(T){}};//specializationtemplatevoidA::f(int);//outofclassmembertemplatedefinitiontemplatetemplatevoidA::g1(T,X1){}//membertemplatespecializationtemplatetemplatevoidA::g1(int,X

c++ - 共享库中静态函数成员的销毁顺序

我目前正在探索Boost.Serialization中与单例相关的一个非常棘手的错误。对于上下文:Boost1.65更改了单例的实现,打破了is_destructed导致程序退出或库卸载时出现段错误的通知。Boost1.66“修复”了这个问题,但会泄漏内存。单例代码(与这个问题相关)归结为:templatestructsingleton{T&inst(){staticTt;returnt;}}使用静态成员函数变量可以避免staticinitfiasco但在破坏方面仍然存在同样的问题。但是FindingC++staticinitializationorderproblems显示代码如何解

c++ - 向下转换指向成员函数的指针。这是合法的用法吗?

我将指向成员函数的指针列表存储在一个数组中。我想索引到数组中并执行适当的函数。将有许多数组列出来自不同类(全部派生自Base)的函数,因此在编译时不知道该类。我的方案有效,但我对不得不在一个地方使用void指针并不完全满意,但我似乎无法避免它。根据C++11标准(它使用g++),我在Base和Derived成员函数指针之间的转换是否合法。我将不胜感激语言律师的建议!下面是我的代码的一个精简但可运行的版本。#includeusingstd::cout;//*************************************classBase{public:typedefint(Ba

c++ - 传入指定类的成员变量

我想传入一个成员变量的名字。我以为我可以做到这一点templatevoidSetVal(T::*newval){};这显然行不通,但希望能理解我正在尝试做的事情。我希望能够设置模板类的某个成员变量。 最佳答案 您始终可以将编译定义的常量作为模板参数。所以这里是:templateR&SetVal(T&t,constR&value){t.*member=value;returnt.*member;}structA{inta;};intmain(){Aa;SetVal(a,10);return0;}

c++ - 为什么以下 SFINAE 测试无法检测到模板成员函数?

使用GCC编译时,我从以下代码中得到的结果总是错误的。我相信这是一个编译器错误,但有人可能知道得更多。#includetemplateclasshas_apply{typedefcharyes[1];typedefcharno[2];templatestructbinder{};templatestaticyes&test(U*,binder>*=0);templatestaticno&test(...);public:staticconstboolresult=(sizeof(yes)==sizeof(test((T*)(0))));};classA{public:templatev

c++ - "expected ' : ', ' , ', ' ; ', ' } ' or ' __attribute__ ' before ' { 结构成员函数中的' token"

我正在尝试编译我教授设计过度的C++代码。这是我的代码:/***Vectorclass.*CommonmathematicaloperationsonvectorsinR3.**WrittenbyRobertOsada,March1999.**/#ifndef__VECTOR_H__#define__VECTOR_H__/***Vector3**/structVector3f{//coordinatesfloatx,y,z;//normfloatnormSquared(){returnx*x+y*y+z*z;}doublenorm(){returnsqrt(normSquared())