필터 지우기
필터 지우기

Can handle class be used to simulate "Pointer" in C language?

조회 수: 10 (최근 30일)
Shunchao Wu
Shunchao Wu 2011년 11월 15일
If I define a "Ptr" class as following:
classdef Ptr < handle
properties
data
end
end
then, objects of Ptr will behave like pointers in C language.
Is this true, guys?
  댓글 수: 1
Shunchao Wu
Shunchao Wu 2011년 11월 15일
Why is the "Bytes" 0 in Variable Editor?
The "Bytes" of a field of a struct, or a property of a object, is 0.
Verified on 2008a and 1010b.

댓글을 달려면 로그인하십시오.

답변 (2개)

Daniel Shub
Daniel Shub 2011년 11월 15일
Objects of the handle class are not identical to pointers. When you pass an object to a function (whether it is a value class or a handle class), MATLAB does not immediately make a copy of the object. It is almost like it passes a pointer (but it is not a pointer). MATLAB has a complicated COW and tries not to copy the object unless it needs to. With a high level language of MATLAB you have to give up some of the control on memory management and hope MATLAB behaves in a reasonable manner.
  댓글 수: 3
per isakson
per isakson 2011년 11월 15일
Undocumented Matlab at, http://undocumentedmatlab.com/ , might interest you.
Shunchao Wu
Shunchao Wu 2011년 11월 16일
Great website!
Thanks, isakson.

댓글을 달려면 로그인하십시오.


Titus Edelhofer
Titus Edelhofer 2011년 11월 15일
Hi,
"then objects of Ptr will behave like pointers". If the behaviour you mean is that once you change a Ptr, all other variables that "point" to the same object are changed as well, then yes. Example:
x = Ptr;
y = x; % both x and y "point" to the same object
x.data = 2; % now y.data is 2 as well
But I would say that "reference" like the & in C++ comes nearer to what handle classes are ...
Titus
  댓글 수: 5
Titus Edelhofer
Titus Edelhofer 2011년 11월 16일
hmm, why not? When you pass an object by reference to a function, the function may change the underlying/referenced object. Of course only, as long as you don't pass a const reference ... Or do I remember entirely wrong, since my C++ programming is about 10 years ago by now ;-).
Shunchao Wu
Shunchao Wu 2011년 11월 19일
en, Titus is right.
The only difference between handle class and C++'s reference is, C++'s reference can not change to another value since initialization.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Build MATLAB Interface to C++ Library에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by