longlong105 发表于 2022-9-20 09:04:22

c# 中的 static 函数 无实例化类可使用, 在C++ 中实现?



如下C# 代码,在 ui程序中可以直接使用 DataConvert.GetBn2Int16 ,而不需要重新实例化 DataConvert 类!

    public class DataConvert
    {
      public static Int16 GetBn2Int16(byte[] B, int index)
      {
            
            if (index > B.Length) return 0x0;
            byte[] b = new byte;
            b = B;
            b = B;
            return BitConverter.ToInt16(b, 0);
      }


而 C++ 也适用类似操作就不行!
观看C++的书籍 都在讲需要实例化。。。。 这就纠结了,难道C++ 不可以这么操作吗?

初音之恋 发表于 2022-9-20 09:22:22

和C一样使用函数就行了,C#路径引用比较强大

wudicgi 发表于 2022-9-20 09:44:12

日常不写 C++, 不过查了下是可以的吧

https://www.tutorialspoint.com/cplusplus/cpp_static_members.htm
看 "Static Function Members" 一节, Box::getCount()

longlong105 发表于 2022-9-20 10:00:39

wudicgi 发表于 2022-9-20 09:44
日常不写 C++, 不过查了下是可以的吧

https://www.tutorialspoint.com/cplusplus/cpp_static_members.htm
(引用自3楼)

感谢...可以这样实现!!!
页: [1]
查看完整版本: c# 中的 static 函数 无实例化类可使用, 在C++ 中实现?