Public Group

# Class Instances

This topic is 4758 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This is a very newbie question, but I just don't understand the different between classes that are created/instanced like this: Blah blah; Blah* blah; What is the difference? Is Blah blah always required and then Blah* blah is a pointer to the blah class created with Blah blah? I just don't understand. Any help is appreciated :).

##### Share on other sites
The first one creates an instance of the Blah class on the stack. It will be deallocated (and the destructor called) when the scope it was declared in is left.

The second is a typed pointer to a memory location. You can initialize it to point to a Blah allocated on the stack, or you could point it to a Blah allocated through an allocation function (such as new). This is where the real advantage of pointers comes in.

The stack is limited in its size. If you allocate too many objects, it will overflow and you'll crash. Hence fixed size arrays do not work well in applications that need to grow. With an allocation function however, you can grab memory from somewhere else (typically the heap) to store those objects, that other place typically has a lot more memory available to it.

So, to answer your question, learn a bit more, it will be explained later...if you have a decent book.

##### Share on other sites
So, if I created the Blah class, and then did this in main (for example):

Blah* blah;

It wouldn't work because no Blah class was actually created?

I could do...

Blah blah;
Blah* blah2
blah2 = blah;

....right?

##### Share on other sites
think of a pointer as a container of an address.

and
Blah blah is not required.

Do more reading and you will learn about dynamic memory and pointers in argument lists and other such wonderful things.

EDIT: would be

blah2 = &blah;

##### Share on other sites
Quote:
 Original post by brcolowSo, if I created the Blah class, and then did this in main (for example):Blah* blah;It wouldn't work because no Blah class was actually created?I could do...Blah blah;Blah* blah2blah2 = blah;....right?

Your first part was correct, the second wasn't.

The blah2 stores an address (poitner to), so you need to take the address of blah.
blah2 = &blah;

But, something to note, when blah goes out of scope, blah2 will no longer point to valid data.

##### Share on other sites
int main() {  Blah blah;  return 0;}

The Blah lives on the stack. If sizeof(Blah) is 100 bytes, then main() will use 100 bytes of stack space for storage of the Blah. Blah will be automatically created when main() goes into scope (is called), and will be automatically destroyed when Blah goes out of scope (returns).

int main() {  Foo * foo;  foo = new Foo;  delete foo;  return 0;}

The Foo lives on the heap. If sizeof(Foo) is 100 bytes, then 100 bytes of heap space will be used to store the instance of Foo. Meanwhile, 4 bytes of stack space will be used to store the pointer "foo", which points at the instance of the Foo. Further, before the call to "new", no Foo will be in the heap. The act of calling "new" is what actually instantiates a Foo; similarly, the act of executing the "delete" is what deallocates the Foo. If you forget one (or the other), then that part will not execute, and your program might not perform like you want.

• 9
• 13
• 41
• 15
• 12