gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:05:35 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com