# noob c++ doubt

## Recommended Posts

Hey, I often see stuff like: Window *wnd; or Window wnd; (Window being a class) When would I want to use one over the other? that is, the pointer over the non-pointer? Thanks

##### Share on other sites
Use the pointer when you want more instances like this:
Window wnd[50];
--> you do this:
Window *wnd;
wnd = new Window[50];
memset(wnd, 0, sizeof(Window));

Now you've created an array of 50 Windows :)

You should also use the pointer when you want to pass them into a function, so that they can be edited inside that function. If you pass values into a function, you can't change them, because a local copy of them is stored within the function scope and is destroyed again, when you're going out of it.

Hope I could help,
Jeroen

##### Share on other sites
Quote:
 Original post by godmoddermemset(wnd, 0, sizeof(Window));

Don't do that. Zero may not be a valid value for all of the members of Window.

Quote:
 You should also use the pointer when you want to pass them into a function, so that they can be edited inside that function.

Better to use references for this.

##### Share on other sites
Quote:
 Original post by bakery2k1Don't do that.

Quoted for emphasis. In addition to being just Plain Wrong, it is basically a one-way ticket to undefined behaviour land. Your program has rather slim prospects of working correctly if the class contains anything more complex than just plain old data.

And, of course, you should use std::vector instead of manual allocations.

##### Share on other sites
I would use a "window*" (created on the heap) when I need to dynamically create and destroy objects, but in the case of a window structure (assuming I would need at least one of in a game and most likely at the begininng of the application startup) I would recommend a "window" structure created on the stack.

Then assuming the "window" structure was quite large and a function needed to use it, pass in a reference instead of the actual object (which is technically a copy from the copy constructor-another expensive operation). Also, if you need to return a window object from a function, use a reference instead to also avoid the copy construtor and the copying of the structure.

Don't use the memset (The C way) function with arrays of classes. the new[] function will call the constructor of the class you're making an array of for each object. It's on you to write the constructor to return a fully initialized object. Also, I would recommend you use the STL where you can (i.e. std::vector<>). Unless you have a solid reason not to use it, it will give you very good performance and stability (windows is debatable).

I would recommend that you try and get a copy of Scott Meyers "Effective C++" or "C++ for Game Programmers." Both are great books that cover your question (and more) in a lot of detail. They do make the assumption that you know about C++ programming at the beginner level, so they may be difficult to understand if you are still learning C++.

cheers,

bob

##### Share on other sites
memset(wnd,0,sizeof(Window)); ?
Shouldn't it be:
memset(wnd,0,50*sizeof(Window));

Oh, another thing:
Using std::vector is good, but it's memory allocation SUX (see GPG series or whatever other source for STL).
We use our own indexable linked list for fast and predictable allocation/removal of items.
Also, using something like this:
std::vector<Window> Windows;

is what I consider bad, because you need to duplicate memory(copy constructors, operator=() and similar) if new window is added, you have your hands tied and may need to use reserve() and resize() members of vector. Try using this improved variant:
std::vector<Window*> Windows;

copying just pointers is better, since vectors and lists an so on are just containers. Allocation is at your disposal, but at least you can define a clear scheme to operate on the data and transfer data from list to list by removing one pointer from a list and appending it to another.

##### Share on other sites
Quote:
 Original post by Lustmemset(wnd,0,sizeof(Window)); ?Shouldn't it be:memset(wnd,0,50*sizeof(Window));

If this is C++ then no, as some have said both are wrong unless Window is a POD-type which I doubt. In C and with POD types it should be 50*sizeof(Window)

Everyone have talked about using pointers for a collection of windows, but it could also be that Window is a base class like this:
class Window{};class WindowA : public Window{};class WindowB : public Window{};char c = AskUserForWindowType();Window* wnd = 0;if( c == 'a'){    Window* wnd = new WindowA;}else if( c == 'b'){    Window* wnd = new WindowB;}else{    throw std::runtime_error("Error");}

This could for example be used if you have a window class using GLUT and a window class using Win32.

##### Share on other sites
For simplicity as a local variable you'd use a non-pointer. However I don't see many uses for storing a windows instance in a local function. Probably there'd either be some kind of tree or list structure for holding windows, where pointers would be used. Why? Many operations simply are quicker because sending around pointers = sending around small (usually) 4 byte variables. In the case of a function needing an object as an argument, a pointer (or preferably a reference) is the way to go. (You have to if the object is to be modified because else you'll not be manipulating the object in question...) Even if the object is not going to be modified copying the entire object takes a lot more time; it can also cause some issues if this object does not have a proper copy constructor.

With regards to storing a pointer as a local variable inside a function, it has some uses. Sometimes it's practical to short code to avoid cases like:
windowHandler->GetWindows(id)->GetChild(i)->SetColor(255, 255, 0);windowHandler->GetWindows(id)->GetChild(i)->SetFrameWidth(2);

which can be considerably more readable as:
CWindow* child = windowHandler->GetWindows(id)->GetChild(i);child->SetColor(255, 255, 0);child->SetFrameWidth(2);

Also, for a few applications you might want/need to do pointer arithmetic...

##### Share on other sites
Quote:
 Original post by LustUsing std::vector is good, but it's memory allocation SUX (see GPG series or whatever other source for STL).

Well, the point is that std::vector (or any other standard library container) allocates *no* memory by itself. It delegates allocation/deallocation to its user-definable (via a template argument) Allocator type. See, for example, the boost pool allocator library.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
627721
• Total Posts
2978803

• 10
• 9
• 21
• 14
• 12