Advertisement

C++ Workshop - Pointers (Ch. 8)

Started by July 17, 2006 03:19 PM
87 comments, last by mihaimoldovan 12 years, 9 months ago
I believe the book is saying that the operating system will take care of it for you when you exit the program. I suggest you ignore the book on this and use delete for every new, delete[] for every new[], free for every malloc, etc. It is never good to get in the habbit of assuming that all of your resources will be released when you close the program because they aren't.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Quote:
Original post by Zahlman
class Foo {  int bar;  public:  void wibble() {    bar = 1; // the usual way    this->bar = 1; // making use of the 'this pointer'  }};int main() {  Foo x, y;  x.wibble(); // <-- 'this' points to x  y.wibble(); // <-- 'this' points to y}


I find this confusing for the reason that, when you refer to a pointer, you use the -> operator normally, but here, your using the dot . operator when dereferencing wibble ? Why is that?

Is this specific for the this pointer?

Advertisement
Quote:
Original post by J0Be
Quote:
Original post by Zahlman
class Foo {  int bar;  public:  void wibble() {    bar = 1; // the usual way    this->bar = 1; // making use of the 'this pointer'  }};int main() {  Foo x, y;  x.wibble(); // <-- 'this' points to x  y.wibble(); // <-- 'this' points to y}


I find this confusing for the reason that, when you refer to a pointer, you use the -> operator normally, but here, your using the dot . operator when dereferencing wibble ? Why is that?

Is this specific for the this pointer?


Foo x, y are not pointers. Therefore he can use the . operator.
However if he declared it as such: Foo *x, *y (notice the pointer operator) then he would have to use the -> operator. And this is always a pointer, so you always have to use the -> operator.

Beginner in Game Development?  Read here. And read here.

 

Quote:
Original post by Alpha_ProgDes
And this is always a pointer, so you always have to use the -> operator.


Hi Alpha_ProgDes,

Thank you for the help, I got it now.
Alpha, I think you should run the C# workshop. [grin]
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Quote:
Original post by jwalsh
Alpha, I think you should run the C# workshop. [grin]


[grin] Thank you. But that would require me to know C# [grin]

Beginner in Game Development?  Read here. And read here.

 

Advertisement
They say the best way to learn is to teach. [grin]
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Pardon the interruption of who should run the C# workshop, lol.

Were there any exercises related to this chapter? I have read Ch 8 and 9, and well, tbh, I am thoroughly confused now.....pointers/references...gonna go back and reread the chapters, just wondered if I missed exercises somewhere? Apparently this is an oft confused topic...

Thanks,

Shawn





Click jwalsh username --> View all topics started by this user --> Refine Search Parameters --> Forum(C++ Workshop) --> Page 2 --> C++ Workshop - Week 7 (Ch. 8) - Quizzes and Extra Credit

Search and you shall find [smile]

Hello all,

That is correct, once again I have a question, lol. In the following code, I was practicing how to use New and Delete with Pointers. In my little while loop, LocalVariable++ does exactly what I thought it would do, increments by 1. However, the next line *pHeap +=1, I had originally put in *pHeap++ thinking it would do the exact same, increment by 1....however, that did not happen. Instead I got all kinds of weird values for *pHeap and a Debug Assertion Failed error. Now my understanding is that using the * in front of the variable gives me the value at pHeap, correct? Why then would a simple ++ not work?

Thanks,

Shawn

*Edit* What is a debug assertion fail error anyhow? lol, i guess i can google that, evidentally it is something bad, ;).

//Using New and Delete and Pointers#include <iostream>int main(){	using namespace std;	unsigned short LocalVariable = 0;						//creates a local variable in main	unsigned short *pLocalVariable = &LocalVariable;		//creates a pointer to LocalVariables address	unsigned short *pHeap = new unsigned short;				//creates a pointer on the heap(free store)	*pHeap = 1;			//assigns the value 1 to the heap variable pHeap	while (LocalVariable<10)	{		cout<<"LocalVariable = "<<LocalVariable<<endl;		cout<<"*pLocalVariable = "<< *pLocalVariable<<endl;		cout<<"*pHeap = "<< *pHeap<<endl;		LocalVariable++;		*pHeap += 1;	}	delete pHeap;	return 0;}


[Edited by - shawnre on June 3, 2007 11:27:17 AM]

This topic is closed to new replies.

Advertisement