[Solved] Confused on code

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Confused on code

Post by zodiac976 »

avansc wrote:why dont you just do Character pChar = new Character(params).
What would that do...Character * pChar = new Character()??
I know about the memory heap but I never saw an object
like that...or is that like passing the address of the object
through a pointer?
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Confused on code

Post by dandymcgee »

zodiac976 wrote:
avansc wrote:why dont you just do Character pChar = new Character(params).
What would that do...Character * pChar = new Character()??
I know about the memory heap but I never saw an object
like that...or is that like passing the address of the object
through a pointer?
He meant:

Code: Select all

Character *pChar = new Character(params);
"new" returns a pointer to the newly created Character object and "=" assigns it to pChar.

I think you need to seriously consider just taking a step back. Just spend a few hours or so googling, testing, and perhaps reading some more about pointers, references, & classes because you seem to be in way over your head. Most of these things are extremely basic OOP practices and there are plenty of online resources for learning them.

EDIT: And for God's sake stop double posting. Give me at least 3-5 minutes to revise my post and DO NOT quote me before then. I edit a lot so as to help you to the best of my abilites.
Last edited by dandymcgee on Mon Jun 29, 2009 4:34 pm, edited 2 times in total.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Confused on code

Post by zodiac976 »

dandymcgee wrote:
zodiac976 wrote: charPtr->loc(charPtr);<----------Right here after returning the variables should have been destroyed and
charPtr->dispStats();<----------this should display some wierd negative values but it keeps the values.
What? Why would they have been destroyed.. you are passing a pointer to itself to the class, which then calls it's own dispStats function, returns nothing, then calls dispStats again in main. There's no destruction whatsoever. Your loc function is the equivalent of:

Code: Select all

void Character::loc()
{
   cout << "\nInside loc function\n\n";

   this->dispStats();

   cout << endl;
}
"this" is a pointer to the class it is used in, and since you are passing a pointer to the Character class, to the Character class, it's the same as using the "this" keyword.

Again I don't see why you are expecting anything to be deleted..?
I think I might be losing it or thinking too hard...
I thought a variable is destroyed when it returns?
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Confused on code

Post by zodiac976 »

dandymcgee wrote:
zodiac976 wrote:
avansc wrote:why dont you just do Character pChar = new Character(params).
What would that do...Character * pChar = new Character()??
I know about the memory heap but I never saw an object
like that...or is that like passing the address of the object
through a pointer?
He meant:

Code: Select all

Character *pChar = new Character(params);
"new" returns a pointer to the newly created Character object and "=" assigns it to pChar.

I think you need to seriously consider reading a lot more on pointers, references, & classes because you seem to be in way over your head. Most of these things are extremely basic OOP practices and there are plenty of online resources for learning them.
Yes I know what that does but why would I make a new object?
Why not just make a pointer to the object? or just use the dot
operator?
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Confused on code

Post by dandymcgee »

Please read my edited post in it's entirety. You need to SLOW DOWN and start using google. Or send me your IM info and we can chat in real time. Posting back and forth so quickly is just creating loads of spam and you keep replying before my edit period has ended.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Confused on code

Post by zodiac976 »

dandymcgee wrote:
zodiac976 wrote: charPtr->loc(charPtr);<----------Right here after returning the variables should have been destroyed and
charPtr->dispStats();<----------this should display some wierd negative values but it keeps the values.
What? Why would they have been destroyed.. you are passing a pointer to itself to the class, which then calls it's own dispStats function, returns nothing, then calls dispStats again in main. There's no destruction whatsoever. Your loc function is the equivalent of:

Code: Select all

void Character::loc()
{
   cout << "\nInside loc function\n\n";

   this->dispStats();

   cout << endl;
}
"this" is a pointer to the class it is used in, and since you are passing a pointer to the Character class, to the Character class, it's the same as using the "this" keyword.

Again I don't see why you are expecting anything to be deleted..?
Omg...never mind I feel really stupid now... :roll:.
Sorry I am excited and posting too quick...
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Confused on code

Post by dandymcgee »

Variables only get deleted when they go out of scope. The class variables are in the class scope, not the "loc()" function's scope. Therefore they exist until deleted manually or the Character class object goes out of scope (in your case this doesn't happen until the program terminates).
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Confused on code

Post by zodiac976 »

