1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++オーバーロードは継承されない。

Last updated at Posted at 2015-06-16

あれー。ビルド通らないなぁ。。。と思ったソース。

class Base
{
public:
virtual void foo(const char* str) = 0;
void foo(const std::string& str){
  foo(str.c_str());
}
};

class Hoge : public Base
{
public 
 void foo(const char* str){
 }
};

std::string str = "abc";
Hoge h;
h.foo(str); //std::stringを受け付ける定義ないよエラー
 

C++がそういう仕様らしい。
http://www.fides.dti.ne.jp/~oka-t/cpplab-hide-overload.html

知らなかった。。。

Hogeに

using Base::foo;

と記載、または、

Hoge h;
Base* b = &h;
b->foo(str)

とすれば、使えるようです。

ただ、この程度であれば、オーバーロードせずに、

std::string str = "abc";
Hoge h;
b->foo(str.c_str())

かな。。。

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?