存储对象的引用

时间:2012-11-08 16:19:23

标签: c# c++

有点奇怪的问题,但我想知道有人可以提供帮助......

在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)都将包含一个指向logger的指针,因此它们将调用相同的logger类。但是,我如何在C#中实现同样的目标?有没有办法存储一个引用或指向全局对象的指针 - 我在C#中猜测,如果我这样做

public class MyClass
{
     private MyLogger logger = null;

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

它实际上是否将类变量记录器分配给_logger的副本?或者我正在混淆:S

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

4 个答案:

答案 0 :(得分:12)

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

基本上,你可以这样做:

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

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

在这个特定的例子中,你几乎只需要删除指针syntax = D

答案 1 :(得分:4)

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

    logger = _logger;

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

答案 2 :(得分:1)

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

与参考类型相反,您有值类型。值类型基本上是基本类型:intdouble等,

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

答案 3 :(得分:0)

为将来的我添加注释,因为有时我也忘记了,对于c#的新手来说,也想可视化“ c ++指针” vs“ c#对象引用” vs“ C#对象引用按引用”。 请注意,通过Ref(最后一个方法调用)传递并分配新的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;
    }
}