15

有点奇怪的问题,但我想知道任何人都可以帮助...

在 C++ 中,我可以做这样的事情

class MyOtherClass
{
     private:
         MyLogger* logger;
     public:
         MyOtherClass (MyLogger* logger)
              : logger (logger)
         {}
};

class MyClass
{
     private:
         MyLogger* logger;
     public:
         MyClass (MyLogger* logger)
              : logger (logger)
         {}
};

int main (int c, char** args)
{
    MyLogger* logger = new MyLogger ();
    /* Code to set up logger */
    MyOtherClass* myOtherClass = new MyOtherClass (logger);
    MyClass* myClass = new MyClass (logger);
}

这样每个其他对象(myOtherClass 和 myClass)都将包含指向记录器的指针,因此它们将调用相同的记录器类。但是,我将如何在 C# 中实现相同的目标?有没有办法存储指向全局对象的引用或指针 - 我猜如果我在 C# 中执行类似的操作

public class MyClass
{
     private MyLogger logger = null;

     public MyClass (MyLogger _logger)
     {
         logger = _logger;
     }
};

它实际上将类变量记录器分配给_logger的副本?还是我把事情搞混了:S

非常感谢任何帮助,并提前感谢您!

4

4 回答 4

20

在 C# 中它实际上要简单得多。

基本上,你可以这样做:

MyLogger logger = new MyLogger();
MyOtherClass myOtherClass = new MyOtherClass(logger);
MyClass myClass = new MyClass(logger);

在 C# 中,类基本上作为引用保留(实际上只是引擎盖下的指针)。在此代码段中,您将引用传递logger给两个对象的构造函数。该引用是相同的,因此每个实例都有相同的MyLogger实例。

在这种特殊情况下,您几乎只需要删除指针语法 =D

于 2012-11-08T16:21:43.960 回答
4

你把事情搞混了。在 C# 中,赋值语句如

    logger = _logger;

复制引用,而不是对象。在这条语句执行之后,仍然(最多)只有一个MyLogger——它现在两个对象变量引用。

于 2012-11-08T16:22:12.740 回答
2

如果类型是引用类型(类就是这种情况),那么您将复制引用,而不是对象本身。

与引用类型相反,您有值类型。值类型基本上是基本类型:int,,double等,

在您的情况下,这意味着您将使用相同的对象,无论您是从类还是从外部调用方法访问它。这是因为您的目标是引用的对象。

于 2012-11-08T16:21:19.047 回答
0

为将来的我添加评论,因为有时我也忘记了,对于 c# 中的新手也想可视化“c++ 指针”与“c# 对象引用”与“C# 对象引用 By Ref”。注意 Passing By Ref(Last method call) 和分配新的 obj 是如何改变原始对象的。

using System;

public class Emp
{
    public int TimeSpentInCompany {get; set;}
}

public class Program
{
    public static void Main()
    {
        
        Emp t1 = new Emp{TimeSpentInCompany = 5};
        Console.WriteLine("original obj before method call-->" + t1.TimeSpentInCompany);
        
        // Test is one by one
        
        // var res = PassObject_SimpleUpdateMemberAndPrint(t1);
        // var res = PassObject_SimpleUpdateObjectAndPrint(t1);
        // var res = PassObjectByRef_SimpleUpdateMemberAndPrint(ref t1);
        var res = PassObjectByRef_SimpleUpdateObjectAndPrint(ref t1);
        
        Console.WriteLine("original obj after method call-->" + t1.TimeSpentInCompany);
        Console.WriteLine("obj from method response-->" + res.TimeSpentInCompany);
    }
    
    static Emp PassObject_SimpleUpdateMemberAndPrint(Emp data)
    {
        /*
            original obj before method call-->5
            in method before modification obj member--> 5
            in method AFTER modification obj member--> 9
            original obj after method call-->9
            obj from method response-->9
        */      
        Console.WriteLine("in method before modification obj member--> "+ data.TimeSpentInCompany);
        data.TimeSpentInCompany += 4;
        Console.WriteLine("in method AFTER modification obj member--> "+ data.TimeSpentInCompany);
        return data;
    }   
    
    static Emp PassObject_SimpleUpdateObjectAndPrint(Emp data)
    {
        /*
            original obj before method call-->5
            in method before assigning new obj --> 5
            in method AFTER assigning new obj --> 9
            original obj after method call-->5
            obj from method response-->9
        */
        Console.WriteLine("in method before assigning new obj --> "+ data.TimeSpentInCompany);
        data = new Emp{TimeSpentInCompany = 9};
        Console.WriteLine("in method AFTER assigning new obj --> "+ data.TimeSpentInCompany);
        return data;
    }
    
    static Emp PassObjectByRef_SimpleUpdateMemberAndPrint(ref Emp data)
    {
        /*
            original obj before method call-->5
            in method before modification obj member--> 5
            in method AFTER modification obj member--> 9
            original obj after method call-->9
            obj from method response-->9
        */      
        Console.WriteLine("in method before modification obj member--> "+ data.TimeSpentInCompany);
        data.TimeSpentInCompany += 4;
        Console.WriteLine("in method AFTER modification obj member--> "+ data.TimeSpentInCompany);
        return data;
    }   
    
    static Emp PassObjectByRef_SimpleUpdateObjectAndPrint(ref Emp data)
    {
        /*
            original obj before method call-->5
            in method before assigning new obj --> 5
            in method AFTER assigning new obj --> 9
            original obj after method call-->9
            obj from method response-->9
        */
        Console.WriteLine("in method before assigning new obj --> "+ data.TimeSpentInCompany);
        data = new Emp{TimeSpentInCompany = 9};
        Console.WriteLine("in method AFTER assigning new obj --> "+ data.TimeSpentInCompany);
        return data;
    }
}
于 2020-07-18T13:05:43.137 回答