Stay in line.Quote:
Originally Posted by aljodav
Printable View
Stay in line.Quote:
Originally Posted by aljodav
I'm sorry, I should know ***********************? OMG, I'm definitely OFF THIS LINE!Quote:
Originally Posted by Ejaz
No need to be sorry, I'm putting you to the place where you belong.Quote:
Originally Posted by aljodav
How do handle agression in interviews! Great derail! Discuss!! :D :thumb:
Actually, handling agression can definately be a part of a job interview. Especially if you are going to be in a client (internal or external) facing role.Quote:
Originally Posted by Zaccheus
I have been on both sides (interviewee and interviewer) where "role playing" involved dealing with a very angry "customer".
Interviewing tech people for their "soft" skills is becoming more and more common.
Ok, I can't resist the temptation to enliven the discussion about this technique for implementing the assignment operator.Quote:
Originally Posted by Zaccheus
Exception safe though it may be, IMHO it can be brutally inefficient. Not because of the call to swap() (that can usually be implemented very efficiently), but because creating the temporary object can force memory allocation that would in many cases be unnecessary for the assignment.
Just think, for example, if std::vector or std::string's assignment operators were implemented in this way. Normally, so long as the size of the RHS vector/string does not exceed the capacity of the LHS vector/string, no reallocation is necessary for the assignment. If implemented using this technique, however, then a new piece of memory would be allocated on every assignment, and an old one thrown away: that's an unnecessary call to each of new[] and delete[] on every assignment. Considering how expensive memory allocation can be, I'd advise caution when choosing to use this technique.
Feel free to disagree, this is just my two cents. :)
Glad to. :D :DQuote:
Originally Posted by HighCommander4
For the mjority of objects, the guarantee that the state will always be 100% consistant overrides the memory concerns and even the time concerns.
If the object happens to be large, then implementing the "bulk" as an independant object eliminates this situation.
About the only place I would agree with your concerns is on (very small) embedded systems.
Out of curiosity, how so?Quote:
Originally Posted by TheCPUWizard
Very Over SimplifiedQuote:
Originally Posted by HighCommander4
Code:class Bulky
{
private:
BigData *m_Bulk;
static bool s_SuppressBulk;
public:
Bulky(Bulky const &src)
{
if (!s_SuppressBulk)
m_Bulk = src.m_Bulk;
}
Bulky operator =(Bulky const &src)
{
s_SuppressBulk = true;
Bulky temp(src);
s_SuppressBulk = false;
Swap(temp);
return *this;
}
}
:eek:Quote:
Originally Posted by TheCPUWizard
But, assuming that all Swap() does is swap pointers, then this is now a shallow copy...
Am I missing something?
EDIT: Wait a minute, I misread your code. As it stands now, no copying is done at all! temp's m_Bulk will be uninitialized, and when Swap() is called, the LHS object will end up with an uninitialized m_Bulk member!
The code is actually missing quite a bit, and was merely intended to illustrate a means where the "cost" of creation of the temporary has been measured to cause a significant application level overhead.Quote:
Originally Posted by HighCommander4
In the 10+ years that Ihave used exception safe assignment, I have had to implement a solution based on the above concept exactly twice. And in one of those cases the code was re-factored within weeks (during the next sprint) to eliminate the need by refining the object model.
I'm afraid this over-simplification makes the point unclear. If s_SuppressBulk is true, then the copy constructor is basically a no-op and m_Bulk is left uinitialized. If s_SuppressBulk is false then the copy constructor performs a shallow copy. Are there other key statements being omitted from your implementation of the copy constructor and assignment operator? Can you elaborate on them? Otherwise I cannot imagine any definition for BigData and Swap that would make this code function correctly.Quote:
Originally Posted by TheCPUWizard
I would also also create a separate, private constructor rather than use a static flag to alter the behavior of the regular copy constructor. The static variable thing is a bit ugly and makes thread-safety more difficult than it has to be.
Yes. You create a localQuote:
Originally Posted by Zaccheus
, and overrideCode:struct/class
.Code:operator()
Why would you want to do this:
'Local' gives you encapsulation. If you modify the 'local function', you only have to re-test the encapsulating function.
Just my two bits,
Pablo.
That's a very good point, one of many special cases where the copy/swap technique can be improved upon. For the bulk of classes, though, I think the copy/swap technique is a good default option.Quote:
Originally Posted by HighCommander4
Lots of thing to learn here! A very good thread ( except aljodav's posts :P )
Well, how about this one?
Suppose we have a template class
The question is : Make "unsigned_type_according_to_n" to beCode:template <unsigned long N>
class Something
{
typedef ...... unsigned_type_according_to_n;
};
- unsigned char, if 0 <= N < 256
- unsigned short, if 256 <= N < 65536
- unsigned long, if 65536 <= N
Sorry if too easy ^_^