我有一个具有相同名称的函数,但在基类和派生类中具有不同的签名.当我尝试在继承自派生的另一个类中使用基类的函数时,我收到一个错误.请参阅以下代码:
class A
{
public:
void foo(string s){};
};
class B : public A
{
public:
int foo(int i){};
};
class C : public B
{
public:
void bar()
{
string s;
foo(s);
}
};
Run Code Online (Sandbox Code Playgroud)
我从gcc编译器收到以下错误:
In member function `void C::bar()': no matching function for call to `C::foo(std::string&)' candidates are: int B::foo(int)
Run Code Online (Sandbox Code Playgroud)
如果我int foo(int i){};从课堂上删除B,或者我将其重命名foo1,一切正常.
这有什么问题?
我写下面的代码是为了解释我的问题.如果我注释第11行(使用关键字"using"),编译器不会编译该文件并显示以下错误:invalid conversion from 'char' to 'const char*'.它似乎没有void action(char)在Parent类中看到Son类的方法.
为什么编译器会以这种方式运行?或者我做错了什么?
class Parent
{
public:
virtual void action( const char how ){ this->action( &how ); }
virtual void action( const char * how ) = 0;
};
class Son : public Parent
{
public:
using Parent::action; // Why should i write this line?
void action( const char * how ){ printf( "Action: %c\n", *how ); }
};
int main( int argc, char** argv ) …Run Code Online (Sandbox Code Playgroud) 可能重复:
C++重载决议
我遇到了一个问题,在我的类重写了它的基类的函数后,所有重载的函数版本都被隐藏了.这是设计还是我做错了什么?
防爆.
class foo
{
public:
foo(void);
~foo(void);
virtual void a(int);
virtual void a(double);
};
class bar : public foo
{
public:
bar(void);
~bar(void);
void a(int);
};
Run Code Online (Sandbox Code Playgroud)
然后,下面会给出一个编译错误,说明条中没有(双)函数.
main()
{
double i = 0.0;
bar b;
b.a(i);
}
Run Code Online (Sandbox Code Playgroud) 在下面的例子中,我试图通过在课堂上将其隐藏起来来隐藏using Employee::showEveryDept最后一个子Designer类Elayer-
#include <iostream>
class Employee {
private:
char name[5] = "abcd";
void allDept() { std::cout << "Woo"; }
public:
void tellName() { std::cout << name << "\n"; }
virtual void showEveryDept()
{
std::cout << "Employee can see every dept\n";
allDept();
}
};
class ELayer : public Employee {
private:
using Employee::showEveryDept;
protected:
ELayer() {}
public:
using Employee::tellName;
};
class Designer : public ELayer {
private:
char color = 'r';
public:
void showOwnDept() { …Run Code Online (Sandbox Code Playgroud) 我低于警告.我的部分代码是:
class Base {
public:
virtual void process(int x) {;};
virtual void process(int a,float b) {;};
protected:
int pd;
float pb;
};
class derived: public Base{
public:
void process(int a,float b);
}
void derived::process(int a,float b){
pd=a;
pb=b;
....
}
Run Code Online (Sandbox Code Playgroud)
我收到以下警告:
Warning: overloaded virtual function "Base::process" is only partially overridden in class "derived"
Run Code Online (Sandbox Code Playgroud)
我以任何方式将进程作为虚函数,所以我期待这个警告不应该来......这背后的原因是什么?
我正在阅读Stroustrup的C++ 0x FAQ,并坚持使用这段代码.请考虑以下代码
struct A
{
void f(double)
{
std::cout << "in double" << std::endl;
}
};
struct B : A
{
void f(int)
{
std::cout << "in int" << std::endl;
}
};
int main()
{
A a; a.f(10.10); // as expected, A.f will get called
B b; b.f(10.10); // This calls b.f and we lose the .10 here
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的理解是,当一个类型被继承时,所有受保护的和公共成员都可以从派生类访问.但根据这个例子,看起来我错了.我期待bf会调用基类f.我通过改变派生类得到了预期的结果
struct B : A
{
using A::f;
void f(int)
{
std::cout << "in …Run Code Online (Sandbox Code Playgroud) 可能的重复:
C++重载解析
为什么派生类中的重写函数会隐藏基类的其他重载?
为什么以下示例:
class A {
public:
void f(int x){ }
};
class B : public A {
public:
void f(float a, int b){ }
};
int main(){
B *b = new B;
b->f(1);
};
Run Code Online (Sandbox Code Playgroud)
原因:
test.cpp:在函数'int main()'中:test.cpp:13:错误:没有匹配函数来调用'B :: f(int)'test.cpp:8:注意:候选者是:void B: :f(float,int)
f(int)并f(float, int)有不同的签名.为什么会导致错误?
编辑
我明白它藏起来了.我在问为什么会这样?
保持旧问题.请参阅下面的解决方案 它可能很简单,但仍然是.我有以下C++ 11代码片段:
#include <vector>
template <typename... Ts>
struct typelist
{
};
template <typename T>
struct EventContainer
{
typedef T Type;
/// TODO. Ring buffer
std::vector<T> container;
void push(const T& t)
{
EventContainer<T>::container.push_back(t);
}
virtual ~EventContainer()
{
}
};
template <template <typename...> class TL>
class EventStorage:
public EventContainer<Ts>...
{
};
class Event1
{
};
class Event2
{
};
typedef typelist<Event1,Event2> Events12;
int main()
{
EventStorage<Events12> ev;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何使EventStorage继承EventContainer与每种类型的模板typelist.我可以用Loki :: library做到这一点,但我想使用带有可变参数模板的C++ 11.谢谢.
解决方案1:修复 …
请看下面的代码:
#include <iostream>
using namespace std;
class A {
public:
A() {};
virtual void foo(double d) { cout << d << endl; }
virtual void foo(double d, int a) = 0;
};
class B : public A {
public:
B() {};
virtual void foo(double d, int a) { cout << d << endl << a << endl; }
};
int main()
{
B b;
b.foo(3.14);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器(试过g ++和visual c ++ 2008)说没有像B:foo(double)这样的函数.g ++的确切消息是:
main.cpp:21:错误:没有匹配函数来调用'B :: foo(double)'
它看起来像隐藏规则的效果,但在我看来这里不应该使用规则,因为我没有覆盖foo(double),并且两个foo方法都是在基类中定义的. …
c++ ×9
inheritance ×5
overriding ×2
polymorphism ×2
c++-faq ×1
c++11 ×1
function ×1
lookup ×1
name-hiding ×1
oop ×1
scope ×1
using ×1