|
Thanks for your response.
It helped me partailly but I guess now I can solve that.
Once again thank you for your response!
Regards,
Tony
|
|
|
|
|
You´re welcome.
I spent a LOT of time making print preview work for different controls and situations.
it´s the journey, not the destination that matters
|
|
|
|
|
Hi
How to create a wrapper class for an existing class?
thanks :
|
|
|
|
|
Your question is too generic: it depends on your scenario.
Which kind of class do you want to wrap (COM object, ActiveX, ...)?
What is your target?
|
|
|
|
|
Typically what you do is to create a new class exposing methods of an interface that you are interested in. Depending of what you want to wrap, the interface might be a C header file, a COM interface or another class, etc. The wrapper class often manages resource allocation and deallocation of the exposed interface, and this is often the most valuable aspect of wrapping. You can also make it easier to use the interface by bundling methods together as you see fit, removing rarely used parameters and such.
|
|
|
|
|
As a member of the wrapper class, an object of the existing class is created.
The member object is then initialized in the constructor of the wrapper class.
From the methods of the wrapper class, the methods of the existing class will be called using the member object.
|
|
|
|
|
The problem here is that one man's wrapper is another's bridge, adapter, guard or facade. Generally you use wrappers to modify the functionality of the wrapped class in some way so how you do it depends on what the class you're wrapping does and what you want the wrapper to do.
If I were you I'd have a look at a book on design patterns - e.g. "Design Patterns" by Gamma, Helm, Johnson and Vlisides - and go through the patterns looking at which of the classes used are just there to modify the behaviour of other classes by containing them rather than by extending them.
Cheers,
Ash
|
|
|
|
|
hi guys need help about my problem,
am trying to swap the values of 'x' and 'y' without using a temporary storage like 'z'..
here is the code
int x = 150 (or any integer value);
int y = 45 (or any integer value);
output:
x = 45
y = 150
help guys, thanks ahead.
|
|
|
|
|
x += y;
y = x - y;
x -= y; If you have long variables or able to type cast to long, try InterlockedExchange .
|
|
|
|
|
one way to get there is by using a number of exclusive-or operations.
|
|
|
|
|
1)
With simple arithmetic operations:
int x = 150;
int y = 45;
x = x + y;
y = x - y;
x = x - y;
[EDIT]
The above technique is dangerous because in some situations you can exceed the data types range!!
Now I know (thanks to emilio_grv and his excellent post[^]) that this technique is not dangerous if the two variables are of the same integer type.
[/EDIT]
2)
The XOR swap:
int x = 150;
int y = 45;
x ^= y;
y ^= x;
x ^= y;
You can read more about the XOR swap here[^].
Also you can check out some CodeProject articles (and their message boards) on this topic:
- A Curious Economic Swap[^]
- The Stupid XOR Trick[^]
modified on Thursday, July 22, 2010 3:56 AM
|
|
|
|
|
Point 1 isn't dangerous at all.
The carry overflow of the sum is compensated by a missing borrow of the subtraction.
Both +- and ^ operates on closed Galois field (+- of size 232 and ^ on 32 fields of size 2)
The only difference is that the ^ operation requires a combinatorial network simpler than + and this can make the processor ALU faster. But it is quite hard this may be observed outside.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
Thank you very much for this enlightening information.
modified on Thursday, July 22, 2010 4:01 AM
|
|
|
|
|
emilio_grv wrote: But it is quite hard this may be observed outside
Today's processors are so severly RAM-speed limited, and integer arithimetic has been so optimized in the silicon, that I doubt the speed difference is measurable between the addition and XOR methods.
If we're talking something like floats.. well, then the addition method doesn't work reliably anyway.
patbob
|
|
|
|
|
More importantly if you're into this sort of micro-optimisation then add'em subtract'em method only works on arithmetic types. Pointers need not apply.
Cheers,
Ash
|
|
|
|
|
This is absolutely true!
I didn't mention that, because I thought it was obvious.
But you are right - maybe it is not quite obvious for the OP, I should mention this fact.
Thanks,
Nuri
|
|
|
|
|
Aescleal wrote: Pointers need not apply
Not necessarily: swapping two pointers can be achieved by add/subtract if reinterpreted as integers of the appropriate size.
Don't think to pointer as "addresses" and to integer as "numbers".
Think in term of "sequence of bits".
The triple xor (as well as the add subtract) is just a mechanism that mix-up the bits so that the ones that were on a place appear to the other and vice-versa.
What the bit are used to represent what is indifferent.
The important thing is that +- (as well of ^) are not the overloaded specific for the particular type, but just the integer ones.
The only constrain is that the integer size (in bits) must be the same as the swapping type.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
I don't think of pointers as addresses - they're variables that store addresses. The problem with add'em substract'em with pointers is either your code ends up with either loads of casts OR spurious zeros:
x += (y - 0);
Which may or may not be bad depending on your point of view. Personally I'd just use std::swap and say sod the efficiency unless someone can show me in a profiler that it's too slow.
Cheers,
ash
|
|
|
|
|
I prefer the XOR swap, it's more symmetric and elegant.
Cheers,
विक्रम (Got my troika of CCCs!)
"cant stand heat myself. As soon as its near 90`F I seriously start to loose interest in doing much." - fat_boy.
"Finally we agree, a little warming will be good if it makes you shut the f*** up about it." - Tim Craig.
|
|
|
|
|
If you're using C the various tricks other people have shown you will work, well, the XOR trick will, the add'em subtract'em one won't in all cases (you can't add two pointers although you can subtract them but can't use -=).
Having said that I wouldn't bother too much unless it's an accademic exercise. Years ago when I worked in the games industry there were loads of tricks like this one which were meant to save clock cycles. However when, after loads of micro-optimisations like this one, someone actually did any timings there didn't turn out to be a lot of difference.
Cheers,
Ash
|
|
|
|
|
I've been asked this one in an interview.
I wasn't aware of the XOR swap at the time and I think I used a temp variable in my answer.
I don't think I got an offer from that interview.
Pete
|
|
|
|
|
I have been a couple of times as well - I'm a bit wary of taking any job that needs that sort of low level bit fiddling though. A good one to fire back at the interviewer is "I'll show you the C, you show me the boolean algebra proof." It's a good test of what the interviewer is like.
Cheers,
Ash
|
|
|
|
|
lgmanuel wrote: am trying to swap the values of 'x' and 'y' without using a temporary storage like 'z'..
why ?
IMO, other than the fact that it uses a small "hack", there is no reason to use that in 2010. (or as a stupid interview question).
Watched code never compiles.
|
|
|
|
|
consider this:
template<class A>
void xswap(A& a, A& b)
{
for(size_t i=0; i<sizeof(a); ++i)
{
((char*)&a)[i] ^= ((char*)&b)[i];
((char*)&b)[i] ^= ((char*)&a)[i];
((char*)&a)[i] ^= ((char*)&b)[i];
}
}
This works even if A is a class one gigabyte wide, without allocate an extra gigabyte.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
...and who would ever handle such monster objects by value?
|
|
|
|