小编Rai*_*op7的帖子

将动态参数转发给构造函数

这是一个跟进问题: std :: initializer_list as std :: array constructor

我从那里得到的答案中学到了一点退出并尝试扩展我的包装类.简而言之,这个问题,我的解决方案以及它的问题.

问题:我需要一个从模板创建数组的类,没有默认构造函数和未知数量的参数.它本身可以使用可变参数模板包装在其他类中.

我的解决方案:工作但不是真正的用户友好因此我希望有一些想法来改进它 运行示例

class a   // some sort of storage class that need wrapper for more functions
{
public:     
   a(int c)
    {
        std::cout << "Class A: " << c << std::endl;
    }
};

template<class TStack>
class b : public TStack  // wrapper with char input
{
public:
    template<class ... TArgs>
    b(char c, TArgs&& ... args)
        : TStack( std::forward<TArgs>(args)...)
    {
        std::cout << "Class B: " << c << std::endl; …
Run Code Online (Sandbox Code Playgroud)

c++ wrapper variadic-templates c++14

5
推荐指数
0
解决办法
164
查看次数

根据函数名称数组的索引调用函数

在游戏中进行随机动作使它看起来真的像真实的...所以如果一个角色有很多capabilitiesmove, work, study... 所以在编程中根据某些条件调用这些函数。我们想要的是一个更加随机和真实的动作,其中没有任何条件,但根据随机条件,角色会采取随机动作。

我想在数组中创建动作(函数),然后声明一个指向函数的指针,程序可以随机生成一个索引,在该索引上,指向函数的指针将被分配数组中相应的函数名称:

#include <iostream>

void Foo()   { std::cout << "Foo"    << std::endl; }
void Bar()   { std::cout << "Bar"    << std::endl; }
void FooBar(){ std::cout << "FooBar" << std::endl; }
void Baz()   { std::cout << "Baz"    << std::endl; }
void FooBaz(){ std::cout << "FooBaz" << std::endl; }


int main()
{

    void (*pFunc)();
    void* pvArray[5] = {(void*)Foo, (void*)Bar, (void*)FooBar, (void*)Baz, (void*)FooBaz};

    int choice;
    std::cout << "Which function: ";
    std::cin >> choice;
    std::cout …
Run Code Online (Sandbox Code Playgroud)

c++ arrays function-pointers function

0
推荐指数
1
解决办法
633
查看次数

为什么较新版本的C++不允许在类声明中定义纯方法?

为什么MSVC++ 2015及其早期版本允许在pure virtual method类声明中定义,但On GCC 4.9和我想MSVC++ 2017不允许:

    #include <iostream>     

    class A{
        public:
            virtual void Foo() = 0;
    };

    class B: public A {
        public:
            virtual void Foo() = 0 { std::cout << "B::Foo()" << std::endl;
            }; // Allowed on MSVC 2015 and old versions

            //virtual void Foo() = 0; // on newer versions
    };

    //void B::Foo(){
    //  std::cout << "B::Foo()" << std::endl;
    //}     // Ok here!

    class C : public B{
        public:
            void Foo(){
                B::Foo(); …
Run Code Online (Sandbox Code Playgroud)

c++ oop chain

0
推荐指数
1
解决办法
82
查看次数