c++中的继承语法

Inheritance syntax in C++

本文关键字:语法 继承 c++      更新时间:2023-10-16

我理解了c++中继承的语法:

class DerivedClassName : public BaseClassName {}

然而,在一个程序中,我发现了这样一个字符串:

class ComplexNumberTest : public CppUnit::TestCase {

,我不明白它是什么意思。很明显,ComplexNumberTestCppUnit的子类,但TestCase是什么呢?

我认为CppUnit::TestCase是指CppUnit类的TestCase方法,但DerivedClassName应该是一个方法的子类?

谁能帮我一下吗?

CppUnit是命名空间,ComplexNumberTest是从CppUnit命名空间中派生出来的TestCase类。

在你的代码中,你有这样的TestCase:

namespace CppUnit
{
  class TestCase
  {
    // blah blah
  };
}

或者TestCase可以是CppUnit内部具有公共访问权限的嵌套类(类型)(感谢PeterWood)

class CppUnit
{
public:
  class TestCase
  {
    // blah blah
  };
};
class ComplexNumberTest : public CppUnit::TestCase
{
   // also blah
};

CppUnit是一个命名空间,或者TestCase是CppUnit中的一个嵌套类。

如果是命名空间:您可以通过使用名称空间:

来摆脱这种语法。
using namespace CppUnit;
class ComplexNumberTest : public TestCase {

Although, you don't usually want to put using namespace in a header file. -感谢评论@PeterWood