全局引用全局对象

时间:2019-05-05 04:32:35

标签: c++ global

我有一些不变的全局对象,像这样:

public class XMLManager
{
    private static XmlDocument xmlInfographic;
    private static TextAsset infographicXML;

    public XMLManager()
    {
        infographicXML = Resources.Load("XML/Infographic") as TextAsset;
        xmlInfographic = new XmlDocument();
        xmlInfographic.LoadXml(infographicXML.text);
    }

    public static string LoadInformation(int index)
    {
        return Cleanup(xmlInfographic.DocumentElement.SelectSingleNode("/Infographic/Info/Info" + index).InnerText);
    }

    private static string Cleanup(string input)
    {
        return input
            .Replace("\n", string.Empty)
            .Replace(" ", string.Empty)
            .Replace("\r", string.Empty); 
    }
}

我需要对这些对象进行“别名”,即引用同一对象的其他名称。

这似乎可行:

const Vehicle Car   = ...;
const Vehicle Truck = ...;
...

允许吗?它会不会以某种方式炸毁在我的脸上?使用const Vehicle& Camion = Truck; 是否和使用Camion一样,包括对象地址?

2 个答案:

答案 0 :(得分:3)

是的,这很好。

  

Reference declaration

     

将命名变量声明为引用,即对已有对象或函数的别名。

这是引用的目的。它们本质上是同一件事的另一个名称。是的,获取引用的地址会为您提供所引用的实际变量的地址。

答案 1 :(得分:1)

是的,您将是相同的,因为相同的变量指向相同的内存地址,并且更改内存地址会影响这两个变量。 https://www.youtube.com/watch?v=Zl-JLUOuyGI

相关问题