草庐IT

weak-template-vtables

全部标签

C++专访: vtable for a class with a pure virtual function

我今天被问到这个面试问题!!(这是一个非常尴尬的电话采访..):Whatisthedifferencebetweenthevtableforaclasswithvirtualfunctionsandaclasswithpurevirtualfunctions?现在,我知道C++标准没有指定任何关于vtables的内容,甚至没有指定v-table的存在......但是从理论上讲,答案是什么?我脱口而出,具有纯虚函数的类可以有一个vtable,其纯虚函数的vtable条目将指向派生类的实现。这个假设正确吗?我没有得到面试官的肯定回答。假设的编译器会为只有纯虚函数的类创建一个vtable吗?

c++ - "missing template argument"是什么意思?

我对C++和这个站点还很陌生,所以肯定会出现错误。当我尝试编译我的代码时,我会收到类似error:missingtemplateargumentbefore'b'之类的错误。几个小时以来,我一直在世界各地寻找答案,结果把我带到了这里。我的任务是实现一个模板化的类Collection来存储一个集合使用数组的对象,沿与集合的当前大小。#include#include"collection.h"usingnamespacestd;vintmain(intargc,char*argv[]){collectionb;//#ifndefCOLLECTION_H#defineCOLLECTION_H

c++ bad_weak_ptr 错误

我想创建一些Timer类,它每N秒打印一次“文本”,其中N将在构造函数中初始化。#include#include#include#includeclassTimer:publicboost::enable_shared_from_this{public:Timer(constdoubleinterval):interval_sec(interval){io_service_=newboost::asio::io_service;timer_=newboost::asio::deadline_timer(*io_service_);start();io_service_->run();}

C++ OO设计: Inheritance of template parameter

我有一个以Base为基类的继承链。我希望能够编写一个继承Base和可能的另一个Base派生类的类模板。我可以使用虚拟继承,但我找到了另一种解决方案。我想知道它是否是常见的/可观的/合法的类设计:编写一个类模板,其中模板参数是它派生的类,即它必须是Base或Base派生类。在构造函数中,我可以使用静态断言来真正确保用户没有使用任何非法类作为模板参数。如果它有效,我将永远不会有虚拟继承问题......问题是,这样做是可以的。我在其他项目中从未见过它,所以我想在使用它之前先确定一下。编辑:为了确保我不会混淆你,这里有一些代码:classBase{};classDerived:publicBa

c++ - 标准是否认为模板类的非模板成员本身是 "templates"?

考虑以下代码:#includetemplatestructA{static_assert(!std::is_same_v);};templatestructB{voidfoo(){A{};}};intmain(){}它来自thisquestion在俄罗斯StackOverflow上,它会询问它是否有效。我试图引用这个:[temp.res]/8.18Thevalidityofatemplatemaybecheckedpriortoanyinstantiation.[ Note:Knowingwhichnamesaretypenamesallowsthesyntaxofeverytempl

c++ 缺少 vtable 错误

我收到一个非常奇怪的错误,与给定类构造函数和析构函数缺少vtable有关。请帮我解决这个问题。架构i386的undefinedsymbol:"vtableforA",referencedfrom:A::A()inA.oA::~MissionController()inA.oNOTE:amissingvtableusuallymeansthefirstnon-inlinevirtualmemberfunctionhasnodefinition.ld:symbol(s)notfoundforarchitecturei386clang:error:linkercommandfailedwit

c++ - C-回调函数模板 : explicitly instantiate template

前提我正在使用一个提供以下接口(interface)的C库(来自C++):voidregister_callback(void*f,void*data);voidinvoke_callback();问题现在,我需要将函数模板注册为回调,这给我带来了问题。考虑以下代码:templatevoidmy_callback(void*data){…}intmain(){intft=42;register_callback(reinterpret_cast(&my_callback),&ft);invoke_callback();}这给了我以下链接器错误(在OSX上使用g++(GCC)4.5.1但

c++ - 如何使用weak_ptr打破shared_ptr循环引用

我读过weak_pointers可以用来打破循环引用。考虑下面的循环引用示例structA{boost::shared_ptrshrd_ptr;};boost::shared_ptrptr_A(boost::make_shared());boost::shared_ptrptr_b(boost::make_shared());ptr_A->shrd_ptr=ptr_b;ptr_b->shrd_ptr=ptr_A;以上是循环引用的例子,我想知道如何破解上面使用weak_ptr的循环引用?更新:根据收到的建议,我提出了以下建议:structA{boost::weak_ptrwk_ptr;}

c++ - 哪个编译器是对的?需要模板化返回类型之前的 'template'?

Thissnippet(取自thisquestion)使用g++编译得很好(如图所示),只要template在返回类型之前存在。相比之下,VC10不编译该代码并出现以下错误:errorC2244:'A::getAttr':unabletomatchfunctiondefinitiontoanexistingdeclaration如果我删除template,VC10很高兴,但g++会报错:error:non-template'AttributeType'usedastemplatenote:use'A::templateAttributeType'toindicatethatitisat

c++ - shared_ptr & weak_ptr 转换

我正在尝试使用std::shared_ptr和std::weak_ptr来处理对象。场景是这样的:我有channel类的对象,它派生自抽象类abstract::channel(带有纯虚函数)。我有一个容器channelContainer(std::vector)包含到channel的共享指针(std::shared_ptr)对象。现在,我有一个deque(std::deque)包含指向channelContainer(std::weak_ptr)/。让我们将此双端队列命名为freeChannelQueue。可以这么说:std::vector>channelContainer;std::