笔试遇到的两个问题 (c、c++)

来源:互联网 发布:mac的废纸篓在哪 编辑:程序博客网 时间:2024/06/10 11:35

 

// 题目1  根据程序写结果

 

#include <cstdlib>

#include <iostream>

 

using namespace std;

 

class A

{

  public:

       A()

      {

            m_static++ ;

       }

 

       int getAValue()

       {

         return m_static  ;

       }

 

       int getValue()

       {

           static int m_LocalStatic=0 ;  // 静态变量   这种情况 还不多见

           m_LocalStatic++ ;

           return m_LocalStatic;

       }

private :               

  static int m_static ;

 

} ;

 

int A::m_static =  0;

 

int main(int argc, char *argv[])

{

    A a;

    cout<<a.getAValue();

    cout<<" "<<a.getValue()<<endl;

 

    A b ;

    cout<<b.getAValue();

    cout<<" "<<b.getValue()<<endl;  

 

    system("PAUSE");

    return EXIT_SUCCESS;

}

//DEV-C++ 中 结果为 :

1 1
2 2 
请按任意键继续. . .
// 题目2 根据 程序写结果
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
    int A[] = {0,2,3,4,5};
    
    int *p=(int*)(A+1);    //这里的1 是一个 siezof(int*)=4  的长度了 
    int *q=(int*)(&A+1);  // 这个就是理论上的A[5] 了  *p的值是不确定的了    这里的 1 是 sizeof(A)=20的长度了
       
    cout<<*(A+1)<<"/t"<<*p-1<<"/t"<<*(q-1)<<endl ;   
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
//在DEV-c++ 中的结果为:
2       1       5
请按任意键继续. . .
最后一个结果比较容易出错 呵呵

原创粉丝点击