Aug 3, 2011

Installing APC (3.1.6) on WAMP (64 bit)

My dev machine has 64bit wampserver installed on windows 7 (MSVC9 (Visual C++ 2008) , Thread Safe, x64). I wanted a caching solution that works well with this setup – As of this writing, APC is the only 64 bit option available.
Get the precompiled TS dll from here (In the comments posted on this page, you will find a link to php_apc.dll without memprotect). Save it in your php “ext” directory.
Add the following line to your php.ini file :
extension=php_apc.dll
Restart apache web server..browse your phpinfo page, and it should have a section for APC – The defaults work fine for a test server setup.
Next, setup the admin page – this will help you view/clear your variable cache(s):
  • Download apc.php from here
  • Save it in your webfolder as apc.php
  • Edit apc.php. Change the admin password (from the default ‘password’ to any string of your liking):
defaults(‘ADMIN_USERNAME’,'apc’);             // Admin Username
defaults(‘ADMIN_PASSWORD’,'pAssW0rd’);      // Admin Password – CHANGE THIS TO ENABLE!!!

Install PHP 5.3.6 on WAMP

I successfully installed PHP 5.3.6 on my WAMP server under Windows 7 64 bit. But this short tutorial will probably work on other versions as well.
Go to the Windows download page at PHP.net and download the desired version. You must select the ZIP VC6 x86 Thread Safe variant.
Unpack the contents to the PHP bin directory in your WAMP installation. I unpacked PHP 5.3.6 in C:\wamp\bin\php\php5.3.6\.
You should now copy the following files from the php5.3.0 directory, to your newly created directory (php5.3.6 in my case): php.ini, phpForApache.ini and wampserver.conf.
Now we just need to make a quick edit in both INI-files. Search for "5.3.0" and replace it with the PHP version you are installing.
Save the files, and you should see the new version in the PHP version menu in WAMP. If not, right-click the WAMP icon and choose "Exit", then start it again.

Jul 17, 2011

网上收集的C++笔试题目(10)

【Q1】Which of the following must be ensured in order to implement a polymorphic function in C++?

    A.    There has to be a pointer of the derived class that has implemented the polymorphic function that holds the address of the derived class object.

    B.    The function must be declared as virtual in both the base class and in the derived class that overrides the function.

    C.    The function must be declared as pure virtual.

    D.    There has to be a base class pointer holding the address of a base or derived class object that has implemented the polymorphic function.

    E.    The function must be declared as virtual in the base class.

【Q2】Which of the following statements accurately describe what it means for a class to grant friend status to a function in C++?

    A. The function has been granted access to all the members of the class.

    B.    The function has been granted access only to the protected data and functions of the class.

    C.    The function has been granted access only to the private functions of the class.

    D.    The function has been granted access only to the protected functions of the class.

    E.    The function has been granted access only to the private data and functions of the class.

【Q3】A C++ developer wants to further throw an exception that is caught in an exception variable excep. Which of the following statements can the developer use to achieve


    A. throw;

    B.  catch excep;

    C. next;

    D. throw next;

    E.  throw excep;

【Q4】What's the output of following C++ code excerpt?
#include <iostream>

 template<class T> class Some
 {
   public:
    static int stat;
 };

 template<class T>
 int Some<T>::stat = 10;

 int main(void)
 {
    Some<int>::stat = 5;
    std::cout << Some<int>::stat   << std::endl;
    std::cout << Some<char>::stat  << std::endl;
    std::cout << Some<float>::stat << std::endl;
    std::cout << Some<long>::stat  << std::endl;
 }

    A.    The output will be:
    5
    10
    10
    10
    B.    The output will be:
    5
    10
    10
    5
    C.    The output will be:
    5
    5
    5
    5
    D.    The output will be:
    10
    10
    10
    10
    E.    The output will be:
    10
    5
    5
    10

【Q5】When a Copy Constructor is NOT written for a class, the C++ compiler generates one. Which of the following statements correctly describe the actions of this compiler­generated Copy Constructor when invoked?

    A.    The compiler­generated Copy Constructor performs a member­wise copy of the object passed to it as an argument, into the object being constructed.

    B.    The compiler­generated Copy Constructor makes the object being constructed, a reference to the object passed to it as an argument.

    C.    The compiler­generated Copy Constructor tags the object as having been Copy­Constructed by the compiler.

    D.    The compiler­generated Copy Constructor does not do anything by default.

    E.    The compiler­generated Copy Constructor invokes the assignment operator of the class.

【Q6】The variables x, y, and z are integer variables that have been properly declared and initialized in C++. Which of the following statements can call the macro below without
 cted results?

 #define YEAR_LENGTH 365
 #define MONTH_LENGTH 30
 #define DAYCALC(y, m, d) ((y * YEAR_LENGTH) + (m * MONTH_LENGTH) + d)

    A. DAYCALC(x * 3, y % 3, z);

    B.  DAYCALC(x, 40­y, 3+z);

    C. DAYCALC(x+12, y, 300);

    D. DAYCALC(x, y, (z + 50));

    E.  DAYCALC(4 % x, y++, z);

【Q7】Which of the following are valid statements considering the C++ code snippet below?

 int foo(longg l);
 //...
 int i = 12;
 foo(i);

    A.    The function foo is invoked with an int that has a value of "12".

    B.    The function foo is invoked with a long that has an undefined value.

    C.    The compiler instantiates a new function that accepts an int instead of a long.

    D.    The function foo is invoked with a long that has a value of "12".

    E.  i is promoted to a long to invoke the function foo.

【Q8】Which of the following are accurate statements concerning the behavior of the dynamic_cast operator in C++?

    A. The dynamic_cast operator fails if used for upcasting.

    B.    The dynamic_cast operator returns the object's address if the operation succeeds.

    C.    The dynamic_cast operator may return 0 if the operation fails.

    D.    The dynamic_cast operator's behavior is a subset of the reinterpret_cast operator's behavior.

    E.    The dynamic_cast operator throws an invalid_cast exception if the operation fails.

【Q9】A C++ developer wants to convert a user­defined type into a basic data type. Which of the following methods can the developer use to do this?

    A.    Write a conversion function to convert the user­defined class to a built­in data type.

    B.    Write a constructor that accepts the user­defined type as an argument.

    C.    Write a virtual constructor.

    D.    Write a constructor that accepts the basic data type as an argument.

    E.    Overload the assignment operator for the user­defined class.

【Q10】For the C++ code below, which of the following assignments for r will produce the value "6"?

  int r = 0;
 int arr[5] = {2,4,6};
 int* p     = arr;

    A. r = p[2];

    B.  r = *(arr+2);

    C. r = *arr[2];

    D. r = *(arr+*p);

    E.  r = p+2;

【Q11】Considering the C++ program below, what should be inserted in place of //***** to ensure a 100% clean shutdown?

 #include <iostream>
 #include <fstream>

 int main(int argc, char* argv[])
 {
     using namespace std;
    
     fstream log("log.txt", ios::out);
     streambuf* clog_buf = clog.rdbuf(log.rdbuf());
    
     clog << "Test the logger" << endl;
       
     //*****
 }

    A.    Nothing is missing.

    B.  exit();

    C. clog.rdbuf(clog_buf);

    D. clog.rdbuf(0);

    E.  log.rdbuf(0);

网上收集的C++笔试题目(9)

【Q1 】Which of the following options correctly describe the relationship between a derived class and its base class in C++?
    A.    Isamemberof relationship

    B.    Is­a relationship

    C.    Has­a­kind­of relationship

    D.    Has­a relationship

    E.    Becomes­a relationship

【Q2】 Which of the following statements correctly describe C++ references?

    A.    A refrence may refer to a different object by assignment.

    B.    A reference must be initialized when it is declared.

    C.    The operator ­> must be used to access the referenced object.

    D.    A reference creates an alias to another object.

    E.    A reference remains valid irrespective of the scope or lifetime of the referenced object.

【Q3】Which of the follow describe appropriate scenarios for the using the C++ volatile type­modifer?

    A.    When a variable cannot be changed

    B.    When a variable can be changed asynchronously by hardware

    C.    When a variable can only be changed by the program normally

    D.    When a variable can be changed asynchronously by another thread

    E.    When a variable can be changed only once

【Q4】 A C++ class named SomeClass has a public static int member named data. An object of SomeClass named objSome is declared. Which of the following statements
  can access to data?


    A. SomeClass::objSome.data

    B.  objSome.data

    C. SomeClass::data

    D. objSome::data

    E.  SomeClass<static int>.data

【Q5】Which of the following C++ code snippets can a developer use to open a file SomeFile.txt with an instance outfile of class ofstream?
    A. std::ofstream outfile;
    outfile.read("SomeFile.txt");
    B.  std::ofstream outfile("SomeFile.txt");

    C. std::ofstream outfile;
    outfile.open("SomeFile.txt");
    D. std::ofstream outfile;
    outfile.open("SomeFile.txt", std::ios::out);
    E.  std::ofstream outfile;
    outfile.read("SomeFile.txt", std::ios::out);

【Q6】Which of the following operators have a unary form in C++?

    A. !

    B.  %

    C. ++

    D. -

    E.  []

【Q7】  Which of the following identify a valid function prototype for a C++ function named Display, which receives a STL vector object as an argument?
    A. template <class T> void Display(std::vector& obj);

    B. template <class T> void Display<T>(std::vector<T>& obj);

    C. template <class T> void Display(std::vector<T>& obj);

    D. template <class T> void Display<T>(std::vector& obj);

    E. template <class T, class Q> void Display<T>(std::vector<Q>& obj);

【Q8】 Which of the following statements describe the result when standard new CANNOT allocate the requested storage in C++?  (Note: older compilers may not implement
 ior).


    A.    It throws an insufficient_memory exception.

    B.    It throws a bad_alloc exception.

    C.    It returns silently with no effect on the expression.

    D.    It returns null.

    E.    It logs an error message to the mem_err.log file.

【Q9】Which of the following are possible results of building the C++ code below?
 Class
SomeClass
 {
     protected:
         int data;
         friend class AnotherClass;
 };

 void SomeFunc(SomeClass sc)
 {
     sc.data = 5;
 }

 class AnotherClass
 {
     public:
         void Another(SomeClass sc)
         {
             sc.data = 25;
         }
         friend void SomeFunc(SomeClass sc);
 };

 int main(void)
 {
     SomeClass sc;
     SomeFunc(sc);
     cout << sc.data;
 }

    A.    Compiling generates an error because main() is not allowed to access the protected member data of SomeClass.

    B.    The build will be successful, and the program will display an output of "5" when it is executed.

    C.    Compiling generates an error because Another() of AnotherClass is not allowed to access the protected member data of SomeClass.

    D.    Compiling generates an error because SomeFunc() is not allowed to access the protected member data of SomeClass.

    E.    Compiling generates an error because AnotherClass is not allowed to declare SomeFunc() as a friend as it is already a friend of SomeClass.

【Q10】Which of the following are valid Container types (i.e. Container concepts) in the Standard Template Library?
    A.    ?

    B.    Associative

    C.    Random

    D.    Sequence

    E.    Linked
【Q11】Which of the following are valid outputs of executing the code below in C++?

 cout << setw(11) << 'A';

    A.    10 blank spaces followed by A

    B.    A with 5 blank spaces on either side

    C.    11A

    D.    11 As

    E.    A followed by 11 blank spaces



Jul 16, 2011

[ZT]Using Explicit in C++

In C++ it is possible to declare constructors for a class, taking a single parameter, and use those constructors for doing type conversion. For example:
class A {
        public:
                A(int);
        };

        void f(A) {}

        void g()
        {
                A a1 = 37;

                A a2 = A(47);

                A a3(57);

                a1 = 67;

                f(77);
        }
A declaration like:

A a1 = 37;
says to call the A(int) constructor to create an A object from the integer value. Such a constructor is called a "converting constructor".
However, this type of implicit conversion can be confusing, and there is a way of disabling it, using a new keyword "explicit" in the constructor declaration:
class A {
        public:
                explicit A(int);
        };

        void f(A) {}

        void g()
        {
                A a1 = 37;      // illegal

                A a2 = A(47);   // OK

                A a3(57);       // OK

                a1 = 67;        // illegal

                f(77);          // illegal
        }
Using the explicit keyword, a constructor is declared to be
"nonconverting", and explicit constructor syntax is required:

class A {
        public:
                explicit A(int);
        };

        void f(A) {}

        void g()
        {
                A a1 = A(37);

                A a2 = A(47);

                A a3(57);

                a1 = A(67);

                f(A(77));
        }
Note that an expression such as:
A(47)
is closely related to function-style casts supported by C++. For example:
double d = 12.34;

        int i = int(d);
 
From: http://www.glenmccl.com/tip_023.htm

Jul 14, 2011

[ZT]STL:函数对象

函数对象:就是起到函数作用的对象。特别是指重定义应用算符函数operator()的类对象。

尽管函数指针被广泛用于实现函数回调,但C++还提供了一个重要的实现回调函数的方法,那就是函数对象。函数对象(也称“算符”)是重载了“()”操作符的普通类对象。因此从语法上讲,函数对象与普通的函数行为类似。


用函数对象代替函数指针有几个优点,首先,因为对象可以在内部修改而不用改动外部接口,因此设计更灵活,更富有弹性。函数对象也具备有存储先前调用结果的 数据成员。在使用普通函数时需要将先前调用的结果存储在全程或者本地静态变量中,但是全程或者本地静态变量有某些我们不愿意看到的缺陷。


其次,在函数对象中编译器能实现内联调用,从而更进一步增强了性能。这在函数指针中几乎是不可能实现的。


下面举例说明如何定义和使用函数对象。首先,声明一个普通的类并重载“()”操作符:


class Negate


{


public:


int operator() (int n) { return -n;}


};


重载操作语句中,记住第一个圆括弧总是空的,因为它代表重载的操作符名;第二个圆括弧是参数列表。一般在重载操作符时,参数数量是固定的,而重载“()”操作符时有所不同,它可以有任意多个参数。


因为在Negate中内建的操作是一元的(只有一个操作数),重载的“()”操作符也只有一个参数。返回类型与参数类型相同-本例中为int。函数返回与参数符号相反的整数。


使用函数对象


我们现在定义一个叫Callback()的函数来测试函数对象。Callback()有两个参数:一个为int一个是对类Negate的引用。Callback()将函数对象neg作为一个普通的函数名:


#include


using std::cout;


void Callback(int n, Negate & neg)


{


int val = neg(n); //调用重载的操作符“()”


cout < < val;


}


不要的代码中,注意neg是对象,而不是函数。编译器将语句


int val = neg(n);


转化为


int val = neg.operator()(n);


通常,函数对象不定义构造函数和析构函数。因此,在创建和销毁过程中就不会发生任何问题。前面曾提到过,编译器能内联重载的操作符代码,所以就避免了与函数调用相关的运行时问题。


为了完成上面个例子,我们用主函数main()实现Callback()的参数传递:


int main()


{


Callback(5, Negate() ); //输出 -5


}


本例传递整数5和一个临时Negate对象到Callback(),然后程序输出-5。


模板函数对象


从上面的例子中可以看出,其数据类型被限制在int,而通用性是函数对象的优势之一,如何创建具有通用性的函数对象呢?方法是使用模板,也就是将重载的操作符“()”定义为类成员模板,以便函数对象适用于任何数据类型:如double,_int64或char:


class GenericNegate


{


public:


template T operator() (T t) const {return -t;}


};


int main()


{


GenericNegate negate;


cout < < negate(5.3333); // double


cout < < negate(10000000000i64); // __int64


}


如果用普通的回调函数实现上述的灵活性是相当困难的。


标准库中函数对象


C++标准库定义了几个有用的函数对象,它们可以被放到STL算法中。例如,sort()算法以判断对象(predicate object)作为其第三个参数。判断对象是一个返回Boolean型结果的模板化的函数对象。可以向sort()传递greater <> 或者less <> 来强行实现排序的升序或降序:


#include // for greater <> and less <>


#include //for sort()


#include


using namespace std;


int main()


{


vector vi;


//..填充向量


sort(vi.begin(), vi.end(), greater() );//降序( descending )


sort(vi.begin(), vi.end(), less() ); //升序 ( ascending )


}

Jul 13, 2011

[ZT]网上收集的C++笔试题目(8)

说明:下划线是我认为的答案,仅供参考。
【Q1】 In the given C++ code snippet, which of the following statements correctly identify how Mon of enum DOW can be assigned to a variable named var?
void main()
{
const int Mon = 1, Tue = 2;
enum DOW{ Mon = 11, Tue = 12 };
...

A.    The variable var will have to be of type enum DOW, and then defined as:
var = Mon;
B.    Mon of enum DOW cannot be assigned because the compiler will give a redefinition error.
C.    Since enum DOW has been declared and defined later, it will be pushed onto the stack later, but will be accessed first. Hence, the scope resolution operator is not required and var will be assigned as:
var = Mon;
D.    Since Mon is of type enum DOW, var will be assigned as:
var = DOW::Mon;
E.    The variable var must be of type const int, and then it can be assigned as:
var = static_cast<const int>(Mon);

【Q2】 For the code snippet below, which of the following statements provide the correct order of constructor calls when object obj is created in main()?
class Base
{
public:
Base(){cout << "In Base Ctor/n";

class Nest
{
public:
Nest(){cout << "In Nest Ctor/n"; }
};
};

class Derive : public Base
{
public:
Derive(){cout << "In Derive Ctor/n"; }
};

void main()
{
    Derive obj;
}

A.    Base constructor
Derive constructor
B.    Base constructor
Derive constructor
Nest constructor
C.    Base constructor
Nest constructor
Derive constructor
D.    Nest constructor
Base constructor
Derive constructor
E.    Derive constructor
Base constructor
Nest constructor

【Q3】 The C++ code below generates a compiler error. Which of the following solutions can be used to correctly access the member named nested?
class SomeClass
{
public:
int data;
protected:
class Nest
{
public:
int nested;
};
public:
static Nest* createNest(){return new Nest;}
};

void use_someclass()
{
SomeClass::Nest* nst = SomeClass::createNest();
nst->nested = 5;
}
A.    Make function void use_someclass() a friend of class SomeClass.
B.    Make the function createNest() a non-static function of SomeClass.
C.    Declare the class Nest in public scope of class SomeClass.
D.    Make the object nst a reference object, and make the function createNest() return a Nest&.
E.    Derive a class from SomeClass. Make the object nst a derived class pointer so that it can access SomeClass's protected declarations.

【Q4】 In C++, which of the following statements regarding the code below are valid?
#include <iostream>

class Outer
{
int m_o;
public:
class Inner
{
int m_i;
public:
Inner(){}
Inner(Outer m_outer, int x)
{
m_outer.m_o = x;
}
};

Outer(int y)
{
m_inner.m_i = y;
}

void Display()
{
using namespace std;
cout << m_o << endl
<< m_inner.m_i << endl;
}
Inner m_inner;
};

void main()
{
Outer    objOut(10);
Outer::Inner objIn(objOut, 5);
objOut.Display();
}

A.    The output will be:
5
10
B.    Outer class cannot access its nested class's private members.
C.    The relationship between class Inner and class Outer is not valid C++ syntax.
D.    Inner class cannot access its containing class's private members.
E.    The output will be:
10
5

【Q5】 What is the output of the program below in C++?
#include <iostream>
using namespace std;

class Object
{
public:
Object() {}

void Print() const
{
cout << "const" << endl;
}

void Print()
{
cout << "mutable" << endl;
}
};

void print_obj(const Object& obj)
{
obj.Print();
}

int main()
{
Object       obj1;
const Object obj2;
Object*const pobj1 = &obj1;

print_obj(obj1);
print_obj(obj2);

obj1.Print();
obj2.Print();

pobj1->Print();

return 0;
}

A.    const
const
mutable
const
const
B.    const
const
mutable
const
mutable
C.    mutable
const
mutable
const
mutable
D.    Undefined behavior
E.    Fails to compile

【Q6】 Which of the following are possible outputs of the C++ code below?
#include <iostream>

class TestPrint
{
public:

void Print()
{
std::cout << "TestPrint" << std::endl;
}

void Print() const
{
std::cout << "const TestPrint" << std::endl;
}

void Print() volatile
{
std::cout << "volatile TestPrint" << std::endl;
}

void Print() const volatile
{
std::cout << "const volatile TestPrint" << std::endl;
}
};

int main(int argc, char* argv[])
{
TestPrint normal_test;
normal_test.Print();

const TestPrint const_test;
const_test.Print();

volatile TestPrint volatile_test;
volatile_test.Print();

const volatile TestPrint const_volatile_test;
const_volatile_test.Print();
}

A.    TestPrint
const TestPrint
const volatile TestPrint
const volatile TestPrint
B.    TestPrint
const volatile TestPrint
const volatile TestPrint
const volatile TestPrint
C.    TestPrint
const TestPrint
volatile TestPrint
const volatile TestPrint
D.    TestPrint
TestPrint
TestPrint
TestPrint
E.    TestPrint
const volatile TestPrint
volatile TestPrint
const volatile TestPrint

【Q7】 Which exception handler will catch the exception throw by SomeFunction in C++?
#include <iostream>

class ExBase
{

};
class ExDer:public ExBase{};
void SomeFunction()
{
ExBase base;
throw base;
}

void main()
{
try
{
SomeFunction();
}
catch(ExDer& ex)
{
std::cout << "ExDer& handler/n";
}
catch(ExDer* ex)
{
std::cout << "ExDer* handler/n";
}
catch(ExBase& ex)
{
std::cout << "ExBase& handler/n";
}
catch(ExBase* ex)
{
std::cout << "ExBase* handler/n";
}
}

A.    ExBase& handler
B.    ExDer& handler
C.    ExBase* handler
D.    ExDer* handler
E.    The compiler will throw an ambiguity error as it will not able to determine which catch handler must be invoked.