草庐IT

c++ - 在当前类的构造函数中调用其他类定义的虚函数

classA{public:...virtualboolFunction(..)const{...}}classB:publicA{public:...virtualboolFunction(..)const{...}}classOtherClass{public:OtherClass(A&a){m_bool=a.Function(..);}private:boolm_bool;}假设类A或类B在构造类OtherClass之前完全初始化。问题>在OtherClass的构造函数中调用虚函数是否有问题? 最佳答案 没有,怎么会有问题呢?

python - 如何从 Python 调用类的 C++ 函数

这个问题在这里已经有了答案:CallingC/C++fromPython?[closed](12个答案)关闭8年前。我试过链接:CallingC/C++frompython?,但我不能这样做,这里我有声明外部“C”的问题。所以请建议假设我有一个名为“function.cpp”的函数,我必须在python代码中调用这个函数。function.cpp是:intmax(intnum1,intnum2){//localvariabledeclarationintresult;if(num1>num2)result=num1;elseresult=num2;returnresult;}然后我如何

c++ - std::current_exception 是否应该从类的析构函数中的 catch block 返回非空

我和我的同事认为我们在VisualC++2012和2013中发现了一个错误,但我们不确定。以下代码中对std::current_exception的调用是否应该返回一个非空的exception_ptr?似乎在我们尝试过的大多数其他编译器上:#include#include#includeclassA{public:~A(){try{throwstd::runtime_error("ohno");}catch(std::exception&){std::clog在VisualC++下运行时,我们得到“0”(假,这意味着返回的exception_ptr为空)。其他编译器,例如g++,打印“

c++ - 如何循环异构类的实例以调用具有相同名称和参数的方法?

如果我有:classA{voidfoo();};classB{voidfoo();};有没有办法收集A和B类型的实例来循环调用foo()方法?例如使用一些宏或指针和一些特殊的数据类型?还是收集函数指针?下面的伪代码可能会让您明白我的意思。intmain(){Aa;Bb;hypothetical_container_thypothetical_container;hypothetical_container.push_back(a);hypothetical_container.push_back(b);[...]//Dostuffwitha,dootherstuffwithbfor(h

c++ - 模板类的模板友元函数

我有以下模板类和模板函数,它们旨在访问类的私有(private)数据成员:#includetemplateclassMyVar{intx;};templatevoidprintVar(constMyVar&var){std::coutvoidscanVar(MyVar&var){std::cin>>var.x;}structFoo{};intmain(void){MyVara;scanVar(a);printVar(a);return0;}将这两个函数声明为MyVar的友元函数,我在templateclassMyVar的声明中尝试了以下方法宣告友元。它们都不起作用。我该怎么办?temp

c++ - 创建指向没有默认构造函数的类的智能指针数组

我指的是question,我们可以创建一个std::unique_ptr数组到一个已经删除了默认构造函数的类,如下所示,如何传递string参数。#include#include#includeusingnamespacestd;classA{stringstr;public:A()=delete;A(string_str):str(_str){}stringgetStr(){returnstr;}};intmain(){unique_ptrptr=make_unique(3);unique_ptrarr[3]=make_unique(3);//Dosomethinghereretur

c++ - 为什么不允许位字段作为类的静态数据成员

谁能解释不允许位域作为类的静态成员背后的原因?例如,定义如下的类:classA{public:A(){}~A(){}private:staticintmem:10;};intA::mem;不编译。用不同的编译器编译这个类:-1-g++抛出错误:-错误:静态成员'mem'不能是位域staticintmem:10;错误:‘intA::mem’不是‘classA’的静态数据成员intA::mem;2-clang抛出错误:-错误:静态成员'mem'不能是位域staticintmem:10;3-VisualStudio15抛出错误:-'A::mem'::非法存储类'intA::mem':不允许成

c++ - 是否有可能我有一个类的前向声明,而不是在头文件中使它们成为引用或指针

//Iprefertoperformforwarddeclarationonmyclass,asIdonot//wishtoship"myclass.h"toclient//However,thefollowingcodedoesn'tallowmetodoso,asclassdefination//isneededinheaderfile.////a.h#include"myclass.h"classa{public:a();myclassme;};我尝试换一种方式。但是,我需要使用动态分配,而我通常会尽量避免这种情况。//a.hclassmyclass;classa{public:

c++ - 专门化模板类的模板成员函数?

我有一个模板类,它有一个需要专门化的模板成员函数,如:templateclassX{public:templatevoidY(){}templatevoidY(){}};虽然VC正确处理了这个问题,但显然这不是标准的,GCC提示:explicitspecializationinnon-namespacescope'classX'我试过:templateclassX{public:templatevoidY(){}};template//Alsotried`template`herevoidX::Y(){}但这导致VC和GCC都提示。正确的做法是什么? 最佳答

c++ - 在 C++ 中重载类的运算符 []

我有一个类,我已经编写了它的[]运算符,我希望运算符有时返回一个int,有时返回一个struct。但是编译器不允许我重载运算符,为什么?它说:“...不能重载”代码:templatestructpart{};templateclassLinkedList{public:LinkedList():size(0),head(0){}T&operator[](constint&loc);part&operator[](constint&loc);};templateT&LinkedList::operator[](constint&loc){..alotofthingwhichcompile