C++覆盖/过载问题

发布于 2024-08-06 03:55:50 字数 1102 浏览 1 评论 0原文

我在 C++ 中遇到一个问题:

#include <iostream>

class A
{
protected:
  void some_func(const unsigned int& param1)
  {
    std::cout << "A::some_func(" << param1 << ")" << std::endl;
  }
public:
  virtual ~A() {}
  virtual void some_func(const unsigned int& param1, const char*)
  {
    some_func(param1);
  }
};

class B : public A
{
public:
  virtual ~B() {}
  virtual void some_func(const unsigned int& param1, const char*)
  {
    some_func(param1);
  }
};

int main(int, char**)
{
  A* t = new B();
  t->some_func(21, "some char*");
  return 0;
}

我使用的是 g++ 4.0.1 且编译错误:

$ g++ -W -Wall -Werror test.cc
test.cc: In member function ‘virtual void B::some_func(const unsigned int&, const char*)’:
test.cc:24: error: no matching function for call to ‘B::some_func(const unsigned int&)’
test.cc:22: note: candidates are: virtual void B::some_func(const unsigned int&, const char*)

为什么我必须指定 B 类中对 some_func(param1) 的调用是 A::some_func(param1) ?这是一个 g++ bug 还是来自 g++ 的随机消息,以防止出现我看不到的特殊情况?

I'm facing a problem in C++ :

#include <iostream>

class A
{
protected:
  void some_func(const unsigned int& param1)
  {
    std::cout << "A::some_func(" << param1 << ")" << std::endl;
  }
public:
  virtual ~A() {}
  virtual void some_func(const unsigned int& param1, const char*)
  {
    some_func(param1);
  }
};

class B : public A
{
public:
  virtual ~B() {}
  virtual void some_func(const unsigned int& param1, const char*)
  {
    some_func(param1);
  }
};

int main(int, char**)
{
  A* t = new B();
  t->some_func(21, "some char*");
  return 0;
}

I'm using g++ 4.0.1 and the compilation error :

$ g++ -W -Wall -Werror test.cc
test.cc: In member function ‘virtual void B::some_func(const unsigned int&, const char*)’:
test.cc:24: error: no matching function for call to ‘B::some_func(const unsigned int&)’
test.cc:22: note: candidates are: virtual void B::some_func(const unsigned int&, const char*)

Why do I must specify that the call of some_func(param1) in class B is A::some_func(param1) ? Is it a g++ bug or a random message from g++ to prevent special cases I don't see ?

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

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

发布评论

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

评论(1

后来的我们 2024-08-13 03:55:50

问题在于,在派生类中,您隐藏了基类中的受保护方法。您可以执行以下操作:完全限定派生对象中的受保护方法,或者使用 using 指令将该方法引入作用域:

class B : public A
{
protected:
  using A::some_func; // bring A::some_func overloads into B
public:
  virtual ~B() {}
  virtual void some_func(const unsigned int& param1, const char*)
  {
    A::some_func(param1); // or fully qualify the call
  }
};

The problem is that in the derived class you are hiding the protected method in the base class. You can do a couple of things, either you fully qualify the protected method in the derived object or else you bring that method into scope with a using directive:

class B : public A
{
protected:
  using A::some_func; // bring A::some_func overloads into B
public:
  virtual ~B() {}
  virtual void some_func(const unsigned int& param1, const char*)
  {
    A::some_func(param1); // or fully qualify the call
  }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文