具有常量构造函数参数的 C++ 变量构造函数方法
C++ variable constructor method with constant constructor arguments
我正在尝试在 class 构造函数中使用函数指针,以便我可以 select 哪个函数用于构造 class 的对象。我想这样做是为了能够更改 class 中的成员变量如何使用同一组构造函数参数确定的方法。我已经能够成功编写代码,如下面的代码所示,但是我需要声明所有指向友元的单独函数。
我的问题:有没有办法将名称未知的函数(即只有 return 类型和一组参数已知)声明为友元?我想要这个是因为在未来的开发中可能会添加新的功能,而 class 保持不变,我不想为每个新功能添加新的朋友声明。
当然,我也愿意接受其他方法来实现我的目标。
#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;
}
您可以将要初始化的部分移动到一个单独的地方,在那里它们被视为 public:
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;
}
因为他们是 class 的成员,所以不需要 friend
,并且他们可以完全访问 class 的 private
成员。
更好的是,static
函数可以 private
,并且在 class 之外无法访问。
在这种情况下,使用代码调度。它(更)更易于维护,并将产生更高效的代码。
#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
我正在尝试在 class 构造函数中使用函数指针,以便我可以 select 哪个函数用于构造 class 的对象。我想这样做是为了能够更改 class 中的成员变量如何使用同一组构造函数参数确定的方法。我已经能够成功编写代码,如下面的代码所示,但是我需要声明所有指向友元的单独函数。
我的问题:有没有办法将名称未知的函数(即只有 return 类型和一组参数已知)声明为友元?我想要这个是因为在未来的开发中可能会添加新的功能,而 class 保持不变,我不想为每个新功能添加新的朋友声明。
当然,我也愿意接受其他方法来实现我的目标。
#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;
}
您可以将要初始化的部分移动到一个单独的地方,在那里它们被视为 public:
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;
}
因为他们是 class 的成员,所以不需要 friend
,并且他们可以完全访问 class 的 private
成员。
更好的是,static
函数可以 private
,并且在 class 之外无法访问。
在这种情况下,使用代码调度。它(更)更易于维护,并将产生更高效的代码。
#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