存档

‘Programming’ 分类的存档

函数指针定义解读

2010年9月19日 Jarod Lee 没有评论

原则:从中间开始到两边结束
中间开始指的是从中间定义的变量开始,
到两边结束:指的是从中间变量开始先右后左的原则依次解读
sample: void (*pFuncPtr)()
中间开始:定义一个pFuncPtr,
先右后左,右边是个右括号,忽略之, 左边是个*说明pFuncPtr是个指针,然后右边是个(), 说明指针指向一个没有参数的函数,然后在左边是void说明指向
的函数的返回类型是void

“从中间开始” (pFuncPtr是一个…), 到右边(无意义的右括号), 左边“*”(指针,指向。。。), 右边-空的参数列表(“ 一个没有参数的函数),
左边void(pFuncPtr是一个指针, 指向带无参的返回类型为void函数)

void *pFuncPtr()
pFuncPtr是一个没有参数的函数,返回类型为void*

复杂的声明和定义
1。 void * (*(*fp1)(int))[10]

fp1是一个函数指针, 指向点一个int参数的函数,该函数返回一个指针,该指针指向一个有10个void*指针元素的数组

2. float (*(*fp2)(int, int, float))(int);

fp2是一个函数指针,指向一个参数列表为(int, int, float)的函数,该函数返回一个函数指针,指向一个参数为(int)返回类型为float的指针

3. typedef double (*(*(*fp3)())[10])();
fp3 a;
fp3是一个函数指针指向一个无参的函数, 返回类型是一个指针,指向一个有十个数组的元素,其中每个元素是一个函数指针指向一个没有参数的返回类型
为double的函数

4 int (*(*f4())[10])();
f4是一个函数返回一个指针,指向一个有十个元素的数组,每个数组元素为一个函数指针,指向一个带无参返回类型为int的函数

分类: C&&C++ 标签:

Packaging and Deploying Resources

2010年5月12日 Jarod Lee 没有评论

he .NET Framework uses a hub and spoke model to package and deploy resources. The hub is the main assembly that contains the nonlocalizable executable code and the resources for a single culture, called the neutral or default culture. The default culture is the fallback culture for the application. Each spoke connects to a satellite assembly that contains the resources for a single culture, but does not contain any code.

There are several advantages to this model:

  • You can incrementally add resources for new cultures after you have deployed an application. Because subsequent development of culture-specific resources can require a significant amount of time, this allows you to release your main application first, and deliver culture-specific resources at a later date.

  • You can update and change an application’s satellite assemblies without recompiling the application.

  • An application needs to load only those satellite assemblies that contain the resources needed for a particular culture. This can significantly reduce the use of system resources.

However, there are also disadvantages to this model:

  • You must manage multiple sets of resources.

  • The initial cost of testing an application increases, because you must test several configurations. Note that in the long term it will be easier and less expensive to test one core application with several satellites, than to test and maintain several parallel international versions.

 

Detail in http://msdn.microsoft.com/en-us/sb6a8618.aspx

分类: .Net 标签:

Static Variable

2010年4月14日 Jarod Lee 没有评论

In the C programming language and its descendants, the term static variable has at least three separate meanings, each related to the semantics of C’s static keyword:

  • Static global variables are declared as “static” at the top level of a source file. Such variables are not visible outside the source file (“file scope”), unlike variables declared as “extern”.
  • Static local variables are declared inside a function, just like automatic local variables. They have the same scope as normal local variables, differing only in “storage duration”: whatever values the function puts into static local variables during one call will still be present when the function is called again.
  • C++ has static member variables: in classes, member variables declared as “static” are class variables (as opposed to instance variables).

Static (C++)

The static keyword can be used to declare variables, functions, class data members and class functions.

By default, an object or variable that is defined outside all blocks has static duration and external linkage. Static duration means that the object or variable is allocated when the program starts and is deallocated when the program ends. External linkage means that the name of the variable is visible from outside the file in which the variable is declared. Conversely, internal linkage means that the name is not visible outside the file in which the variable is declared.

The static keyword can be used in the following situations.

  • When you declare a variable or function at file scope (global and/or namespace scope), the static keyword specifies that the variable or function has internal linkage. When you declare a variable, the variable has static duration and the compiler initializes it to 0 unless you specify another value.
  • When you declare a variable in a function, the static keyword specifies that the variable retains its state between calls to that function.
  • When you declare a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. A static data member must be defined at file scope. An integral data member that you declare as conststatic can have an initializer.
  • When you declare a member function in a class declaration, the static keyword specifies that the function is shared by all instances of the class. A static member function cannot access an instance member because the function does not have an implicit this pointer. To access an instance member, declare the function with a parameter that is an instance pointer or reference.
  • You cannot declare the members of a union as static. However, a globally declared anonymous union must be explicitly declared static.

