C ++ object variables store values, not object references

In: http://www.horstmann.com/ccj2/ccjapp3.html in section A3.4. Objects noted the following:

In C ++, object variables store values, not object references

Can you clarify this point? And, does it only pointercontain references to objects in C ++?

Can we say here what object referenceis object address?

Thank.

+5
source share
8 answers

Can you clarify this point?

Best understood compared to, for example, Java or C #:

MyClass a = new MyClass; // creates an instance.
MyClass b = a; // we still have one instance of MyClass in memory

Unlike C ++:

MyClass a; // a *is* an instance of MyClass.
MyClass b = a; // we make a copy of a, now there are *two* instances of MyClass.

And, is this just a C ++ pointer that contains references to objects?

No, links too.

, ?

, A&, . , #. xml_node pugixml :

xml_node root = ...;
xml_node root2 = root; // doesn't copy the document
// root2 refers to the same object as root.
+4

++ value/pointer/reference. Java int, , , , .

Java , , , , , . ++ . - , ( ). , , .

. .

+3

, , , , : , ...

(++ sense, Java) ( , , , ++ 0x), . , , , , , ( ).

, Java, , ( ) Java ++ : -, ++ Java. swap Java, :

template <typename T>
void swap( T & lhs, T & rhs )
{
   T tmp = lhs;
   lhs = rhs;
   rhs = tmp;
}
int main() {
   int a = 1, b = 5; 
   swap( a, b );      // a == 5, b == 1 after this call in this context
}

, , Java ( ), ++.

+2

- , . , . , refernce , 1) 2) . . . , , .

+1

, ++ , and, implit. Java ++ . IN java:

myobject a;
a = new myobject();

++:

myobject a;

   myobject * a;    a = new myobject();

+1

++ . ,

class A {
    int x[5000];
}

, 5000 ints. , A :

class B {
    A content;
};

B 5000 ints . , Java, - A.

++ A:

class C {
    A* content;
};

A

class D {
    A& content;
};

. , ++ , , , undefined, .

+1

++ . , . , , ,

int a;

SomeOtherType a;

, , . , , ,

another_variable = a;

, :

SomeType *a = &some_existing_variable;

, "b = a;" a b - , - , , . . , NULL, , .

+1

, : , ( ) () . -, , .

" " : http://akos.ma/188y

0

All Articles