你如何解释C ++指向一个C#/ Java开发人员? [英] How do you explain C++ pointers to a C#/Java developer?

查看:81
本文介绍了你如何解释C ++指向一个C#/ Java开发人员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#/ Java开发人员努力学习C ++。当我尝试学习指针的概念,我触动了我以前一定处理这个概念的想法。如何使用指针只熟悉到.NET或Java开发人员的概念来解释?有我的真正的从来没有处理这一点,是它只是隐藏了我,还是我用这一切的时候不调用它呢?

I am a C#/Java developer trying to learn C++. As I try to learn the concept of pointers, I am struck with the thought that I must have dealt with this concept before. How can pointers be explained using only concepts that are familiar to a .NET or Java developer? Have I really never dealt with this, is it just hidden to me, or do I use it all the time without calling it that?

推荐答案

一个Java对象是++共享指针是C的等价物。

Java objects in C++

A Java object is the equivalent of a C++ shared pointer.

一个C ++指针就像没有建在垃圾回收的Java对象。

A C++ pointer is like a Java object without the garbage collection built in.

C ++有分配对象的方式有三种:

C++ has three ways of allocating objects:


  • 静态存储持续时间的对象。

    • 这是在启动时创建(前主)和主退出后死亡。结果
      还有一些技术告诫到,但是这是基础。


    • 这些声明时创建的,当他们走出去的范围破坏。结果
      我相信这是如C#结构

    动态存储时间的对象


    • 这是通过新创建的最接近C#/ Java对象( AKA指针)结果
      技术上指针需要可通过手动销毁删除。但是,这被认为是不好的做法,在正常情况下,他们被放在控制他们的寿命自动存储时间的对象(通常称为智能指针)内。当智能指针超出范围被销毁,析构函数可以调用删除上的指针。智能指针可以但正如细粒度的垃圾收集器。

    • These are created via new and the closest to a C#/Java object (AKA pointers)
      Technically pointers need to be destroyed manually via delete. But this is considered bad practice and under normal situations they are put inside Automatic Storage Duration Objects (usually called smart pointers) that control their lifespan. When the smart pointer goes out of scope it is destroyed and its destructor can call delete on the pointer. Smart pointers can be though of as fine grain garbage collectors.

    最接近于Java是shared_ptr的,这是一个智能指针,保持指针的用户的数量的计数,并删除它,当没有人使用它

    The closest to Java is the shared_ptr, this is a smart pointer that keeps a count of the number of users of the pointer and deletes it when nobody is using it.

    这篇关于你如何解释C ++指向一个C#/ Java开发人员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