For more information, see auto, extern, and register.

Example


The following example shows how a variable declared static in a function retains its state between calls to that function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// static1.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;
void showstat( int curr )
{
   static int nStatic;    // Value of nStatic is retained
   // between each function call
   nStatic += curr;
   cout << "nStatic is " << nStatic << endl;
}

int main()
{
    for ( int i = 0; i &lt; 5; i++ )
        showstat( i );
}

Output:

nStatic is 0
nStatic is 1
nStatic is 3
nStatic is 6
nStatic is 10

The following example shows the use of static in a class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// static2.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;
class CMyClass
{
public:
   static int m_i;
};

int CMyClass::m_i = 0;
CMyClass myObject1;
CMyClass myObject2;

int main()
{
   cout << myObject1.m_i << endl;
   cout << myObject2.m_i << endl;

   myObject1.m_i = 1;
   cout << myObject1.m_i << endl;
   cout << myObject2.m_i << endl;

   myObject2.m_i = 2;
   cout << myObject1.m_i << endl;
   cout << myObject2.m_i << endl;

   CMyClass::m_i = 3;
   cout << myObject1.m_i << endl;
   cout << myObject2.m_i << endl;
}

Output:

0
0
1
1
2
2
3
3

The following example shows a local variable declared static in a member function. The static variable is available to the whole program; all instances of the type share the same copy of the static variable.

NoteNote

Assigning a value to a static local variable in a multithreaded application is not thread safe and we do not recommend it as a programming practice.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// static3.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
struct C {
   void Test(int value) {
      static int var = 0;
      if (var == value)
         cout << "var == value" << endl;
      else
         cout << "var != value" << endl;

      var = value;
   }
};

int main() {
   C c1;
   C c2;
   c1.Test(100);
   c2.Test(100);
}
var != value
var == value
static

Normally, objects are placed on the stack. Memory is allocated by growing the stack at the top; thus objects are destroyed in the reverse order in which they are created. An object’s life span is the same as its scope. If an object comes into scope more than once, then it is reinitialized each time, and destroyed when leaving its scope.

  +----------+
  |          |
  |   Heap   |  Allocated with new until deleted or program exits.
  |          |
  +^^^^^^^^^^+
  |   Stack  |  Local objects, parameters, temporaries, function return addresses.
  +----------+     +---------+
  |   Data   | <-- |  Data   |  Initial values for static and global objects.
  +----------+     +---------+
  |   Code   | <-- |  Code   |  Executable machine instructions.
  +----------+     +---------+  (Cannot be read or written by program.)
  | Reserved |    a.out on disk
  | for OS   |
  | and other|  Cannot be read or written by program, will cause segmentation
  | programs |  fault or general protection fault.
  +----------+
     Memory

static objects are placed in the data segment. They are initialized from values stored in the executable file, and therefore these values must be known at compile time. Initialization occurs only once. Values are maintained when the object is out of scope (e.g. between function calls), and it is safe to return a pointer or reference to them. Numeric values not explicitly initialized are set to 0.

1
2
3
4
5
6
7
8
9
10
11
12
 // Return by reference, f() is an alias for s, not a temporary copy
int& f() {      
   static int s=1; // Initialized only once
   ++s;
   return s;       // Safe to return by reference
}
int main() {
   cout << f();    // 2
   cout << f();    // 3
   f()=5;          // OK, s=5;
   s=6;            // Error, s is not in scope
}
1
2
xxxx
bbb
分类: C&&C++ 标签:

[MFC]Device Contexts

2010年4月9日 Jarod Lee 没有评论

A device context is a Windows data structure containing information about the drawing attributes of a device such as a display or a printer. All drawing calls are made through a device-context object, which encapsulates the Windows APIs for drawing lines, shapes, and text. Device contexts allow device-independent drawing in Windows. Device contexts can be used to draw to the screen, to the printer, or to a metafile.

CPaintDC objects encapsulate the common idiom of Windows, calling the BeginPaint function, then drawing in the device context, then calling the EndPaint function. The CPaintDC constructor calls BeginPaint for you, and the destructor calls EndPaint. The simplified process is to create the CDC object, draw, and destroy the CDC object. In the framework, much of even this process is automated. In particular, your OnDraw function is passed a CPaintDC already prepared (via OnPrepareDC), and you simply draw into it. It is destroyed by the framework and the underlying device context is released to Windows upon return from the call to your OnDraw function.

CClientDC objects encapsulate working with a device context that represents only the client area of a window. The CClientDC constructor calls the GetDC function, and the destructor calls the ReleaseDC function. CWindowDC objects encapsulate a device context that represents the whole window, including its frame.

CMetaFileDC objects encapsulate drawing into a Windows metafile. In contrast to the CPaintDC passed to OnDraw, you must in this case call OnPrepareDC yourself.

