|
|
Home » U++ Library support » LineEdit, EditFields, DocEdit » operator<<= in tree control reference example
operator<<= in tree control reference example [message #30497] |
Wed, 05 January 2011 10:55  |
gprentice
Messages: 260 Registered: November 2005 Location: New Zealand
|
Experienced Member |
|
|
In the tree control reference example there is this code
void LoadTree(int parent, const String& path, Progress& pi)
{
pi.SetText(DeFormat(path));
for(FindFile ff(AppendFileName(path, "*.*")); ff; ff.Next()) {
if(pi.StepCanceled())
return;
String n = ff.GetName();
if(n != "." && n != "..") {
edit.Add();
edit.Top() <<= n;
Why does the last line use operator<<= instead of operator= ?
If there's no reason then it should be changed to operator= because it took me a while to track down the fact that it seems to go through Ctrl::operator<<= which goes through the SetData virtual function into EditField::SetData. For a newcomer or even not for a newcomer, it's not easy to find what that operator<<= does - it doesn't make any sense to use it in an example.
Also, why does the above code do an unnecessary call to the Top() function - couldn't it just be edit.Add() = n;
And lastly (off topic but related to the edit.Add function call), does U++ assume that memory allocation with new never fails - how does U++ handle failure?
Graeme
|
|
|
|
|
Re: operator<<= in tree control reference example [message #30514 is a reply to message #30500] |
Thu, 06 January 2011 10:32   |
cbpporter
Messages: 1427 Registered: September 2007
|
Ultimate Contributor |
|
|
Well, the fastest way to get the answer would be if you do the test yourself. Make new fail by allocating to much memory and see what happens .
Or quote from the docs:
Quote: |
We decided to ignore possibility of "out-of-memory" exceptions and recovery. If U++ application goes out of memory, it simply prints the error message and terminates. This is quite pragmatic resolution - our experience is that it is quite hard and annoying to achieve robustness here and it cannot be reliably tested. Also, most platforms with virtual memory will almost freeze long before out-of-memory problem due to intensive page swapping. Connected issue - default and copy constructors are not allowed to throw exceptions in U++ (the common reason to throw exception here was out-of-memory condition). This limitation will be removed in future releases.
|
As for the use of Top, I have no idea. Ask the author of the package. I would not use a Top after and Add if the Add returns a reference, and I'm not sure if there is a real reason for it.
The way newcomers learn this is by going over the code, the documentation, asking on the forum and applying the conventions themselves. It is something you will hit your head on, overcome it and learn from your experience. This solution is not idle, but there is no easy way to point people in the right direction from the start and make sure they do head that way.
|
|
|
|
|
Re: operator<<= in tree control reference example [message #30521 is a reply to message #30518] |
Thu, 06 January 2011 12:12   |
|
gprentice wrote on Thu, 06 January 2011 11:53 | The only operator<<= I can find is in the Ctrl base class
const Value& operator<<=(const Value& v) { SetData(v); return v; }
but the call to SetData (not SetValue?) is not virtual and just throws the data away - yet the code works ???
|
The fact you are missing is that SetData() is virtual That way, if you override it in any Ctrl derived class you get automatically the <<= operator, since it is defined in Ctrl in such way that it calls the overridden method from the child class. There is no SetValue() (except some special Ctrls, like DropList, TreeCtrl etc.), the general interface for operating value of Ctrls is made entirely by SetData(), GetData() and their respective operators <<= and ~.
Honza
|
|
|
Re: operator<<= in tree control reference example [message #30522 is a reply to message #30521] |
Thu, 06 January 2011 12:26  |
gprentice
Messages: 260 Registered: November 2005 Location: New Zealand
|
Experienced Member |
|
|
yep, I know SetData is a virtual function but a call to a virtual function is "virtual" only when the call is made on a pointer or reference - except, I've just looked up a C++ book I have and in a member function (such as operator<<=) an unqualified call is also virtual because it's equivalent to this->SetData - hmm, I probably knew that once but I've long since forgotten!
Graeme
|
|
|
Goto Forum:
Current Time: Fri May 09 17:57:45 CEST 2025
Total time taken to generate the page: 0.01758 seconds
|
|
|