我有一个C结构具有相关功能:性能C结构对C++结构/
struct StructC {
int a;
int b;
};
static inline StructC func1()
{
StructC ret;
//do something with variables ret.a and ret.b
return ret;
}
static inline void func2(StructC var)
{
//do something with variables var.a and var.b
...
}
和C++结构:
struct StructCPP {
int a;
int b;
StructCPP func3() //can also be static
{
StructCPP ret;
//do something with instance variables ret.a and ret.b
return ret;
}
void func4() //can't use static here since it operates on the instance variables
{
//do something with instance variables a and b
...
}
};
我的问题:通过这些结构来时,这是更快函数/方法?
由于在StructC上运行的C函数是静态的,所以只有一个实例驻留在内存中,但其结构中的C++方法会发生什么?它的方法(func3()和func4())占用每个实例的冗余内存,还是C++编译器优化它只保存一个实例,所以在传递C++结构时,只有实例变量a和b,通过了吗?
该函数调用这些函数更快(如果有任何差异)?:
void functionA(StructC var); //StructC as argument
void functionB(StructCPP var); //StructCPP as argument
(该程序是C和C++的混合物)
如果我没有弄错,我相信结构被解释为C++中的类。 – turnt
您传递_objects_,而不是_structs_。与结构关联的方法不会被复制。如果这就是你要求的。 – stefan
关于表现,在你问之前总是测试。你有没有尝试看看这里有什么区别?另外,我认为你对结构的理解基本上是不正确的。 –