带有常量构造函数参数的C++变量构造函数方法
C++ variable constructor method with constant constructor arguments
我正在尝试在类构造函数中使用函数指针,这样我就可以选择使用哪个函数来构造类的对象。我想这样做是为了能够改变如何用同一组构造函数参数确定类中成员变量的方法。我已经能够成功地对其进行编码,如下面的代码所示,但是我需要声明所有指向的单独函数为友元。
我的问题是:有什么方法可以将一个名称未知的函数(即只知道返回类型和一组参数)声明为友元吗?我想要这样做是因为在未来的开发中可能会添加新的函数,而类保持不变,我不想为每个新函数添加新的友元声明。
当然,我也对实现我的目标的其他方法持开放态度。
#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
相关文章:
- "error: no matching function for call to"构造函数错误
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 选择要调用的构造函数
- 如何委托派生类使用其父构造函数?
- 构造函数正在调用一个使用当前类类型的函数
- 没有用于初始化C++中的变量模板的匹配构造函数
- 初始化具有非默认构造函数的std::数组项的更好方法
- 当从函数参数中的临时值调用复制构造函数时
- 在c++构造函数中使用随机字符串生成器
- 一对向量构造函数:初始值设定项列表与显式构造
- 从构造函数抛出异常时如何克服内存泄漏
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 具有默认模板类型的默认构造函数的类型推导
- 使用dynamic_cast和构造函数时出错
- 在c++中使用向量时,如何调用构造函数和析构函数
- 奇怪的构造函数行为