|
Oh oh, In fact, you are right. I have nothing to protest.
Of course, a static member has class scope, so that it can call only others static members.
But in this case, the guy need a solution to adapt with his problem (it may be by historic). Because he needed to call a none static member, it was evident that there had to be exist an instance of class.
It is very clear that we shoud avoid such case but a trick in porogamming makes the life more funny;P and I like to solve that stuck
It is so interesting in reasoning with you.;P
|
|
|
|
|
In general, I agree with Christian on this.
Christian Graus wrote:
A design principle of C++ is to allow bad coding, and trust that coders will write good code, so they never have to fight the language. What you propose is possible, that doesn't make it good coding practice.
And Microsoft encourages this practice with Windows callback functions.
The callback (if implemented as part of a C++ class) has to be a static member, but in general, what's done is that there's a programmer-supplied value (usually void*) that can be passed as well as the function address.
In those cases the approach is to pass the address of an instance of the class, as has been previously suggested.
Steve S
Developer for hire
|
|
|
|
|
Does anybody here has a tutorial or a link to a tutorial of
Visual C++.NET 2003?
|
|
|
|
|
Recently upgrading to VC++ .NET 2003, a logging utility using std::ofstream has broken. When outputting a string, the _address_ of the string gets into the log file and not the string contents!!! What is going on??
Addendum: Seems ostream has no << _Elem* operator. When I changed to write(...) it works like a charm. The standard has changed I guess...
/R
|
|
|
|
|
Yes Robert. This was the most considerable change I was able to find out in VC.NET . You dont have streams now but still you can use STL's fstream and ostream etc.
Just do a plain include like
#include <fstream>
I hope this will solve your problem.
Stuck to Programming through an unbreakable bond
My Articles
|
|
|
|
|
Thanks Aamir,
I'm not sure what you mean by the "plain include"? I do use the include:
#include <fstream>
for the std::ifstream and std::ofstream STL classes.
I checked the VC7 STL implementation and a lot of stream operators are available for the std::ofstream (through std::ostream). All _except_ char* (!!). For example, when the ofstream was passed a char* it used the operator << (void * ptr), so it was no wonder that the pointer value got into the text file instead of the string content. I'm not convinced that this is according to the standard, as STLPort is much more standard compliant and with STLPort it works just as intended. I guess the VC6 STL bugs just have been replaced by VC7 STL bugs...
/Rob
|
|
|
|
|
I have used a bit mask before but how does one
extract individual bits. Say I wanted to extract
bit 28 from 32 bit int, how do I do that?
Thank You
Bo Hunter
|
|
|
|
|
Try this:
int nvalue = 0xabcd;
if ( nvalue & (1<<28) )
;
Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
|
|
|
|
|
That is exactly what I needed.
Thank You
Bo Hunter
|
|
|
|
|
The other guys code works, and sometimes it is best. However consider the following
#define FOO_MASK 0x01000000 //0000 0001 0000 0000 0000 0000 0000 0000 0000, mask for foo
...
if(my_var && FOO_MASK) {
....
This has the advantage that when I read your code I know instantly you are looking for foo, while the other guys code just gives me (1 >> 28). It takes a moment to realize that he is looking at bit 29, and still gives me no indication of what bit 29 is!
Instead of the above you can also use bitfields:
#pragma pack(1)
struct myStruct {
long foo :28;
long bar :1;
long baz :3;
}
#pragma pack()
...
if(mystrct.bar) {
...
This is compiler and CPU specific, but it works fairly often in the real world, and is more readable.
If you must use the other guys code, please do it in a class, and only in one place. Then comment what is going on so when I come across it 10 years from now I can safely make a change.
|
|
|
|
|
Hello to All,
I need to create a bitmap object from the pixel color information in WindowsCE. I know its size ( width and length) and the RGB value of each pixel. Could any body tell me how to create a DIB with above info?
"CreateDIBitmap" function is not supported in WindowsCE.So how can i achieve this funtionality?Please help me.
Thanks in Advance,
Muthupandi.s
|
|
|
|
|
This seems like an easy question, but nevertheless, Im still
stuck.
How can I programmatically create a folder on the c:\ drive?
I just want to create an empty folder.
Please, any response any one can give me will be greatly
appreciated.
Sincerely,
Danielle Brina
|
|
|
|
|
CreateDirectory(...); // in Windows SDK
INTP
|
|
|
|
|
if ( CreateDirectory("c:\\MyDir", NULL))
{
AfxMessageBox("Danielle Brina's directory created ");
}else
{
AfxMessageBox("Danielle Brina's directory creation failed ");
}
Wandered in, liked what I saw, and am here to stay!
What's your excuse?
|
|
|
|
|
Hope this Will help
SHFileOperation
Copies, moves, renames, or deletes a file system object.
int SHFileOperation(<br />
LPSHFILEOPSTRUCT lpFileOp<br />
);
-----------------------------
"I Think It Will Help"
-----------------------------
Alok Gupta
visit me at http://www.thisisalok.tk
|
|
|
|
|
I'm writng an aplication which will blocks banners, popups, some scripts etc.
So I think I need to catch all HTTP traffic, change some parts of packets and put it back to user's program such as browser.
I tried to MSDN and Google but found only solutions how to capture packets with no way to change them.
|
|
|
|
|
It sounds like you'll need to make a proxy - i.e. an app that acts as a web proxy server, and modifies the http traffic that passes through it
Have a search for rewriting proxies, that might help
--
Help me! I'm turning into a grapefruit!
Phoenix Paint - back from DPaint's ashes!
|
|
|
|
|
Most people will agree that macro should be as less frequently
as possible. However, I found that sometimes, macro increase
clearity.
#define Stack( type, next ) StackTemplate<type, (int)&(((type*)NULL)->next)>
So, what other choices do I have besides macro? Should I continue
to use macro in such situation?
Thanks
-----------------------------
C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".
|
|
|
|
|
#define Stack( type, next ) StackTemplate<type, (int)&(((type*)NULL)->next)>
I'm curious.. what significance does the next parameter have? Could you not rewrite the templates instead?
--
Weiter, weiter, ins verderben.
Wir müssen leben bis wir sterben.
I blog too now[^]
|
|
|
|
|
Jörgen Sigvardsson wrote:
I'm curious.. what significance does the next parameter have?
That template specification is weird. It's assuming a structure something like this:
struct MyStruct
{
int member1;
int member2;
int next;
}; The 2nd template parameter in the #define will calculate the size of the structure up to (and not including) the next member, in this case the calculation will return 8. It seems that the structure was at one point used as a linked list, and this stack structure stores it as an array, therefore needing the size of the useful parts of the structure.
Jörgen Sigvardsson wrote:
Could you not rewrite the templates instead?
I would . Although I'd probably rewrite the structure without a next parameter so you can just use the sizeof operator on the type. Of course, the other way to do it is to use a default parameter for the second template parameter, defined as "(int)&(((type*)NULL)->next) "
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Who.. uuh, came up with this template? It's overly complex and kind of bizarre.
--
Weiter, weiter, ins verderben.
Wir müssen leben bis wir sterben.
I blog too now[^]
|
|
|
|
|
// Sample
// C/C++ macro: can not handle min(++n1,n2) or min(n1++,n2)
#define min(n1,n2) ((n1)<(n2)?(n1):(n2))
// min interger: inline equivalent with type checking
inline min(int n1,int n2) { return(n1<n2?n1:n2); }
SIMPLE STACK:
#include "mytype.h" // contains MyType class or structure
#include <stack> // standard C++ header (contains template)
typedef std::stack<MyType> MyStack;
// MyStack.push(...), MyStack.pop(...), MyStack.top(...), MyStack.top()
INTP
|
|
|
|
|
Okay, this is no simple stack class, this is the stack template class
that used in an advanced 3D application -- Doom 3. This is the source
from idsoftware. They used two macros here in a stack template.
idStack(type, next) and STACK_NEXT_PTR( element ) to simplify the
usage. Since many books suggest to avoid macro in C++, and now that
I'm looking at the code from idsoftware, I've been wondering if
there is a way to replace macro. How they used macro in the code
below seems very clever to me.
Note: The code is free and downloadable from idsoftware.
#define idStack( type, next ) idStackTemplate<type, (int)&(((type*)NULL)->next)>
template< class type, int nextOffset >
class idStackTemplate {
public:
idStackTemplate( void );
void Add( type *element );
type * Get( void );
private:
type * top;
type * bottom;
};
#define STACK_NEXT_PTR( element ) (*(type**)(((byte*)element)+nextOffset))
template< class type, int nextOffset >
idStackTemplate<type,nextOffset>::idStackTemplate( void ) {
top = bottom = NULL;
}
template< class type, int nextOffset >
void idStackTemplate<type,nextOffset>::Add( type *element ) {
STACK_NEXT_PTR(element) = top;
top = element;
if ( !bottom ) {
bottom = element;
}
}
-----------------------------
C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".
|
|
|
|
|
Alex Ngai wrote:
How they used macro in the code below seems very clever to me.
It's nothing more than a hack. They've simply taken a C-based stack and made it into a template class, without actually converting it to C++. I don't recommend using it as an example of a good C++ stack. If it was a true C++ template-based stack, the second template parameter wouldn't even be necessary.
The std::stack class will be fine for your use.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Interesting code. Quite ugly. I can't understand what benefit they get from doing it this way, except that you have access to the 'next' pointer. It looks to me as though they either have some old C code they are wrapping with a nice template, or else this was written in the early days of templates.
Unless it's a really wierd data structure which has multiple stacks at once (ie, each element is part of several linked lists at the same time).
You would need to look at the larger context. To remove the macro, you would probably need to completely rewrite the data structure.
Although macros should be avoided if possible (and it's not *always* possible), it seems to me that in this case, the whole design is quite ugly, and the macro is the least of the problems. But without a good idea of how this class is actually used, I can't be sure. Maybe this bit of ugliness makes the overall program really elegant (and fast).
|
|
|
|