在MANAGED c++项目中重载c++的可视化操作

visual Operation overloading C++ in a MANAGED C++ project

本文关键字:c++ 可视化 重载 操作 MANAGED 项目      更新时间:2023-10-16

我有一个托管的c++ dll,它与c# GUI对话。它工作得很好,通信通过包装器进行,一切正常。dll内的代码都是用c++写的,但我不能得到操作符重载工作,我计划用它来排序向量。我不知道我的代码中是否有错误,或者它不起作用,因为它在一个托管的c++项目中。

我试过排序(它,它),但它不起作用,因为操作符重载不起作用。我还尝试过使用比较排序(it, it, less_than_second),但这也不起作用,因为我得到了错误。我尝试了很多东西,我得到了错误,如:-排序只需要2个参数- less_than_second_未声明的标识符

操作符重载不会产生错误。

下面是move类的代码,我想重载操作符<:

public class Move
{
public:
    friend bool operator < (const Move &firstMove, const Move &secondMove)
    {
        return (firstMove.score < secondMove.score);
    }
    bool operator<(const Move& b) {
     return this->score < b.score;
    }
    int positionFrom;
    int positionTo;
    int tileFrom;
    int score;
    bool isJumpMove;
    Move(void);
    Move(int to);
    Move(int from, int to, bool isJumpMove);
    Move(int from, int to, int tileFrom, bool isJumpMove);

};
Testcode in a function of another class.
Move* move1 = new Move();
        move1->score = 2;
        Move* move2 = new Move();
        move2->score = 1;
        Move* move3 = new Move();
        move3->score = 0;
        Move* move4 = new Move();
        move4->score = 4;
        if(move1 < move2)
        {
            Console::WriteLine("2 is bigger than 1");
        }
        else
        {
            Console::WriteLine("1 is bigger than  2");
        }
        vector<Move*> vectorList;
        vectorList.push_back(move1);
        vectorList.push_back(move2);
        vectorList.push_back(move3);
        vectorList.push_back(move4);
        for (int i=0; i<vectorList.size(); i++) {
         Console::WriteLine(vectorList[i]->score);
        }
        sort (vectorList.begin(), vectorList.end() );
        sort (vectorList.begin(), vectorList.end(), less_than_second);
        for (int i=0; i<vectorList.size(); i++) {
         Console::WriteLine(vectorList[i]->score);
        }
the compare function ( it's in the cpp file of another class )
inline bool less_than_second( Move &move1,Move &move2){
        return (move1.score < move2.score);
    }

您的向量元素是Move*(指针)。这是一个内置类型,你不能重新定义它的操作符。less_than_second签名错误,不接受指针

我很确定你需要在你的类之外定义你的布尔运算符,比较两个对象不是类的隐式部分,它的外部对象被比较。

我想你混淆了重载这个词的两种不同用法。操作符重载意味着提供作用于用户类型的机制,就好像它是语言基本类型一样。函数重载意味着您有两个或多个具有相同名称但不同参数的函数。对于c++中的每个操作符,您都有一个必须匹配的特定定义,而您可以为派生类型的参数重载定义多个,它将始终包含相同数量的参数。

我记得c#中。net的逻辑和算术运算符重载要求您使用反射。以中级语言水平发出。你可以测试Type.IsPrimitive。

public delegate result BinaryOperator

      TResult>(TLeft left, TRight right);

/// <summary>
/// See Stepanov and McJones BinaryOperator
/// </summary>
/// <typeparam name="TLeft"></typeparam>
/// <typeparam name="TRight"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <typeparam name="TOwner"></typeparam>
static class GenericOperatorFactory<TLeft, TRight, TResult, TOwner>
{
    private static BinaryOperator<TLeft, TRight, TResult> add;
    private static BinaryOperator<TLeft, TRight, TResult> sub;
    private static BinaryOperator<TLeft, TRight, TResult> mul;
    private static BinaryOperator<TLeft, TRight, TResult> div;

    public static BinaryOperator<TLeft, TRight, TResult> Add
    {
        get
        {
            if (add == null)
            {
                // For debugging
                Console.WriteLine("Creating Add delegate for:nTLeft = {0}n" +
                "TRight = {1}nTResult = {2}nTOwner = {3}", 
                typeof(TLeft), typeof(TRight), typeof(TResult), typeof(TOwner));

                DynamicMethod method = new DynamicMethod(
                    "op_Addition:" + typeof(TLeft).ToString() + ":" + typeof(TRight).ToString() + ":" + typeof(TResult).ToString() + ":" + typeof(TOwner).ToString(),
                    typeof(TLeft),
                    new Type[] { typeof(TLeft), typeof(TRight) },
                    typeof(TOwner)
                );

                ILGenerator generator = method.GetILGenerator();

                generator.Emit(OpCodes.Ldarg_0);
                generator.Emit(OpCodes.Ldarg_1);

                if (typeof(TLeft).IsPrimitive)
                {
                    Console.WriteLine("Adding primitives");
                    generator.Emit(OpCodes.Add);
                }
                else
                {
                    Console.WriteLine("Adding non-primitives");
                    MethodInfo info = typeof(TLeft).GetMethod(
                        "op_Addition",
                        new Type[] { typeof(TLeft), typeof(TRight) },
                        null
                    );

                    generator.EmitCall(OpCodes.Call, info, null);
                }

                generator.Emit(OpCodes.Ret);

                Console.WriteLine("Method name = " + method.Name);

                add = (BinaryOperator<TLeft, TRight, TResult>)method.CreateDelegate(typeof(BinaryOperator<TLeft, TRight, TResult>));
            }

            return add;
        }   
    }

网上还有其他一些例子