virtual

Can we make a class copy constructor virtual in C++

落花浮王杯 提交于 2019-12-18 03:03:08
问题 Can we make a class copy constructor virtual in C++? How to use? 回答1: No you can't, constructors can't be virtual. C++03 - 12.1 Constructors 4) A constructor shall not be virtual (10.3) or static (9.4). [...] If you need something like this, you can look up the virtual constructor idiom here. 回答2: No you cannot. Furthermore, the whole concept does not make sense. Virtual functions are functions that are dispatched based on the value of an object (the dynamic type of the object). When a

使用CMake来构建STM32工程

你。 提交于 2019-12-17 22:20:06
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> CMake 是一个跨平台的自动化建构系统,它使用一个名为 CMakeLists.txt 的文件来描述构建过程, 可以产生标准的构建文件,如 Unix 的 Makefile 或Windows Visual Studio 的 projects/workspaces 。 使用CMake来构建STM32工程,生成不同的构建文件, 如生成Visual Studio或Eclipse的工程文件,再配置好编译环境,我们就可以根据个人喜好选择IDE来开发STM32项目了。 配置toolchain 首先创建/opt/stm32-toolchain目录,所需的工具资源都会放在该目录下。 安装GCC ARM Embedded 配置toolchain的第一步是安装ARM Cortex-M和Cortex-R对应的GCC,包括预编译器、编译器和链接器等一系列工具。 最新版的GCC ARM可以从 launchpad 下载,这里使用的是gcc-arm-none-eabi-4_9-2015q3-20150921-linux.tar.bz2 下载后解压在/opt/stm32-toolchain/gcc-arm目录下。 安装STM32Cube STM32Cube是一个全面的软件平台,包括了ST产品的每个系列。(STM32CubeF4

C++ member function virtual override and overload at the same time

孤街醉人 提交于 2019-12-17 21:55:59
问题 If I have a code like this: struct A { virtual void f(int) {} virtual void f(void*) {} }; struct B : public A { void f(int) {} }; struct C : public B { void f(void*) {} }; int main() { C c; c.f(1); return 0; } I get an error that says that I am trying to do an invalid conversion from int to void*. Why can't compiler figure out that he has to call B::f, since both functions are declared as virtual? After reading jalf's answer I went and reduced it even further. This one does not work as well.

May I call a virtual function to initialize a base-class sub-object?

血红的双手。 提交于 2019-12-17 21:09:14
问题 I know that virtual functions should not be called either directly or indirectly in a constructor, but this code runs fine. Is what I have here safe? #include <iostream> #include <string> struct A { A (const std::string& name) {std::cout << name << std::endl;} virtual std::string tag() const = 0; }; struct B: A { B() : A (tag()) {} virtual std::string tag() const override {return "B";} }; int main() { B b; // Output gives "B\n" } If not, would the following (based on a comment) be a correct

C#: What are virtual events and how can they be used?

人盡茶涼 提交于 2019-12-17 21:01:55
问题 How does a virtual event work? How would you override it? How would that work? And in what cases would you do that? Would it for example be an ok replacement for protected OnEvent methods? So that inheriting classes could just override the event and raise it directly? Or would that be wrong or just not work? The MSDN says this about it: An event can be marked as a virtual event by using the virtual keyword. This enables derived classes to override the event behavior by using the override

need a virtual template member workaround

旧城冷巷雨未停 提交于 2019-12-17 18:18:44
问题 I need to write a program implementing the visitor design pattern. The problem is that the base visitor class is a template class. This means that BaseVisited::accept() takes a template class as a parameter and since it uses 'this' and i need 'this' to point to the correct runtime instance of the object, it also needs to be virtual. I'd like to know if there's any way around this problem. template <typename T> class BaseVisitor { public: BaseVisitor(); T visit(BaseVisited *visited); virtual

Hoisting the dynamic type out of a loop (a.k.a. doing Java the C++ way)

此生再无相见时 提交于 2019-12-17 16:27:29
问题 I was discussing the merits of "modern" languages compared to C++ with some friends recently, when the following came up (I think inspired by Java): Does any C++ compiler optimize dynamic dispatch out of a loop? If not, is there any kind of construction that would allow the author to force (or strongly encourage) such an optimization? Here's the example. Suppose I have a polymorphic hierarchy: struct A { virtual int f() { return 0; } }; struct B : A { virtual int f() { return /* something

In C++, is a function automatically virtual if it overrides a virtual function?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 16:05:15
问题 I would expect that if foo is declared in class D , but not marked virtual, then the following code would call the implementation of foo in D (regardless of the dynamic type of d ). D& d = ...; d.foo(); However, in the following program, that is not the case. Can anyone explain this? Is a method automatically virtual if it overrides a virtual function? #include <iostream> using namespace std; class C { public: virtual void foo() { cout << "C" << endl; } }; class D : public C { public: void

translate virtual address to physical address

拜拜、爱过 提交于 2019-12-17 15:36:58
问题 The following page table is for a system with 16-bit virtual and physical addresses and with 4,096-byte pages. The reference bit is set to 1 when the page has been referenced. Periodically, a thread zeroes out all values of the reference bit.All numbers are provided in decimal. I want to convert the following virtual addresses (in hexadecimal) to the equivalent physical addresses. Also I want to set the reference bit for the appropriate entry in the page table. • 0xE12C • 0x3A9D • 0xA9D9 •

c++ overloaded virtual function warning by clang?

半腔热情 提交于 2019-12-17 15:35:26
问题 clang emits a warning when compiling the following code: struct Base { virtual void * get(char* e); // virtual void * get(char* e, int index); }; struct Derived: public Base { virtual void * get(char* e, int index); }; The warning is: warning: 'Derived::get' hides overloaded virtual function [-Woverloaded-virtual] (the said warning needs to be enabled of course). I don't understand why. Note that uncommenting the same declaration in Base shuts the warning up. My understanding is that since