是否有任何关键字需要重新定义;“全部”;模板化派生类中模板化基类的方法? 我知道这看起来是个愚蠢的问题,但是C++中使用面向对象的东西真的很麻烦。 例如,Foo是基类: template <typename T> class Foo { public: virtual void Method1() { } virtual void Method1(int a) { } virtual void Method2() { } virtual void Method2(int a) { } //... lots of other methods }; 模板 福班{ 公众: 虚拟void Method1(){} 虚空方法1(int a){} 虚拟void Method2(){} 虚空方法2(int a){} //…还有很多其他方法 };

是否有任何关键字需要重新定义;“全部”;模板化派生类中模板化基类的方法? 我知道这看起来是个愚蠢的问题,但是C++中使用面向对象的东西真的很麻烦。 例如,Foo是基类: template <typename T> class Foo { public: virtual void Method1() { } virtual void Method1(int a) { } virtual void Method2() { } virtual void Method2(int a) { } //... lots of other methods }; 模板 福班{ 公众: 虚拟void Method1(){} 虚空方法1(int a){} 虚拟void Method2(){} 虚空方法2(int a){} //…还有很多其他方法 };,c++,C++,是否有类似于: template <typename T> class Bar : public Foo<T> { public: using Foo<T>::*; //redefine all inherited methods from Foo virtual void Method1(int a) { } virtual void Method2(int a) { } //other methods overloadi

是否有类似于:

template <typename T>
class Bar : public Foo<T> {
  public:
    using Foo<T>::*; //redefine all inherited methods from Foo
    virtual void Method1(int a) { }
    virtual void Method2(int a) { }
    //other methods overloading..
};
模板
分类栏:公共食品{
公众:
使用Foo::*;//重新定义从Foo继承的所有方法
虚空方法1(int a){}
虚空方法2(int a){}
//其他方法重载。。
};
而不是:

template <typename T>
class Bar : public Foo<T> {
  public:
    using Foo<T>::Method1
    using Foo<T>::Method2
    //... lots of other methods

    virtual void Method1(int a) { }
    virtual void Method2(int a) { }
    //other methods overloading..
};
模板
分类栏:公共食品{
公众:
使用Foo::Method1
使用Foo::Method2
//…还有很多其他方法
虚空方法1(int a){}
虚空方法2(int a){}
//其他方法重载。。
};
因此,我们可以:

int main() {
  Bar<int> b;
  b.Method1();
  b.Method2();
  //... lots of other methods

  //This obviously works without the 'using' keyword:
  Foo<int>* f = &b;
  f->Method1();
  f->Method2();
  //etc
  return 0;
}
intmain(){
b栏;
b、 方法1();
b、 方法2();
//…还有很多其他方法
//这显然在没有“使用”关键字的情况下有效:
Foo*f=&b;
f->Method1();
f->Method2();
//等
返回0;
}

不,没有类似的功能,但通常不需要。基本继承机制已经提供了使用对
所做的操作

如果派生类中的重载隐藏基类中的方法,或者如果要更改访问模式,但不是一般情况,则需要使用
using

class A {
    void f() {}
public:
    void g(int) {}
    void h(int) {}
};

struct B : A {
    using A::f; // make f public
    void g(double) {}
    using A::g; // otherwise A::g is hidden by the overload
    // using A::h isn't needed
};

请注意,您仍然可以通过
B
实例调用
A::h()
,因为没有任何东西会隐藏它。

否。模板专用化是独立的类型,不会从常规模板继承或与之相关。这取决于你,以确保它们具有相同的隐式接口,C++不使这一点特别容易。 另请看最近的这个问题,它很可能就是您正在寻找的。
1。什么是隐藏

恐怕您的小示例有问题,我怀疑您的问题在于
隐藏

让我们首先说明
隐藏
是什么:

struct Base { void foo(int) { std::cout << "Base" << std::endl; } };

struct Derived: Base { void foo(float) { std::cout << "Derived" << std::endl; } };

int main(int, char* argv[])
{
  Derived d;
  int integer = 1;
  float floating = 2;
  d.foo(floating);     // outputs "Derived" as expected
  d.foo(integer);      // outputs "Derived" too UhOh ?
}
这里的问题是,它只是
隐藏
,这可能令人惊讶

void fooize(const Base& b) { b.foo(); }

int main(int argc, char* argv[])
{
  Derived d;
  d.foo();          // output "Derived"
  fooize(d);        // output "Base"
}
如果希望重新定义,则需要
virtual
关键字。然后,您将通过以下方式更正您的类:

struct Base
{
  virtual ~Base() {}       // Polymorphism means virtual destructor
  virtual foo(int) const { std::cout << "Base" << std::endl; }
};

struct Derived: Base
{
  virtual ~Derived() {}    // Not necessary, but sweet reminder
  virtual foo(int) const;  // virtual not necessary, but a sweet reminder again :)
};
struct Base
{
virtual~Base(){}//多态性表示虚拟析构函数

virtualfoo(int)const{std::cout,但主要示例
b.Method1()中的方法调用
如果不将
using
添加到
Bar
类中,它将不起作用。对不起,你是对的。它只在有重载方法时才起作用,如果你有很多这样的方法,这仍然是一件痛苦的事情。恐怕语言中没有办法解决这个问题,这只是设计的,没有好的解决方案“So we can do:”部分中的所有内容都可以在使用
Foo
定义中的
语句的情况下工作。您试图解决的问题到底是什么?@Matthieu-关于您的“食谱”:您可能应该使用“公共继承”一词,因为正如你所知,私有继承是另一种形式的继承composition@Matthieu:谢谢你的详细解释,事实上我已经知道了,这不是我的意思。我忘了在我的示例中使方法虚拟。(问题更新)@Manuel:事实上,即使是私有继承也会引入多态性,尽管仅限于类本身及其朋友。私有继承与组合绝对不同:例如,不能将Pimpl与私有继承一起使用,因此存在可见性问题。我自己只允许私有继承触发
EBO
而且我认为这是必要的……我经常看到它是出于懒惰而非真正的必要性。。。
struct Base
{
  virtual ~Base() {}       // Polymorphism means virtual destructor
  virtual foo(int) const { std::cout << "Base" << std::endl; }
};

struct Derived: Base
{
  virtual ~Derived() {}    // Not necessary, but sweet reminder
  virtual foo(int) const;  // virtual not necessary, but a sweet reminder again :)
};