gpt4 book ai didi

c++ - 类方法包含相同的类对象

转载 作者:太空宇宙 更新时间:2023-11-04 15:13:29 30 4
gpt4 key购买 nike

func1func2中是否合法?类方法包含相同的类对象。

例子:

class Foo
{
public:
void func1() {
//...
Foo foo; // Is it legal?
//...
}

// Is it legal in paramete?
void func2(Foo object) {
//...
}

// It is legal using reference
// http://stackoverflow.com/questions/6921185/why-do-objects-of-the-same-class-have-access-to-each-others-private-data
void func3(Foo& object) {
//
}
};

最佳答案

Is it legal?

是的

Why?

class Foo
{
public:
void func1() {
//...
Foo foo; // Is it legal?
//...
}
};

类成员函数的主体是在考虑了整个类定义之后编译的。

因此,整个类都可用于方法的实现,包括方法和在当前方法之后声明的成员。

这在[class.mem]中有介绍

Within the class member-specification, the class is regarded as complete within function bodies, default arguments, using-declarations introducing inheriting constructors, exception-specifications, and brace-or-equal-initializers for non-static data members (including such things in nested classes).

关于c++ - 类方法包含相同的类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44044522/

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