分类: MFC 标签: ,

Favov object composition over inheritance

2010年3月24日 Jarod Lee 没有评论

The other major concept you should recognize is that of object composition. This is simply the construction of objects that contain others:
encapsulation of several objects inside another one. While many beginning
OO programmers use inheritance to solve every problem, as you begin to
write more elaborate programs, you will begin to appreciate the merits of
object composition.Your new object can have the interface that is best for
what you want to accomplish without having all the methods of the parent
classes. Thus, the second major precept suggested by Design Patterns is
Favor object composition over inheritance.

Program to an interface and not to an implementation

2010年3月24日 Jarod Lee 没有评论

Putting this more succinctly, you should define the top of any class
hierarchy with an abstract class or an interface, which implements no
methods but simply defines the methods that class will support. Then in all
of your derived classes you have more freedom to implement these
methods as most suits your purposes.

How to set Visual Studio 2008′s default “company name”

2010年3月2日 Jarod Lee 没有评论

Option 1: Modify the registry info
32bit OS: HKLMSoftwareMicrosoftWindows NTCurrentVersionRegisteredOrganization
64bit OS: HKLMSoftwareWow6432NodeMicrosoftWindows NTCurrentVersionRegisteredOrganization
It’s according to the following MSDN library documentation:
#Template Parameters

http://msdn.microsoft.com/en-us/library/eehb4faa.aspx

For the AssemlyCompany (CompanyName) in AssemblyInfo, it’s $registeredorganization$ which uses value from registry key value from HKLMSoftwareMicrosoftWindows NTCurrentVersionRegisteredOrganization.
(For 64 bit OS, the registry key is HKLMSoftwareWow6432NodeMicrosoftWindows NTCurrentVersionRegisteredOrganization)
Option 2: Modify the template file

Open the template package and customize assemblyInfo file. Please remember to customize this file both in the ProjectTemplates folder and ProjectTemplatesCache folder.

For example:
For VB.NET Winform project, I should modify assemblyInfo file in:
…Microsoft Visual Studio 9.0Common7IDEProjectTemplatesVisualBasicwindows1033WindowsApplication.zip
and
…Microsoft Visual Studio 9.0Common7IDEProjectTemplatesCacheVisualBasicWindows1033WindowsApplication.zip

分类: .Net 标签:

BackgroundWorker Component Overview

2010年2月9日 Jarod Lee 没有评论

There are many commonly performed operations that can take a long time to execute. For example:

  • Image downloads
  • Web service invocations
  • File downloads and uploads (including for peer-to-peer applications)
  • Complex local computations
  • Database transactions
  • Local disk access, given its slow speed relative to memory access

Operations like these can cause your user interface to hang while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the BackgroundWorker component provides a convenient solution.

The BackgroundWorker component gives you the ability to execute time-consuming operations asynchronously (“in the background”), on a thread different from your application’s main UI thread. To use a BackgroundWorker, you simply tell it what time-consuming worker method to execute in the background, and then you call the RunWorkerAsync method. Your calling thread continues to run normally while the worker method runs asynchronously. When the method is finished, the BackgroundWorker alerts the calling thread by firing the RunWorkerCompleted event, which optionally contains the results of the operation.

The BackgroundWorker component is available from the Toolbox, in the Components tab. To add a BackgroundWorker to your form, drag the BackgroundWorker component onto your form. It appears in the component tray, and its properties appear in the Properties window.

To start your asynchronous operation, use the RunWorkerAsync method. RunWorkerAsync takes an optional object parameter, which can be used to pass arguments to your worker method. The BackgroundWorker class exposes the DoWork event, to which your worker thread is attached through a DoWork event handler.

The DoWork event handler takes a DoWorkEventArgs parameter, which has an Argument property. This property receives the parameter from RunWorkerAsync and can be passed to your worker method, which will be called in the DoWork event handler. The following example shows how to assign a result from a worker method called ComputeFibonacci. It is part of a larger example, which you can find at How to: Implement a Form That Uses a Background Operation.

分类: .Net 标签:

Best Vim Tips

2010年2月9日 Jarod Lee 没有评论
分类: Linux/Unix, Programming, Windows 标签:

解决英文环境下gvim的乱码问题

2010年2月8日 Jarod Lee 没有评论

1. 如果包含的文本中有宽字符集的字体,例如中文,日文,韩文等等,这时候需要设置文件的encoding为utf-8
设置命令 :set encoding=utf-8
2. 设置完成后,如果还是不能正常显示,说明当前的字体不支持,可以通过设置字体解决,由于unicode属于宽字符集,设置gvim的宽字体即可。
设置命令 set guifontwide=DotumChe:h12:cANSI

参考:gvim同时处理中日韩文

分类: Programming, Windows 标签: