草庐IT

instance-methods

全部标签

C++14 类型列表,有什么理由更喜欢 'free functions' 到 'methods',反之亦然?

我看到了在C++11/14中实现类型列表的两种可能的风格,我很好奇是否有任何理由更喜欢其中一种。第一种技术是outlinedhere并在Boost的MPL库上建模。在这种风格中,您定义了接收类型列表并对其进行操作的元“自由函数”(使用声明的顶层)。以下是您将如何实现std::transform的元版本,该版本适用于类型而不是第一种样式中的值:templatestructtype_list;namespaceimpl{templateclassF,classL>structtransform_impl;templateclassF,templateclassL,typename...T>

C++14 类型列表,有什么理由更喜欢 'free functions' 到 'methods',反之亦然?

我看到了在C++11/14中实现类型列表的两种可能的风格,我很好奇是否有任何理由更喜欢其中一种。第一种技术是outlinedhere并在Boost的MPL库上建模。在这种风格中,您定义了接收类型列表并对其进行操作的元“自由函数”(使用声明的顶层)。以下是您将如何实现std::transform的元版本,该版本适用于类型而不是第一种样式中的值:templatestructtype_list;namespaceimpl{templateclassF,classL>structtransform_impl;templateclassF,templateclassL,typename...T>

c# - 困惑: instance creation of c# class in c++

假设someClass是一个用C#定义的类,带有一些方法intdoSomething(void),并且为简单起见,提供一个不带参数的构造函数。然后,在C#中,必须在gc堆上创建实例:someClassc;//legit,butonlyanullpointerinC#//c->doSomething()//wouldnotevencompile.c=newsomeClass();//nowitpointstoaninstanceofsomeclass.inti=c->doSomething();现在,如果someClass被编译成一些.Net库,你也可以在C++/CLI中使用它:some

c# - 困惑: instance creation of c# class in c++

假设someClass是一个用C#定义的类,带有一些方法intdoSomething(void),并且为简单起见,提供一个不带参数的构造函数。然后,在C#中,必须在gc堆上创建实例:someClassc;//legit,butonlyanullpointerinC#//c->doSomething()//wouldnotevencompile.c=newsomeClass();//nowitpointstoaninstanceofsomeclass.inti=c->doSomething();现在,如果someClass被编译成一些.Net库,你也可以在C++/CLI中使用它:some

c++ - 错误 "C++ requires a type specifier for all declarations whilst defining methods"

我对C++比较陌生(所以请尽量保持简单的答案!),我不明白为什么会出现错误:C++需要所有声明的类型说明符同时定义方法。我正在尝试编写一个简单的程序来逐行读取文本文件,将值存储到数组中。但是,当我尝试在.cpp文件中声明方法时遇到问题。请在下面找到代码。StringList.h#ifndefStringListH#defineStringListH#include#includeclassStringList{public:StringList();~StringList();voidPrintWords();private:size_tnumberOfLines;std::vecto

c++ - 错误 "C++ requires a type specifier for all declarations whilst defining methods"

我对C++比较陌生(所以请尽量保持简单的答案!),我不明白为什么会出现错误:C++需要所有声明的类型说明符同时定义方法。我正在尝试编写一个简单的程序来逐行读取文本文件,将值存储到数组中。但是,当我尝试在.cpp文件中声明方法时遇到问题。请在下面找到代码。StringList.h#ifndefStringListH#defineStringListH#include#includeclassStringList{public:StringList();~StringList();voidPrintWords();private:size_tnumberOfLines;std::vecto

c++ - 常见问题解答 : Why does dynamic_cast only work if a class has at least 1 virtual method?

这在C++中无法编译:classA{};classB:publicA{};...A*a=newB();B*b=dynamic_cast(a); 最佳答案 因为dynamic_cast只能向下转换多态类型,所以标准这么说。您可以通过向基类添加virtual析构函数来使您的类具有多态性。事实上,你可能应该无论如何(见脚注)。否则,如果您尝试通过A指针删除B对象,您将调用UndefinedBehavior.classA{public:virtual~A(){};};瞧!脚注关于在多态类型中需要虚拟析构函数的“规则”有一些异常(except

c++ - 常见问题解答 : Why does dynamic_cast only work if a class has at least 1 virtual method?

这在C++中无法编译:classA{};classB:publicA{};...A*a=newB();B*b=dynamic_cast(a); 最佳答案 因为dynamic_cast只能向下转换多态类型,所以标准这么说。您可以通过向基类添加virtual析构函数来使您的类具有多态性。事实上,你可能应该无论如何(见脚注)。否则,如果您尝试通过A指针删除B对象,您将调用UndefinedBehavior.classA{public:virtual~A(){};};瞧!脚注关于在多态类型中需要虚拟析构函数的“规则”有一些异常(except

c++ - 如何解决 "pure virtual method called"

我明白为什么会发生这种情况,但我一直在尝试解决它...这是我的代码在我的程序退出时生成错误(因此导致崩溃)时所做的事情...purevirtualmethodcalledSomeClass::~SomeClass(){BaseClassObject->SomePureVirtualMethod(this);}voidDerivedClass::SomePureVirtualMethod(SomeClass*obj){//Dostufftoremoveobjfromacollection}我从来没有给newSomeClass打过电话但我有一个QList我附加了SomeClass*反对。S

c++ - 如何解决 "pure virtual method called"

我明白为什么会发生这种情况,但我一直在尝试解决它...这是我的代码在我的程序退出时生成错误(因此导致崩溃)时所做的事情...purevirtualmethodcalledSomeClass::~SomeClass(){BaseClassObject->SomePureVirtualMethod(this);}voidDerivedClass::SomePureVirtualMethod(SomeClass*obj){//Dostufftoremoveobjfromacollection}我从来没有给newSomeClass打过电话但我有一个QList我附加了SomeClass*反对。S