开发者

How do I create a class that can initialize C++ data types?

开发者 https://www.devze.com 2023-03-23 08:13 出处:网络
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 e

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 goa开发者_Go百科l 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!


This is not done with an assignment operator but with an overloaded typecast. This would make your main function work like expected:

#include <iostream>
using namespace std;

class test
{
public:
    int member;
    test(int i) : member(i) {}
    operator int() const {return member;}
};

int main()
{
    int i;
    test t = 90;

    cout << (i = t);
    return 0;
}


What you are trying to do needs an conversion operator

operator int() 
{
    return this->member;
}

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:

class test
{
public:
    int member;
    test(int i) : member(i) {}

    operator int() {return this->member;}
};

int main(void)
{
    int i;
    test t = 90;

    cout << (i = t);
    return 0;
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消