C++ C++;比较两种结构以说明其区别
我有两个这样的结构C++ C++;比较两种结构以说明其区别,c++,C++,我有两个这样的结构 struct Activity { int id; string Description; int parameter1; int parameter2; int parameter3; int parameter4; etc... } Activity A1; A1.id=0; A1.parameter1=50; Activity A2; A2.id=0; A2.parameter1=55; struct Activity { int id; string D
struct Activity {
int id;
string Description;
int parameter1;
int parameter2;
int parameter3;
int parameter4;
etc...
}
Activity A1;
A1.id=0;
A1.parameter1=50;
Activity A2;
A2.id=0;
A2.parameter1=55;
struct Activity {
int id;
string Description;
int parameter1;
int parameter2;
int parameter3;
int parameter4;
etc...
public:
bool compare(const Activity& param)
{
//...compare structs here like:
if (id != param.id)
//write something
//etc...
//at the end you can also return bool that indicates that structs are equal or not
return true;
}
}
我想比较一下,看看大家有什么不同?
在这种情况下,类似于:
paameter1是不同的
感谢实现这一点的最佳解决方案可能是在结构内部编写public方法,该方法将自身与通过参数传递的结构进行比较 这个看起来像这样
struct Activity {
int id;
string Description;
int parameter1;
int parameter2;
int parameter3;
int parameter4;
etc...
}
Activity A1;
A1.id=0;
A1.parameter1=50;
Activity A2;
A2.id=0;
A2.parameter1=55;
struct Activity {
int id;
string Description;
int parameter1;
int parameter2;
int parameter3;
int parameter4;
etc...
public:
bool compare(const Activity& param)
{
//...compare structs here like:
if (id != param.id)
//write something
//etc...
//at the end you can also return bool that indicates that structs are equal or not
return true;
}
}
这显然只适用于两个相同的类,除非您编写更多的比较方法,但是比较两个不同的结构可能很困难
还有其他方法可以比较两个变量(包括结构)。为此,可以使用memcmp()函数,但它不会直接告诉您哪些字段不同
根据@Tony_D所说的内容进行编辑。最好的解决方案可能是在结构内部编写public方法,该方法将自身与通过参数传递的结构进行比较 这个看起来像这样
struct Activity {
int id;
string Description;
int parameter1;
int parameter2;
int parameter3;
int parameter4;
etc...
}
Activity A1;
A1.id=0;
A1.parameter1=50;
Activity A2;
A2.id=0;
A2.parameter1=55;
struct Activity {
int id;
string Description;
int parameter1;
int parameter2;
int parameter3;
int parameter4;
etc...
public:
bool compare(const Activity& param)
{
//...compare structs here like:
if (id != param.id)
//write something
//etc...
//at the end you can also return bool that indicates that structs are equal or not
return true;
}
}
这显然只适用于两个相同的类,除非您编写更多的比较方法,但是比较两个不同的结构可能很困难
还有其他方法可以比较两个变量(包括结构)。为此,可以使用memcmp()函数,但它不会直接告诉您哪些字段不同
根据@Tony_D所说的内容进行编辑。最好的解决方案可能是在结构内部编写public方法,该方法将自身与通过参数传递的结构进行比较 这个看起来像这样
struct Activity {
int id;
string Description;
int parameter1;
int parameter2;
int parameter3;
int parameter4;
etc...
}
Activity A1;
A1.id=0;
A1.parameter1=50;
Activity A2;
A2.id=0;
A2.parameter1=55;
struct Activity {
int id;
string Description;
int parameter1;
int parameter2;
int parameter3;
int parameter4;
etc...
public:
bool compare(const Activity& param)
{
//...compare structs here like:
if (id != param.id)
//write something
//etc...
//at the end you can also return bool that indicates that structs are equal or not
return true;
}
}
这显然只适用于两个相同的类,除非您编写更多的比较方法,但是比较两个不同的结构可能很困难
还有其他方法可以比较两个变量(包括结构)。为此,可以使用memcmp()函数,但它不会直接告诉您哪些字段不同
根据@Tony_D所说的内容进行编辑。最好的解决方案可能是在结构内部编写public方法,该方法将自身与通过参数传递的结构进行比较 这个看起来像这样
struct Activity {
int id;
string Description;
int parameter1;
int parameter2;
int parameter3;
int parameter4;
etc...
}
Activity A1;
A1.id=0;
A1.parameter1=50;
Activity A2;
A2.id=0;
A2.parameter1=55;
struct Activity {
int id;
string Description;
int parameter1;
int parameter2;
int parameter3;
int parameter4;
etc...
public:
bool compare(const Activity& param)
{
//...compare structs here like:
if (id != param.id)
//write something
//etc...
//at the end you can also return bool that indicates that structs are equal or not
return true;
}
}
这显然只适用于两个相同的类,除非您编写更多的比较方法,但是比较两个不同的结构可能很困难
还有其他方法可以比较两个变量(包括结构)。为此,可以使用memcmp()函数,但它不会直接告诉您哪些字段不同
根据@Tony_D的话编辑。
如果(A1.parameter1!=A2.parameter1)…
你试过什么吗?我认为OP要求的是类似于dotnet或java中的反射。但遗憾的是,C/C++中没有类似的东西。如果(A1.parameter1!=A2.parameter1).
您尝试过什么吗?我认为OP要求的是类似于dotnet或java中的反射。但遗憾的是,C/C++中没有类似的东西。如果(A1.parameter1!=A2.parameter1).
您尝试过什么吗?我认为OP要求的是类似于dotnet或java中的反射。但遗憾的是,C/C++中没有类似的东西。如果(A1.parameter1!=A2.parameter1).
您尝试过什么吗?我认为OP要求的是类似于dotnet或java中的反射。但遗憾的是,在C/C++.+1中没有类似的东西,但可能应该是bool compare(const Activity¶m)const
(如果费心跟踪布尔结果)。const&
避免了对param
进行不必要的慢拷贝构造,并且尾部的const
允许您使用该函数将const
活动对象与另一个对象进行比较。谢谢,这是因为我在结构中有很多成员,我不想为每个成员编写一个比较。@Gpouilly您必须为每个+1编写一个比较,但可能应该是bool compare(const Activity¶m)const
(如果费心跟踪布尔结果)。const&
避免了对param
进行不必要的慢拷贝构造,并且尾部的const
允许您使用该函数将const
活动对象与另一个对象进行比较。谢谢,这是因为我在结构中有很多成员,我不想为每个成员编写一个比较。@Gpouilly您必须为每个+1编写一个比较,但可能应该是bool compare(const Activity¶m)const
(如果费心跟踪布尔结果)。const&
避免了对param
进行不必要的慢拷贝构造,并且尾部的const
允许您使用该函数将const
活动对象与另一个对象进行比较。谢谢,这是因为我在结构中有很多成员,我不想为每个成员编写一个比较。@Gpouilly您必须为每个+1编写一个比较,但可能应该是bool compare(const Activity¶m)const
(如果费心跟踪布尔结果)。const&
避免了对param
进行不必要的慢拷贝构造,并且尾部的const
允许您使用该函数将const
活动对象与另一个对象进行比较。谢谢,这是因为我在结构中有很多成员,我不想为每个成员写一个比较。@Gpouilly你必须为每个成员写一个比较