|
Like you've shown it is what I was thinking...
class CKeypadWnd : public CWnd
{
};
class CKeypadEdit : public CEdit
{
static CKeypadWnd *pKeypadWnd;
static int KeypadRefCount;
static CKeypadWnd *pActiveKeypadWnd;
...
};
...
// In the CPP file:
CKeypadWnd *CKeypadEdit::pKeypadWnd = NULL;
int CKeypadEdit::KeypadRefCount = 0;
CKeypadWnd *CKeypadEdit::pActiveKeypadWnd = NULL;
...
Mark
Mark Salsbery
Microsoft MVP - Visual C++
This episode brought to you by the number 3
|
|
|
|
|
There's only a couple of reasons I can think of to share a class this way:
1) You're sharing data between the various CEdit objects
2) There are a lot of your CEdit objects in existance at a time and you're concerned with memory usage, performance (the time to create the keypad object), or memory fragmentation.
I try to avoid static members unless the class is intended to provide static methods. If it were me, and if item 2 is the issue, I would dynamically create a single application global instance of the keypad and just use it wherever it's needed.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Actually, I do have lots of Edits. But I wanted to have a simple way to just declare each edit and then have the keypad function all built in without additionally allocating it again globally and then tie it to each Edit somehow.
Thanks for all the responses and comments!
|
|
|
|
|
Well, once you allocate your CEdit iobject on the heap, everything inside it also resides on the heap, so you can declare your keybad object like so:
class CMyEdit { private: CMyKedPad m_KeyPad; };
...and then initialize it in the CMyEdit constructor.
At that point, there's no overhead involved in allocating that object on the heap, and you don't have to worry about delete/null it when your CMyEdit object cleans itself up. My guess is that your KeyPad object doesn't contain much data, so it's not like multiple instances is going to take up much space (probably less than 64 bytes).
I still see no need for a static object (which is my entire point).
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Well, the keypad will be a dialog of sort (enable touchscreen capability). In this case, would you still think a static is not necessary and then just declare the kaypad class in the Edit class?
Thanks.
Stan
|
|
|
|
|
hi, may be i'm a crazy idiot, but is there a way to change a funtion code and use it while running my project??
I buit a project to use a function such as "void cal(int a, int b) " stored in "cal.h"
first my project create a new header file like "cal.h" , it changes this function to "void cal(int a,int b,int c)" and its code too; second i want to use that func after updated and don't want to exit project.
But i'm only change this header file on the first step, then to use it i must compiled again. hic.
i've still not found a method yet, hic.
and..sorry for my bad English
|
|
|
|
|
what do you want to do exactly?
I mean do you want to change values iside your programm while running?
Or do you want to use different Functions depending on s.th.?
|
|
|
|
|
Do you mean edit and continue?
While running an application you can stop at a breakpoint in the debugger, edit the code, and
continue running with the new code.
Make sure edit and continue is enabled in the Tools/Options property pages.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
This episode brought to you by the number 3
|
|
|
|
|
N.D.Quan wrote: I buit a project to use a function such as "void cal(int a, int b) " stored in "cal.h"
first my project create a new header file like "cal.h" , it changes this function to "void cal(int a,int b,int c)" and its code too; second i want to use that func after updated and don't want to exit project.
no thats not possible, but you can use argument list to achieve same ..
type va_arg(<br />
va_list arg_ptr,<br />
type <br />
);<br />
void va_end(<br />
va_list arg_ptr <br />
);<br />
void va_start(<br />
va_list arg_ptr <br />
);
void va_start(<br />
va_list arg_ptr,<br />
prev_param <br />
);
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief
|
|
|
|
|
|
Hello @ All!
I hope someone can help me in understanding how things work. So ich try to explain my problem.
I have an application written in c++. Implementing an interface gives access to a callback function.
So I'm called back when new data is available.
No comes the part where I have to put my hands on. In this Callback I copy the data and pass it to various functions. At the end of passing&calculation i have calulated some information. this information I want to give to an TCP or UDP Server. So the serverdata has to be updated each time i have calculated new data.
so i have experimented with the winsock tutorials... but how can i integrate the server in my running application whitout blocking it? Do I need two threads/processes or callbacks or events or even functionpointers? Has anyone suggestions on which to investigate ?
Thanks a lot
|
|
|
|
|
You may want to do some studying: Windows Sockets 2[^]
There's a variety of ways to handle sockets without blocking the application.
You may or may not need multiple threads depending on the communication demand on the CPU.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
This episode brought to you by the number 3
|
|
|
|
|
Study of that stuff is niche but does not fit my needs. I have an app running and want to include the server into this app in a way the rest can still do what it has to do..
Greetz
|
|
|
|
|
// This is file example.h
inline void f() { ... } // definition of f
If i include the above header file into example1.cpp and example2.cpp in a aproject and build the project why does the linker not give an error of redefinitation assuming the compiler has inlined the funtions.
Assuming the functions are not in inlined by the compiler then they shall become static to each file. This introduces the overhead of functional call and also of making the function f() static in the translation unit of both the files according to the old rules. With new rules there is something called out of line copy of function f() that shall be generated.
Can someone shed light on the case on the scenarios and what happens when inlined functions are rejected by the compiler.
|
|
|
|
|
Hi,
First, you don't need the keyword inline in the Headerfile if you use the {//impl.} in the headerfile.
void f(){} in a header is already inline.
Now to the include statement... what do you think does include mean???
hmm... I thik it means substitude this code here... Because you could
write the code you write in a headerfile as well in the cpp file.
So you have your' code included in the first and in the second cpp file.
Hope this helps...
Greetz
|
|
|
|
|
there's a lot of info in the MSDN for this, under the "inline" keyword.
here's some of it:
---
Even with __forceinline, the compiler cannot inline code in all circumstances. The compiler cannot inline a function if:
The function or its caller is compiled with /Ob0 (the default option for debug builds).
The function and the caller use different types of exception handling (C++ exception handling in one, structured exception handling in the other).
The function has a variable argument list.
The function uses inline assembly, unless compiled with /Og, /Ox, /O1, or /O2.
The function is recursive and not accompanied by #pragma inline_recursion(on). With the pragma, recursive functions are inlined to a default depth of 16 calls. To reduce the inlining depth, use inline_depth pragma.
The function is virtual and is called virtually. Direct calls to virtual functions can be inlined.
The program takes the address of the function and the call is made via the pointer to the function. Direct calls to functions that have had their address taken can be inlined.
The function is also marked with the naked __declspec modifier.
---
|
|
|
|
|
Hi,
I have the following situation.
class CPCntPPg : public CPropertyPage
{
DECLARE_DYNCREATE(CPCntPPg)
// Construction/destruction.
public:
CPCntPPg();
~CPCntPPg();
// Dialog Data.
//{{AFX_DATA(CPCntPPg)
enum { IDD = IDPP_PCNT };
CVQProgress m_ctlVQProgress; // CStatic derived class.
//}}AFX_DATA
CEdit m_ctlVQ[VQPNUM];
CSpinButtonCtrl m_ctlVQSpin[VQPNUM];
int m_nVQ[VQPNUM];
:
// Overrides.
// ClassWizard generate virtual function overrides.
//{{AFX_VIRTUAL(CPCntPPg)
public:
virtual BOOL OnApply();
protected:
virtual void DoDataExchange(CDataExchange *pDX); // DDX/DDV support.
//}}AFX_VIRTUAL
// Implementation.
protected:
void InitPPgData(); // Initializes the property page.
void InitSpinButtonCtrls();
:
// Generated message map functions.
//{{AFX_MSG(CPCntPPg)
virtual BOOL OnInitDialog();
//}}AFX_MSG
afx_msg void OnChangeVQ(UINT nID);
:
DECLARE_MESSAGE_MAP()
};
// EN_CHANGE handler. When the contents of an edit control is changed by the user I want the
// m_ctlVQProgress control to redraw itself to reflect the changes.
void CPCntPPg::OnChangeVQ(UINT nID)
{
int nVQ; // Contents from changing edit control retrieved by GetWindowText() and
// _ttoi().
:
m_ctlVQProgress.SetVQ(nIndex, nVQ, TRUE /*Redraw*/); // This will initialize a data
// member and redraw the control.
}
The problem is when the edit control is set to an initial value by DDX/DDV in OnInitDialog
the OnChangeVQ handler gets also called causing the m_ctlVQProgress control to draw itself. This
happens before the OnPaint handler of the m_ctlVQProgress control is invoked. Is there any way to prevent this from happening ?
TIA
|
|
|
|
|
What's different about the initial state of the progress control compared to when a change is
made to the edit control later by the user?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
This episode brought to you by the number 3
|
|
|
|
|
What is cache hit rate in inling and why does too much inlining can also reduce your instruction cache hit rate, thus reducing the speed of
instruction fetch from that of cache memory to that of primary memory.
|
|
|
|
|
Where did that question come from?
Are you talking about registers?
Inlining is basically like macros, where it injects the instructions into the code instead of calling the actual function, which could produce bloating, but will reduce the cost of calling a function.
- S
50 cups of coffee and you know it's on!
|
|
|
|
|
Steve Echols wrote: Where did that question come from?
The same place that all of his questions come from. There seems to be no rhyme or reason behind them, other than for the sake of asking a question.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
"Cache hit rate inlining"
I would describe it as a technique used to avoid conditional jump instructions and reducing function call overhead.
Here are some reasons how/why __inline functions can possibly make code faster.
1.) In machine code when you conditionally jump from one address to another, the cpu branch predictor may choose the wrong path and will not prefetch the correct instructions. This is known as a cache miss.
More on cpu branch prediction:http://en.wikipedia.org/wiki/Branch_prediction[^]
2.) There 2 types of function linkages in machine code. The first is a leaf function. A leaf function is a function that does not call any other function. Its the end of the line or 'A leaf' like on a tree. This function may (possibly)be linked differently than other functions by the compiler (it may be inlined depending on the choice of the compiler). The second type of function is a non-leaf function which is calling other functions inside its body. It looks visually more like a tree branch with other branches or leaves atatched. (At least in my twisted mind!)
Now... When you call a non-leaf function linkage (most leaf too), the first thing that happens in __asm code is the function-prologue. The stack and registers are prepared for usage. http://en.wikipedia.org/wiki/Function_prologue[^]
As you can imagine... this requires some cpu cycles... which = time.
After the function has executed... the function must restore the stack and registers to its previous state... so the function who called it can continue processing! Otherwise the function which called function 2 would be completely confused. All of its registers and the stack would be different!
Here is an example from a project I am currently working on demonstrating a prologue and epilogue in a function.
A function which has been __inlined (if it was actually inlined) does not have this prologue/epilogue overhead.
Now you also asked how __inlining functions can cause increased cache misses. Well, __inline functions make code much larger. The increase in code size may cause a small, critical section of code to no longer fit in the cache, and this will cause cache misses!
Hope this helps,
-Randor (David Delaune)
|
|
|
|
|
Why is writing a function that returns a dereferenced pointer is a memory leak just waiting to happen. The function here initializes a reference to the object returned. In that case is the only alternative left is to return objects by value. When is it that objects are returned by reference and when is it that they are returned by value.
|
|
|
|
|
first of all, you never return a reference to an auto object( one created on the stack) because once you leave that scope, the object self destructs and you're left with a reference to nothing!
also you should not create an object on the heap and then return a reference to that object, because then you will have a memory leak because the allocated memory is never freed, the object done not self destruct when the reference goes out of scope!!!
would be cool if it did, but it doesn't.
so return a object on the stack, make sure the object can copy itself correctly if you're going to return objects by values, that is make sure the object has a copy constructor that does all the right things if you're doing anything specialized with that object, otherwise the complier will try to do a good job in trying to copy only auto class members, for pointer only the what the pointer points to will get copied but not what is pointer do! this is call a shallow copy....a deep copy is something that makes a copy of the pointer to things, but you will not have to worry about this when you're returning objects by value.
you will have to worry about this if you're want to clone an object, then you need to do a deep copy!
Yours Truly, The One and Only!
|
|
|
|
|
Why is a reference then returned in case of assigment operator where a dereferenced pointer is returned?
|
|
|
|