c++ - 我如何能够将不完整的类本身用作参数?

标签 c++

struct test{
 void call(test t1){
   //
 }
};

我问了一个类似的问题,并找到了一些其他链接,例如:

How am I able to use a class as a function parameter in itself?

Incomplete types in member function definitions

但他们都没有回答这个问题:

编译器需要为 t1 发出代码到堆栈上分配的空间,但在那 点 test 不完整,它怎么知道它需要多少空间?

最佳答案

struct test 类不是不完整类型。

来自 cppreference.com :

Incomplete type

  • the type void (possibly cv-qualified);
  • incompletely-defined object types
    • class type that has been declared (e.g. by forward declaration) but not defined;
    • array of unknown bound;
    • array of elements of incomplete type;
    • enumeration type from the point of declaration until its underlying type is determined.

All other types are complete.

自己测试一下,在你的 call 函数中做一个测试,比如:

void call(test t1)
{
    std::cout << sizeof t1 << std::endl;
}

关于c++ - 我如何能够将不完整的类本身用作参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70684028/

相关文章:

python - 简单的 boost::python 程序崩溃

c++ - LinkedList ADT指针 block

c++ - 如何在 Opencv 中处理图像的特定部分

C++ fstream 多输入文件

c++ - LLVM - 如何将 ConstantExpr 转换为 ConstantDataArray 以便打印全局变量 char* 的值?

c++ - STL 字符串中的别名

c++ - 如何修复 "no suitable conversion function from "字符串“到 "const char *"存在”?

c++ - 析构顺序

C++17 模板推导指南不用于空参数集(ver 2)?

c++ - 如何比较 clang 中的两个源位置?