polymorphic virtual base class?

Is there a way to downcast from a virtual base class to a derived class when there are no virtual functions involved? Here's some code to demonstrate what I'm talking about: struct Base1 { int data; }; struct Base2 { char odd_size[9]; }; struct ViBase { double value; }; struct MostDerived : Base1, Base2, virtual ViBase { bool ok; }; void foo(ViBase &v) { MostDerived

多态虚拟基类?

当没有涉及虚拟功能时,是否有办法从虚拟基类向派生类下降? 下面是一些代码来演示我在说什么: struct Base1 { int data; }; struct Base2 { char odd_size[9]; }; struct ViBase { double value; }; struct MostDerived : Base1, Base2, virtual ViBase { bool ok; }; void foo(ViBase &v) { MostDerived &md = somehow_cast<MostDerived&>(v); //but HOW? md.ok = true; } int main(

Access to protected constructor of base class

A derived class can call a protected base class constructor in its ctor-initializer, but only for its own base class subobject, and not elsewhere: class Base { protected: Base() {} }; class Derived : Base { Base b; public: Derived(): Base(), // OK b() { // error Base b2; // error } }; What does the standard say about this? Here is [cla

访问受保护的基类的构造函数

派生类可以在其ctor-initializer中调用受保护的基类构造函数,但仅针对其自己的基类子对象,并不在其他地方: class Base { protected: Base() {} }; class Derived : Base { Base b; public: Derived(): Base(), // OK b() { // error Base b2; // error } }; 标准对此有何评论? 这是[class.protected] / 1: 如果非静态数据成员或非静态成员函数是其命名类

Acessing to a base subobject and virtual specifier c++

This question already has an answer here: In C++, what is a virtual base class? 11 answers An object of class struct C : A, B {}; contains two base subobjects, one of type A and one of type B . You can access them "directly": void foo(A &); void bar(B &); C c; foo(c); // accesses the A-subobject bar(c); // accesses the B-subobject You can also say static_cast<A&

访问基础子对象和虚拟说明符c ++

这个问题在这里已经有了答案: 在C ++中,什么是虚拟基类? 11个答案 类struct C : A, B {};一个对象struct C : A, B {}; 包含两个基本子对象,一个是A类型,另一个是B类型。 你可以直接访问它们: void foo(A &); void bar(B &); C c; foo(c); // accesses the A-subobject bar(c); // accesses the B-subobject 你也可以明确地说static_cast<A&>(c)和static_cast<B&>(c) ,尽管这通

Inheriting constructors and virtual base classes

I'm about to create an exception class hierarchy which conceptually looks somewhat like this: #include <iostream> #include <stdexcept> class ExceptionBase : public std::runtime_error { public: ExceptionBase( const char * msg ) : std::runtime_error(msg) {} }; class OperationFailure : virtual public ExceptionBase { public: using ExceptionBase::ExceptionBase; }; class F

继承构造函数和虚拟基类

我即将创建一个异常类层次结构,其概念上看起来有点像这样: #include <iostream> #include <stdexcept> class ExceptionBase : public std::runtime_error { public: ExceptionBase( const char * msg ) : std::runtime_error(msg) {} }; class OperationFailure : virtual public ExceptionBase { public: using ExceptionBase::ExceptionBase; }; class FileDoesNotExistError : virtual public Exc

Multiple instances of a virtual base class subobject (really)

Given the code: #include <cassert> struct X {}; struct Y1: virtual X {}; struct Y2: virtual X {}; struct Y3: virtual X {}; struct Y4: virtual X {}; struct Z1: Y1, Y2 {}; struct Z2: Y3, Y4 {}; struct XYZ: Z1, Z2 {}; int main() { XYZ xyz; X *x1 = static_cast<Y1*>(&xyz); X *x2 = static_cast<Y2*>(&xyz); X *x3 = static_cast<Y3*>(&xyz); X

虚拟基类子对象的多个实例(真的)

给定代码: #include <cassert> struct X {}; struct Y1: virtual X {}; struct Y2: virtual X {}; struct Y3: virtual X {}; struct Y4: virtual X {}; struct Z1: Y1, Y2 {}; struct Z2: Y3, Y4 {}; struct XYZ: Z1, Z2 {}; int main() { XYZ xyz; X *x1 = static_cast<Y1*>(&xyz); X *x2 = static_cast<Y2*>(&xyz); X *x3 = static_cast<Y3*>(&xyz); X *x4

In C++, what is a virtual base class?

I want to know what a "virtual base class" is and what it means. Let me show an example: class Foo { public: void DoSomething() { /* ... */ } }; class Bar : public virtual Foo { public: void DoSpecific() { /* ... */ } }; Virtual base classes, used in virtual inheritance, is a way of preventing multiple "instances" of a given class appearing in an inheritance hiera

在C ++中,什么是虚拟基类?

我想知道“虚拟基类”是什么以及它的意思。 让我举个例子: class Foo { public: void DoSomething() { /* ... */ } }; class Bar : public virtual Foo { public: void DoSpecific() { /* ... */ } }; 在虚拟继承中使用的虚拟基类是在使用多重继承时防止给定类的多个“实例”出现在继承层次结构中的一种方法。 考虑以下情况: class A { public: void Foo() {} }; class B : public A {}; class C : public A {}; cl

thread is determined during compile or runtime?

i just ask my self:when i make pool of threads in code then i compile the code , does the compiled code have a copy for each thread? and if i use macro function , and pass it to the threads, is this macro expanded during compile time"what i think" or during runtime, and if it is in compile time why this following code need mutex: #include <boost/asio.hpp> #include <b

线程在编译或运行时确定?

我只问自己:当我在代码中创建线程池时 然后我编译代码, 编译后的代码是否有每个线程的副本? 如果我使用宏函数,并将其传递给线程, 在编译期间“我在想什么”或在运行时期间扩展了这个宏, 如果它在编译时为什么下面的代码需要互斥体: #include <boost/asio.hpp> #include <boost/thread.hpp> #include <boost/date_time.hpp> #include <iostream> namespace asio = boost::asio; #define P

std::thread causing application to abort with error R6010

I have a class named Task which internally holds a member std::thread. The general idea is to create a thread that stays alive as requests of processing come. class Task { public: Task(); ~Task(); void start(); // some funny stuff here protected: Task(const Task& ref); void main_function(); std::thread m_thread; // more funny stuff like queues, mutexes, etc

std :: thread导致应用程序中止错误R6010

我有一个名为Task的类,它内部拥有一个成员std :: thread。 总体思路是创建一个线程,随着处理请求的到来而保持活动状态。 class Task { public: Task(); ~Task(); void start(); // some funny stuff here protected: Task(const Task& ref); void main_function(); std::thread m_thread; // more funny stuff like queues, mutexes, etc } 在函数start()中我做了: void Task::st

How do I declare a bool function in a window.h thread?

For my assignment I need to write a multithreaded program that outputs all the prime numbers less than or equal to the number entered by the user, in a separate thread. I am new to the threads and do not fully understand how to correctly implement them. But I created a boolean function which determines whether an integer is a prime number or not and I named it bool isPrime. However, the bool

我如何在window.h线程中声明一个bool函数?

对于我的任务,我需要编写一个多线程程序,在单独的线程中输出小于或等于用户输入的数字的所有素数。 我是新来的线程,并没有完全理解如何正确地实现它们。 但是我创建了一个布尔函数来确定一个整数是否是一个素数,我将它命名为bool isPrime。 然而,布尔isPrime给我以下错误“声明是不符合”LPVOID isPrime“。我不知道如何解决它,我也注意到,我的编译器(visual c + +)给出了”cout“的红色下划线和“cin”,但如果我声明使用

creating a thread from a function object class

I would like to create active objects that start a new thread (executing their function application operator) when they're instantiated. template <typename R, typename... Ts> // VARIADIC TEMPLATE class active_object{ protected: std::thread* t; R& result; public: active_object(R init, Ts... args) : t{nullptr}, result{init} { start_thread(args...); } void start

从函数对象类创建一个线程

我想创建活动对象,当它们被实例化时启动一个新线程(执行它们的函数应用程序运算符)。 template <typename R, typename... Ts> // VARIADIC TEMPLATE class active_object{ protected: std::thread* t; R& result; public: active_object(R init, Ts... args) : t{nullptr}, result{init} { start_thread(args...); } void start_thread(Ts... args){ t = new std::thread( *this, args.