Page 1 of 1

Why are all the objects pointers?

Posted: Tue Dec 30, 2008 5:08 am
by Nathan
From all the examples I've seen in the book, documentation and what I have to use in my program to get it to compile, all the object's names have asterisks after them, like this:

wxTextCtrl*
wxStaticBitmap*
wxSlider*
wxButton*

That means they're pointers, right? Why do they have to be pointers?

Is it because they are pointers that we have to use the "->" instead of "." to access member functions and variables?

Posted: Tue Dec 30, 2008 7:14 am
by tierra
Yes, they are pointers, and you should read a book on C++ to understand why they are used. Pretty much all of the beginner C++ books available explain what pointers are, why you should use them, and how to use them.

Posted: Tue Dec 30, 2008 7:26 am
by samsam598
Sorry ,does that mean we can not initialize these kind of object on the stack?

Posted: Tue Dec 30, 2008 10:52 am
by radcapricorn
Pointers are just that: pointers. They can be initialized to whatever value you wish (of course you won't get any good initializing them with random values). Whenever you see a pointer it does not necessary mean an object is created dynamically, that is, pointers can pretty well point to stack-allocated objects. Consider this:

Code: Select all

int a; // stack-allocated
int* pa = &a; // no allocation, just point to static object
int* pb = new int; // heap-allocated
But speaking of wxWidgets (and actually many other GUI toolkits like MFC, GTK and Qt), ALMOST all objects are indeed created on the heap. This is due to the toolkit mechanics and asynchronous (event-driven) behavior. You can look through wxWidgets docs and you'll see that only a limited amount of objects is safe to create on the stack.
So as to should you or should not use stack objects - see the manuals, that's what they are for.

Posted: Tue Dec 30, 2008 3:00 pm
by TrV
Generally, it's more efficient to allocate an object on the heap (general object-oriented programmation consideration). With C++, you can do both (stack and heap (remember C++ comes from C)). Lots of other OO languages allow only one allocation type, which is on the heap, using a garbage collector to clean once objects are unused.

Posted: Tue Dec 30, 2008 3:45 pm
by mc2r
samsam598 wrote:Sorry ,does that mean we can not initialize these kind of object on the stack?
No, it means that the functions returning a pointer allocated the object, the pointer points at, on the heap.

-Max

Posted: Wed Dec 31, 2008 2:40 am
by samsam598
TrV wrote:Generally, it's more efficient to allocate an object on the heap (general object-oriented programmation consideration). With C++, you can do both (stack and heap). Lots of other OO languages allocate allow only one allocation type, which is on the heap, using a garbage collector to clean once objects are unused.
I was wondering so how can we make maximum use of C++'s GOD-LIKE-CONTROL of variable/object's creation(on the stack or on the heap) and lifetime and a lot of C++'s powerful advantage if almost all the lib create object on the heap as other oop lauages do?

Posted: Wed Dec 31, 2008 11:41 am
by Jorg
You can use shared_ptr<SomeObject>. When you use shared_ptr on the stack, and the last shared_ptr instance that points to that specific class is killed, the object is removed. WARNING do no use this with wx* classes. If you for example create a wxButton, wxListCtrl the object gets owned by the parent window that it is displayed on. So you can only use the shared_ptr in your own data model not with wxWidgets

- Jorgen

Posted: Thu Jan 01, 2009 6:21 pm
by Frank
samsam598 wrote: I was wondering so how can we make maximum use of C++'s GOD-LIKE-CONTROL of variable/object's creation(on the stack or on the heap) and lifetime and a lot of C++'s powerful advantage if almost all the lib create object on the heap as other oop lauages do?
You just can't do it with wxWindow-Derived classes (and many other wx classes, except wxDialog).

wxWidgets is old. That was a design-choice done some thirteen or so years ago. Nobody would do this today when designing a toolkit, but you can't change it today without breaking backward-compatibility.

Of course you can use boost::shared_ptr<> with wx classes. You just need to give it a custom deleter, wich calls wxWindow::Destroy() instead of delete. Something like this:

Code: Select all

// This ist the Custom-Deleter
template<class WIN>
struct MyCustomWindowDeleter {
   void operator () (WIN* win) { win->Destroy(); }
}

// Your Smart-Pointer to wxWindow-Objects

boost::shared_ptr<wxFrame> MyWin(new wxFrame(...), MyCustomWindowDeleter<wxFrame>());

// When MyWin goes out of Scope, the Window gets destroyed.

// You could do a free helper template function to get your smartpointer, so the compiler can do it's template deduction magic and you can save some typing:

template<class WIN>
boost::shared_ptr<WIN> getSharedPtr (WIN* win)
{
   return boost::shared_ptr<WIN> MyWin(win, MyCustomWindowDeleter<WIN>());

}

// Now, you can write

boost::shared_ptr<wxFrame> MyWin = getSharedPtr(new wxFrame(...));