如何创建一个可以初始化 C++ 的类数据类型?
标题基本上说明了一切。我主要想这样做,以便我可以创建一个对象(例如自定义字符串对象),该对象可以初始化其他 API 中其他函数的参数。下面是我尝试让自定义整数类工作的示例:
#include <iostream>
using namespace std;
class test
{
public:
int member;
test(int i) : member(i) {}
friend int &operator=(int &i, test t);
};
int &operator=(int &i, test t)
{
return (i = t.member);
}
int main()
{
int i;
test t = 90;
cout << (i = t);
return 0;
}
不幸的是,我收到一条错误消息,指出operator= 需要是成员函数。我了解 C++ 标准的目标是防止实现赋值运算符的静态和非成员重载,但是还有其他方法可以做到这一点吗?感谢您的任何帮助/建议!
The title basically says it all. I mainly want to do this so that I can create an object (say, a custom string object) that can initialize the parameters of other functions in other APIs. Here's an example of me trying to get a custom integer class to work:
#include <iostream>
using namespace std;
class test
{
public:
int member;
test(int i) : member(i) {}
friend int &operator=(int &i, test t);
};
int &operator=(int &i, test t)
{
return (i = t.member);
}
int main()
{
int i;
test t = 90;
cout << (i = t);
return 0;
}
Unfortunately I receive an error saying that operator= needs to be a member function. I understand the C++ standard's goal in preventing static and non-member overloads for the assignment operator from being implemented, but is there any other way to do this? Thanks for any help/suggestions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不是通过赋值运算符完成的,而是通过重载类型转换完成的。这将使您的主要功能按预期工作:
This is not done with an assignment operator but with an overloaded typecast. This would make your main function work like expected:
您尝试执行的操作需要转换运算符
对于您尝试编写的类(仅包含整数成员),您不需要需要重载
=
运算符。=
运算符是编译器默认为每个类生成的成员函数之一。需要注意的是,它对类成员进行简单的逐位复制(浅复制),因为您只有整数,所以它对您来说应该足够好了。如果您将动态分配的指针作为成员函数,则需要重载
=
运算符,因为在这种情况下,这些指针的浅拷贝将导致包含指向同一动态的成员指针的所有对象内存位置和如果其中一个对象完成其生命周期,其他对象将留下一个悬空指针。正如@Tony,在评论中恰当地指出,浅拷贝通常不好,但并不总是。请参阅他对场景的评论。
如果您想重载赋值运算符,请查看复制并交换成语以正确的方式做事。
您还应该查看三法则 。
What you are trying to do needs an conversion operator
For the class you are trying to write(containing only integer members), You do not need to overload the
=
operator.=
operator is one of the member functions that is generated by the compiler by default for every class. Caveat is, it does a simple bit by bit copy(shallow copy) of class members, since you have only integers it should be good enough for you.You would need to overload the
=
operator if you had dynamically allocated pointers as member functions, because in that case a shallow copy of those pointers would result in all the objects containing a member pointer pointing to the same dynamic memory location & if one of the object finishes it lifetime, other objects are left with a dangling pointer.As @Tony, aptly points in out comments Shallow copy is usually bad but not always. See his comments for a scenario.
If at all you want to overload the assignment operator check out the Copy and Swap Idiom to do it right way.
You should also check out the Rule of Three.
试试这个:
Try this:
赋值运算符不能是友元函数。赋值运算符只能声明为非静态成员函数。这是为了确保它接收 L 值作为其第一个操作数。对于 []、() 和 -> 也是如此。运营商。在您的情况下,由于 int 是内置类型,因此您不能使用成员函数。您可以实现operator int() 将用户定义的类型转换为int。
The assignment operator cannot be a friend function. The assignment operator can only be declared as a non-static member function. This is to ensure that it receives the L-value as its first operand. The same is true for the [], (), and -> operators. In your case, since int is an build-in type, you cannot use member function. You can implement operator int() to cast your user-defined type to int.