I know that is why I feel bad and like a tard now..... :roll:.
Anyways what exactly would this be for:

Character * pChar = new Character(params);

I know what it is but why do this? All of the tutorials
and books I read never mention about creating a
dynamic object but I have used something like this:

int * ptr = new int;
delete ptr;

int * ptr = new int[some_num];
delete [] ptr;
User avatar
ultimatedragoon69
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Tue Oct 28, 2008 1:57 pm
Current Project: Pangea's quest (text ~tile~ based rpg)
Favorite Gaming Platforms: Dreamcast, PC, playstation 1, Virtual Boy, Snes
Programming Language of Choice: c++
Contact:

Re: Confused on code

Post by ultimatedragoon69 »

that's what i tryed to say dandymcgee of course i was rather tired and didn't word it as i should have.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Confused on code

Post by dandymcgee »

zodiac976 wrote: Anyways what exactly would this be for:

Character * pChar = new Character(params);

I know what it is but why do this?
Because this:

Code: Select all

Character * pChar = new Character(params);  //Create new Character object and assign it's address to pChar
Does the exact same thing as this:

Code: Select all

Character character(1, 50, 20, 0, 5); //Create new Character object
Character * charPtr = &character; //Assign it's address to charPtr
ultimatedragoon69 wrote:that's what i tryed to say dandymcgee of course i was rather tired and didn't word it as i should have.
Oh well, it happens. ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Confused on code

Post by zodiac976 »

I finally came across this on google:
http://www.tads.org/t3doc/doc/sysman/dynobj.htm
It is often useful to create objects dynamically as well. The main reason you'd want to create an object dynamically, rather than define it statically, is that you don't know in advance that you'll need the object at all - or, more typically, you don't know exactly how many instances of the object you'll need. For example, suppose your game includes a pool of water, and you want the player to be able to fill any container with water from the pool. If you could only define objects statically, you'd have to pre-define a sufficient number of "quantity of water" objects to cover each possible container, and you'd have to add new static objects every time you modified your game to add new containers. You'd also have to work out a scheme to keep track of which objects were already in some container, so that you could find an appropriate unused object when the player filled a new container. With dynamic objects, though, you need only define a class for "quantity of water," and then dynamically create a new instance of this class each time the user fills a new container.
I sorta understand this but I am more of a visual learner so
I wish he would have shown an example :( (actual code). I may
have to read this a million times to completely understand it.

Books I read or tutorials online really only give you basic examples
which helps but for instance:

int * ptr = new num
int * ptr = new num[];

Showing this helps but when people come across stuff like this:
Character * cPtr = new Character;

It confused me even though I know it is a dynamic object.
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Confused on code

Post by zodiac976 »

dandymcgee wrote:
zodiac976 wrote: Anyways what exactly would this be for:

Character * pChar = new Character(params);

I know what it is but why do this?
Because this:

Code: Select all

Character * pChar = new Character(params);  //Create new Character object and assign it's address to pChar
Does the exact same thing as this:

Code: Select all

Character character(1, 50, 20, 0, 5); //Create new Character object
Character * charPtr = &character; //Assign it's address to charPtr
ultimatedragoon69 wrote:that's what i tryed to say dandymcgee of course i was rather tired and didn't word it as i should have.
Oh well, it happens. ;)
Thanks now I understand it..seriously 8-).
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: [Solved] Confused on code

Post by zodiac976 »

So to finish up my newbieness I fiddle with some code
and everyone's posts and came up with this:

Code: Select all

#include <iostream>
using namespace std;

class Character
{
	public:
		Character() {};
		Character(int);
		void displayNum();
		void loc();
	private:
		int num;
};

Character::Character(int defNum):
	num(defNum)
{}

void Character::displayNum()
{
	cout << num << endl;
}

void Character::loc()
{
	this->displayNum();
}

int main()
{
	Character * cPtr = new Character(5);

	cPtr->displayNum();   //output 5
	cPtr->loc();          //output 5

	delete cPtr;

	return 0;
}
Nothing special but I did use everything I learned in this
thread, thanks all ;).
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Confused on code

Post by zodiac976 »

avansc wrote:why dont you just do Character pChar = new Character(params).
Hey thanks for posting that, I read up on dynamic objects
and it is coming in handy in my projects.
Post Reply