有没有一种方法可以使类递归?

时间:2019-02-10 20:21:57

标签: c++ class recursion

因此,我想创建一个类,该类可以具有本身是类型的对象。 像这样:

class foo {

    foo Avalue = foo();
    foo Bvalue = foo();

    foo(int a, int b) {
        Avalue = goo(a);
        Bvalue = goo(b);
    }

    foo(foo a, int b) {
        Avalue = foo(a);
        Bvalue = goo(b);
    }

    foo(foo a, foo b) {
        Avalue = foo(a);
        Bvalue = foo(b);
    }
}

class goo : foo {

    int value;
}

,以便我可以拥有一个总是以“ goo”对象终止的递归对象。 有办法做到吗?

2 个答案:

答案 0 :(得分:6)

不。从根本上讲这是不可能的。想一想:该类将具有无限的大小(嗯,我想除非它没有其他成员,否则它将做什么?),并且没有可证明的定义/身份。

不过,您可以存储指向其他Result http_download_save(char *url, char *outfile) { Result ret=0; httpcContext context; char *newurl=NULL; u32 statuscode=0; u32 contentsize=0, readsize=0, size=0; u8 *buf, *lastbuf; do { ret = httpcOpenContext(&context, HTTPC_METHOD_GET, url, 1); // This disables SSL cert verification, so https:// will be usable ret = httpcSetSSLOpt(&context, SSLCOPT_DisableVerify); // Enable Keep-Alive connections ret = httpcSetKeepAlive(&context, HTTPC_KEEPALIVE_ENABLED); // Set a User-Agent header so websites can identify your application ret = httpcAddRequestHeaderField(&context, "User-Agent", "httpc-example/1.0.0"); // Tell the server we can support Keep-Alive connections. // This will delay connection teardown momentarily (typically 5s) // in case there is another request made to the same server. ret = httpcAddRequestHeaderField(&context, "Connection", "Keep-Alive"); ret = httpcBeginRequest(&context); if(ret!=0){ httpcCloseContext(&context); if(newurl!=NULL) free(newurl); return ret; } ret = httpcGetResponseStatusCode(&context, &statuscode); if(ret!=0){ httpcCloseContext(&context); if(newurl!=NULL) free(newurl); return ret; } if ((statuscode >= 301 && statuscode <= 303) || (statuscode >= 307 && statuscode <= 308)) { if(newurl==NULL) newurl = (char*)malloc(0x1000); // One 4K page for new URL if (newurl==NULL){ httpcCloseContext(&context); return -1; } ret = httpcGetResponseHeader(&context, "Location", newurl, 0x1000); url = newurl; // Change pointer to the url that we just learned printf("redirecting to url: %s\n",url); httpcCloseContext(&context); // Close this context before we try the next } } while ((statuscode >= 301 && statuscode <= 303) || (statuscode >= 307 && statuscode <= 308)); if(statuscode!=200){ printf("\x1b[31mAn Error Occured\x1b[0m\n"); httpcCloseContext(&context); if(newurl!=NULL) free(newurl); return -2; } // This relies on an optional Content-Length header and may be 0 ret=httpcGetDownloadSizeState(&context, NULL, &contentsize); if(ret!=0){ httpcCloseContext(&context); if(newurl!=NULL) free(newurl); return ret; } // Start with a single page buffer buf = (u8*)malloc(0x1000); if(buf==NULL){ httpcCloseContext(&context); if(newurl!=NULL) free(newurl); return -1; } do { // This download loop resizes the buffer as data is read. ret = httpcDownloadData(&context, buf+size, 0x1000, &readsize); size += readsize; if (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING){ lastbuf = buf; // Save the old pointer, in case realloc() fails. buf = (u8*)realloc(buf, size + 0x1000); if(buf==NULL){ httpcCloseContext(&context); free(lastbuf); if(newurl!=NULL) free(newurl); return -1; } } } while (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING); if(ret!=0){ httpcCloseContext(&context); if(newurl!=NULL) free(newurl); free(buf); return -1; } FILE* out = fopen(outfile, "w"); fwrite(buf, 1, size, out); fclose(out); httpcCloseContext(&context); free(buf); if (newurl!=NULL) free(newurl); return 0; } 对象的指针。只要不是每个foo都有一个确实指向指向另一个foo的成员指针,或者如果引用形成循环依赖关系,此方法就起作用。无论哪种方式,编译器都不会以尝试的解决方案所必须的方式进行诊断,但是如果您不小心,可能会在运行时发现问题。

您的代码表明您正在实现一棵树。这就是(通常是一棵树)foo(通常是一棵树)动态创建其节点并将其与指针链接的原因(我想是其中之一)。就像每个链表的实现一样。

答案 1 :(得分:2)

问题在于,foo中的foo将包含一个foo,其中将包含一个foo,其中将包含一个foo,其中将包含foo将包含foo,其中将包含foo,其中将包含foo,其中将包含foo,其中foo将包含{{1} },其中将包含foo,其中将包含foo,其中将包含foo,其中将包含foo,其中将包含foo,其中将包含{ foo将包含foo,其中将包含foo,其中将包含foo,其中将包含foo,其中foo将包含let test = marketDailyData.timeSeriesDaily[0] //produce error "Cannot subscript a value of type 'Dictionary<String, TimeSeriesDaily>.Values' with an index of type 'Int'" 其中将包含...

相关问题