C++ 是什么意思?在类中使用平均值?

发布于 2024-10-20 14:30:23 字数 118 浏览 3 评论 0原文

在类定义中使用 using 意味着什么?

class myClass {
public:
  [...]
  using anotherClass::method;
};

What does it mean to have a using inside a class definition?

class myClass {
public:
  [...]
  using anotherClass::method;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苹果你个爱泡泡 2024-10-27 14:30:23

该声明取消隐藏基类成员。这最常用于允许成员函数的重载。例子:

class Base {
public:
   void method() const;
};

class Derived : public Base {
public:
   void method(int n) const;
   // Without the using, you would get compile errors on d.method();
   using Base::method;
};

That declaration unhides a base class member. This is most often used to allow overloads of a member function. Example:

class Base {
public:
   void method() const;
};

class Derived : public Base {
public:
   void method(int n) const;
   // Without the using, you would get compile errors on d.method();
   using Base::method;
};
烦人精 2024-10-27 14:30:23

我见过的情况:

class A
{
    void foo(int);
    void foo(float);
}

class B : public A
{
    void foo(string);
}

B b;
b.foo(12); // won't work!

因为我在 B 中实现了一个具有不同签名的新 foo 函数,所以它隐藏了 A 中的 foo 函数。为了覆盖此行为,我会这样做:

   class B : public A
   {
       void foo(string);
       using A::foo;
   }

The case I've seen it:

class A
{
    void foo(int);
    void foo(float);
}

class B : public A
{
    void foo(string);
}

B b;
b.foo(12); // won't work!

Because I have implemented a new foo function in B with a different signature it hides the foo functions from A. In order to override this behavior I would do:

   class B : public A
   {
       void foo(string);
       using A::foo;
   }
大海や 2024-10-27 14:30:23

大多数情况下,像这样的语法的使用方式如下:

class derived : public base {
public:
    [...]
    using base::method;
};

这里的 using 声明取消隐藏来自父类的成员声明。如果衍生中的另一个成员声明可能会隐藏中的成员,有时这是必要的。

Most often, syntax like this is used like so:

class derived : public base {
public:
    [...]
    using base::method;
};

The using declaration here unhides a member declaration from the parent class. This is sometimes necessary if another member declaration in derived may hide the member from base.

落花随流水 2024-10-27 14:30:23

如果 anotherClass 是包含类似成员函数的基类

virtual void f();

,并且您决定重载派生类中的函数,就像

virtual void f(int);

它“隐藏”基类中的 f() 一样。例如,通过指向派生类的指针调用 f() 会导致错误,因为编译器不会“看到”不再从基类获取参数的 f() 版本。

通过编写,

using Base::f;

您可以将基类函数带回作用域,从而启用重载解析,就像您最初期望的那样。

If anotherClass is a base class that contains a member function like

virtual void f();

and you decide to overload the function in the derived class like

virtual void f(int);

it "hides" f() in the base class. Calling f() through a pointer to the derived class for example, would result in an error, since the compiler does not "see" the version of f() taking no arguments from the base class anymore.

By writing

using Base::f;

you can bring the base classes function back into scope, thus enabling overload resolution as you might have expected it to work in the first place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文