带有常量构造函数参数的C++变量构造函数方法

C++ variable constructor method with constant constructor arguments

本文关键字:构造函数 C++ 变量 方法 参数 常量      更新时间:2023-10-16

我正在尝试在类构造函数中使用函数指针,这样我就可以选择使用哪个函数来构造类的对象。我想这样做是为了能够改变如何用同一组构造函数参数确定类中成员变量的方法。我已经能够成功地对其进行编码,如下面的代码所示,但是我需要声明所有指向的单独函数为友元。

我的问题是:有什么方法可以将一个名称未知的函数(即只知道返回类型和一组参数)声明为友元吗?我想要这样做是因为在未来的开发中可能会添加新的函数,而类保持不变,我不想为每个新函数添加新的友元声明。

当然,我也对实现我的目标的其他方法持开放态度。

#include <iostream>
class foo
{
private:
    int var_1;
public:
    foo(void (*functionPtr)(foo*))
    {
        functionPtr(this);
    }
    ~foo(){}
    int get_var() {return var_1;}
    friend void function_1(foo*); // declare all unique
    friend void function_2(foo*); // functions as friends
    /*friend void (*functionPtr)(foo*); // this is what I want:
                                        // to declare all functions with
                                        // a specific return type and
                                        // specific arguments as a friend
                                        // of this class */
};
void function_1(foo* T)
{
    std::cout << "function 1" << std::endl;
    T->var_1 = 1;
}
void function_2(foo* T)
{
    std::cout << "function 2" << std::endl;
    T->var_1 = 2;
}
int main()
{
    foo F1(&function_1);
    std::cout << F1.get_var() << std::endl;
    foo F2(&function_2);
    std::cout << F2.get_var() << std::endl;
    return 0;
}

您可以将要初始化的部件移动到一个单独的地方,在那里它们被视为公共的:

struct foovars
{
    int var_1;
};
class foo : foovars
{
public:
    foo(void (*functionPtr)(foovars*))
    {
        functionPtr(this);
    }
};
void function_1(foovars* T)
{
    std::cout << "function 1" << std::endl;
    T->var_1 = 1;
}

现在,从持有class foo实例的代码的角度来看,您的成员变量和以前一样私有。但是,管理接收foovars*的特殊函数可以修改其成员。

静态class函数在这里工作得更好:

class foo
{
private:
    int var_1;
public:
    foo(void (*functionPtr)(foo*))
    {
        functionPtr(this);
    }
    ~foo(){}
    int get_var() {return var_1;}
    static void function_1(foo*); // declare all unique
    static void function_2(foo*); // functions as friends
};
void foo::function_1(foo* T)
{
    std::cout << "function 1" << std::endl;
    T->var_1 = 1;
}
void foo::function_2(foo* T)
{
    std::cout << "function 2" << std::endl;
    T->var_1 = 2;
}

int main()
{
    foo F1(&foo::function_1);
    std::cout << F1.get_var() << std::endl;
    foo F2(&foo:function_2);
    std::cout << F2.get_var() << std::endl;
    return 0;
}

由于他们是类的成员,因此不需要friend,并且他们可以完全访问类的private成员。

更好的是,static函数可以是private,并且在类之外不可访问。

在这种情况下,使用标记调度。它(更)易于维护,并将产生更高效的代码。

#include <iostream>
class foo
{
private:
    int var_1;
public:
    struct type1_type {}; static constexpr type1_type type1{};
    struct type2_type {}; static constexpr type2_type type2{};
    foo(type1_type)
    : var_1(1)
    {
        // initialisation for type 1 construction
    }
    foo(type2_type)
    : var_1(2)
    {
        // initialisation for type 2 construction
    }
    ~foo(){}
    int get_var() {return var_1;}

};
int main()
{
    foo F1(foo::type1);
    std::cout << F1.get_var() << std::endl;
    foo F2(foo::type2);
    std::cout << F2.get_var() << std::endl;
    return 0;
}

预期结果:

1
2