读书人

第一次发现求解释,该如何解决

发布时间: 2012-03-15 11:50:38 作者: rapoo

第一次发现,求解释

C/C++ code
#include "stdafx.h"#include <iostream>using namespace std;class M //C++11 delegating constructors{    int x, y;    char *p;public:    M()    {        M(10);    };    M(int v) : x(v), y(0),  p(new char [0])  {    } //#1 target    };int main(int argc, char* argv[]){    M m();   // M m    printf("Hello World!\n");    return 0;}


M m();是个什么情况,编译通过了,确实无效代码
写成 int m();也一样...写错了,但是编译器怎么解释的,也没有报错...VC6编译的

[解决办法]
int m(3);就没问题了
[解决办法]
warning C4930: “M m(void)”: 未调用原型函数(是否是有意用变量定义的?)
C/C++ code
#include <iostream>#include <string>using namespace std;class M //C++11 delegating constructors{    int x, y;    char *p;public:    M()    {        printf("默认构造函数\n");        M(10);//还有这个地方原则上是不会有任何作用的 需要自定义=运算符然后赋值    };    M( const M &m )    {        printf("拷贝构造函数\n");        x = m.x;        y = m.y;        p = new char[strlen(m.p)+1];        strcpy(p,m.p);    }    M(int v) : x(v), y(0),  p(new char [1])      {        printf("带参数构造函数\n");    } //#1 target    int Getx()const    {        return x;    }    };int main(int argc, char* argv[]){    M m();   // M m    printf("Hello World!\n");    M n;    printf("%d\n",n.Getx());    return 0;} 

读书人网 >C++

热点推荐