Why are all the objects pointers? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Nathan
Experienced Solver
Experienced Solver
Posts: 83
Joined: Sat May 13, 2006 2:22 pm
Location: United States
Contact:

Why are all the objects pointers?

Post by Nathan » Tue Dec 30, 2008 5:08 am

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?

User avatar
tierra
Site Admin
Site Admin
Posts: 1343
Joined: Sun Aug 29, 2004 7:14 pm
Location: Salt Lake City, Utah, USA
Contact:

Post by tierra » Tue Dec 30, 2008 7:14 am

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.

samsam598
Super wx Problem Solver
Super wx Problem Solver
Posts: 324
Joined: Mon Oct 06, 2008 12:55 pm

Post by samsam598 » Tue Dec 30, 2008 7:26 am

Sorry ,does that mean we can not initialize these kind of object on the stack?

radcapricorn
Experienced Solver
Experienced Solver
Posts: 70
Joined: Fri Nov 07, 2008 4:25 pm
Location: Saint-Petersburg, Russia

Post by radcapricorn » Tue Dec 30, 2008 10:52 am

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.
win xp pro sp3/VS Express 2008/MinGW;
win Vista Ultimate/VS 2005;
Debian Lenny/gcc/cegcc-mingw32ce;
wxWidgets-2.8.9 w/wxWinCE;

TrV
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 630
Joined: Wed Jul 04, 2007 1:12 pm

Post by TrV » Tue Dec 30, 2008 3:00 pm

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.
Last edited by TrV on Wed Dec 31, 2008 1:07 pm, edited 2 times in total.

mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r » Tue Dec 30, 2008 3:45 pm

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

samsam598
Super wx Problem Solver
Super wx Problem Solver
Posts: 324
Joined: Mon Oct 06, 2008 12:55 pm

Post by samsam598 » Wed Dec 31, 2008 2:40 am

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?

Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg » Wed Dec 31, 2008 11:41 am

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
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb

Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

Post by Frank » Thu Jan 01, 2009 6:21 pm

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(...));

Post Reply