Advanced Minds

  • Decrease font size
  • Default font size
  • Increase font size
  • default color
  • red color
  • green color
FireBoard
Welcome, Guest
Please Login or Register.    Lost Password?
c++ reference Not pointers, but references (OOP questions) (1 viewing) (1) Guests
Go to bottom Post Reply Favoured: 0
TOPIC: c++ reference Not pointers, but references (OOP questions)
#8595
Holly Sommer (Visitor)
Click here to see the profile of this user
Birthdate:
c++ reference Not pointers, but references (OOP questions)  
I am investigating this concurrently, but I still have a newbie question. My background is primarily in C and perl (notice - no real OOP), so I've got a decent grasp on using pointers (or finkelheims, etc.), but one thing I'm unsure about wrt JAVA is this: Foo firstFoo = new Foo(); Foo secondFoo = firstFoo; As I understand it, JAVA doesn't use pointers (in spite of the NullPointerException error, which I see was debated earlier). So, both secondFoo and firstFoo are references to the same _object_, rather than pointers to different places in memory which happen to have the same size, correct? Or are secondFoo and firstFoo just copies (instantiations?) of Foo? If the Foo _object_ (prototype) is changed, those changes will be reflected in both firstFoo and secondFoo, instantly, I believe. But how would changes to firstFoo affect secondFoo? Rather
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#8596
Ray Tayek (Visitor)
Click here to see the profile of this user
Birthdate:
c++ reference Not pointers, but references (OOP questions)  
But how would changes to firstFoo affect secondFoo? Rather
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#8597
Aaron Porter (Visitor)
Click here to see the profile of this user
Birthdate:
c++ reference Not pointers, but references (OOP questions)  
In your example firstFoo and secondFoo both reference the same _object_. If you make changes using one reference the changes occur to the _object_ itself so they appear when you use the other reference as well. If you then change firstFoo to reference a different _object_ or null like   firstFoo = null; secondFoo still references the _object_ initially referenced by firstFoo. Did any of that make any sense? Let me know if it didn't and I'll try to rephrase. Aaron
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#8598
Simon Gronlund (Visitor)
Click here to see the profile of this user
Birthdate:
c++ reference Not pointers, but references (OOP questions)  
:I am investigating this concurrently, but I still have a newbie :question. : :My background is primarily in C and perl (notice - no real OOP), so I've :got a decent grasp on using pointers (or finkelheims, etc.), but one :thing I'm unsure about wrt JAVA is this: : :Foo firstFoo = new Foo(); :Foo secondFoo = firstFoo; : :As I understand it, JAVA doesn't use pointers (in spite of the :NullPointerException error, which I see was debated earlier). So, both econdFoo and firstFoo are references to the same _object_, rather than : pointers to different places in memory which happen to have the same ize, correct? Or are secondFoo and firstFoo just copies instantiations?) of Foo? Yes, there will be only one original_Foo _object_ and both firstFoo and secondFoo will refer to it. :If the Foo _object_ (prototype) is changed, those changes will be :reflected in both firstFoo and secondFoo, instantly, I believe. But how :would changes to firstFoo affect secondFoo? Rather
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#8599
c++ reference Not pointers, but references (OOP questions)  
But how would changes to firstFoo affect secondFoo? If by changes to firstFoo , you mean assigning firstFoo to refer to a different _object_ from the original Foo, then firstFoo will now refer to that new _object_, while secondFoo refers to the original Foo. But if you mean that you call methods of firstFoo or directly change its member fields, then you're not changing firstFoo
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#8600
Steve Chapel (Visitor)
Click here to see the profile of this user
Birthdate:
c++ reference Not pointers, but references (OOP questions)  
question. My background is primarily in C and perl (notice - no real OOP), so I've got a decent grasp on using pointers (or finkelheims, etc.), but one thing I'm unsure about wrt JAVA is this: Foo firstFoo = new Foo(); Foo secondFoo = firstFoo; As I understand it, JAVA doesn't use pointers (in spite of the NullPointerException error, which I see was debated earlier). So, both secondFoo and firstFoo are references to the same _object_, rather than pointers to different places in memory which happen to have the same size, correct? Or are secondFoo and firstFoo just copies (instantiations?) of Foo?... Just to add to the other very good answers posted to this question, let me just translate your code into C++ and C: Foo *firstFoo = new Foo(); Foo *secondFoo = firstFoo; Foo *firstFoo = malloc(sizeof(Foo)); // no constructor is called, but you get the idea Foo *secondFoo = firstFoo; After your code executes, both firstFoo and secondFoo point or refer to the same _object_. Be sure to distinguish a reference (which is a small, constant size indication of the place in memory the _object_ resides) from the _object_ that it refers to (which can vary in size).
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
Go to top Post Reply
Powered by FireBoardget the latest posts directly to your desktop
wymiana łozyska głowki ramy zbiór najlepszych stron tectonic nauka acc maker 7.6 download Dywany
roll up handicap phone Taxes Relief day and age