宣布班级为静态

Declaring a class as static?

本文关键字:静态 布班级      更新时间:2023-10-16

背景:

我目前正在通过EDX |中级C 课程。尽管该课程是由Microsoft领导的,但并不声明您需要使用其IDE Visual Studios成功完成。我只有一个Mac,所以我完全不用担心就使用Xcode进行了介绍课程。

问题:

在中间课程的第一个模块中,它指出您可以将标题内的类声明为静态:

// Math.h
// Header file for the Math class
#pragma once
// Math class definition
static class Math
{
    public:
    // given base and exponent, calculate value
    static int Math::pow(int base, int exp);
};

xcode标记错误,说:

'static'不允许在类型
的声明中允许

根据以前提出的问题,这是不可能的混淆人们。

static在此上下文中无效。替代方案是全静态成员,但类本身无效。

a 类似语法的使用将是:

static class Math
{
    public:
    // given base and exponent, calculate value
    static int Math::pow(int base, int exp);
} math;  // <---- note object

在这种情况下,对象mathstatic,而不是类本身。

只需在上课前删除静态关键字即可。练习中的所有内容仍然以相同的方式完全有意义。