Advertisement

Pointers to classes

Started by August 25, 2000 10:08 PM
4 comments, last by +AA_970+ 24 years, 4 months ago
I have a question, let''s say you have a class CWindow and you declare a variable like: CWindow Main_Window; Now what if you declared it like: CWindow *Main_Window; What''s the difference? I sometimes see code (in the dx samples source code) where pointers are used but they really don''t seem necessary. So why would someone use the pointer? The only reason I see to use the pointer is if you wanted to dynamically create arrays of the class. (Is it b/c some people like to use "->" more than "." ) +AA_970+
If you have a pointer you have to allocate it yourself. Usually that means using the new operator and putting it on the heap.

If you do it without a pointer its put on the stack. When possible this is the better alternative since the stack is faster for allocation and deallocation. The problem is that when the function or where ever you declared it exits the class is deallocated. And if you have any pointers left to it and you try to use them bad things happen.

Btw, CWindow? I''ve heard of TWindow and CWnd
For a good time hit Alt-F4! Go ahead try it, all the cool people are doing it.
Advertisement
Another reason is this:

class Big
{
public:
char bigarray[100000];
};

now when you pass a variable of type Big to a function how would you want to do it: by value or by reference?

By value would require all 100000 characters to be copied to a temporary variable while a reference would pass a 4-byte address to the function. Also if you want to modify a variable that is declared externally to the function that wants to modify it you need to pass by reference.

Oh another reason is polymorphism mechanisms work through pointers:

CWindow *wnd = new CAppWindow;
wnd->DoTheRightThing();

Hope this helps you.

Dire Wolf
direwolf@digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
quote: Original post by blue-lightning

The problem is that when the function or where ever you declared it exits the class is deallocated. And if you have any pointers left to it and you try to use them bad things happen.


Ok this is confusing to me. First of all, a function can exit???
Do you mean like if i have something like a class declared within a class and the "parent" class exits the class declared within it is deallocated?

Second, how would you have a pointer left to the class if the class isn''t declared as a pointer. Do you mean if pointers are left within the class?

quote: Original post by blue-lightning
Btw, CWindow? I''ve heard of TWindow and CWnd


Well this is what i named the class in my app
But in ATL (i never used it i just saw it in the docs) there is a CWindow.



+AA_970+
The following is the type of error he is talking about:

CWindow *AFunc(void)
{
CWindow Result;

return(&Result);
}

Result gets deallocated when AFunc exits. The pointer returned is no longer valid. The address may well still be valid and there may be a valid CWindow structure at that address for a short while, but the address is free for reallocation and once it is reused the program will most likely crash. Instead you would use the following:

CWindow *AFunc(void)
{
CWindow *Result = new CWindow;

return(Result)
}

You then have to remember to deallocate it when it is no longer needed.

CWindow *AWindow = AFunc();
...
delete AWindow;
Keys to success: Ability, ambition and opportunity.
Ok, now i understand what he was talking about.

+AA_970+

This topic is closed to new replies.

Advertisement