Re: working with pointers



Michael wrote:
> sorry, I'm used to working in c++ :-p
>
> if i do
> a=2
> b=a
> b=0
> then a is still 2!?
>
> so when do = mean a reference to the same object and when does it mean make
> a copy of the object??

To understand this in C++ terms, you have to treat everything, including
simple integers, as a class instance, and every variable is a reference
(or "smart pointer".) The literals '0' and '2' produce integer class
instances rather than primitive integers. Here's a reasonable C++
translation of that code, omitting destruction issues:

class Integer
{
private:
int value;
public:
Integer(int v) { value = v; }
int asInt() { return value; }
}

void test()
{
Integer *a, *b;
a = new Integer(2);
b = a;
b = new Integer(0);
}

In that light, do you see why a is still 2?

Shane
.



Relevant Pages

  • Re: Q: global variable
    ... The life span of a variable is as long as the class instance it is ... We typically don't hold a reference to a Page ... A static field ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: "Global" objects
    ... reference to a class instance. ... Static objects are problematic, and should be ... protected to a class instance that they reside in (other than the Form ... have a main form on which I show various user controls depending on ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: An object-oriented network DBMS from relational DBMS point of view
    ... The sentence has a context, it presumes a memory model, you can point ... "A class instance is explicitly created by a class instance creation ... In the context of the Java memory model, you need a reference to them ...
    (comp.databases.theory)
  • Re: How to Clone a class instance
    ... > Could anyone show me how to clone a class instance without writing code to ... contained objects), with some of them reference counted, blind memory ... instance1 is already a typed pointer (usually referred to as a "reference") ...
    (borland.public.delphi.language.objectpascal)
  • Re: is null allowed in C#?
    ... Fei Li wrote: ... obj is a variable, not a class instance. ... "This value isn't a reference to a class instance at all. ...
    (microsoft.public.dotnet.languages.csharp)