gpt4 book ai didi

c++ - 类外的成员函数定义

转载 作者:太空狗 更新时间:2023-10-29 19:57:10 27 4
gpt4 key购买 nike

是否可以在类声明之外定义函数或方法?如:

class A 
{
int foo;
A (): foo (10) {}
}

int A::bar ()
{
return foo;
}

最佳答案

可以在类外部定义但不声明方法,类似于在 C 中创建函数原型(prototype)然后稍后定义它们的方式,即:

class A 
{
int foo;
A (): foo (10) {}
int bar();
}

// inline only used if function is defined in header
inline int A::bar () { return foo; }

关于c++ - 类外的成员函数定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39921926/

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