相关疑难解决方法(0)

为什么具有相同名称但签名不同的多重继承函数不会被视为重载函数?

下面的代码片段在编译过程中会产生一个"foo的foo调用"错误,我想知道如果没有完全限定对foo的调用,是否有任何方法解决这个问题:

#include <iostream>

struct Base1{
    void foo(int){
    }
};

struct Base2{
    void foo(float){
    }
};

struct Derived : public Base1, public Base2{
};

int main(){
    Derived d;
    d.foo(5);

    std::cin.get();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

所以,问题就像标题所说的那样.想法?我的意思是,以下工作完美无瑕:

#include <iostream>

struct Base{
    void foo(int){
    }
};

struct Derived : public Base{
    void foo(float){
    }
};

int main(){
    Derived d;
    d.foo(5);

    std::cin.get();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ scope overloading multiple-inheritance

49
推荐指数
2
解决办法
8748
查看次数

使用和重载基类的模板成员函数?

在下面,struct Y重载了X成员函数f.两个重载都是模板函数,但是要明确指定不同的参数(typenameint):

struct X
{
    template <typename> static bool f() { return true; }
};

struct Y : public X
{
    using X::f;
    template <int> static bool f() { return false; }
};

int main()
{
    std::cout << Y::f <void>() << " " << Y::f <0>() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

1 0按预期使用gcc 打印.然而,clang(3.3)抱怨说

[...] error: no matching function for call to 'f'
        std::cout << Y::f <void>() << " " << Y::f …
Run Code Online (Sandbox Code Playgroud)

c++ overloading member-functions using-declaration template-function

6
推荐指数
1
解决办法
1027
查看次数

C++重载运算符,具有来自模板的多重继承

我有一个层次结构,代表HTTP客户端的某些部分,如下所示:

typedef list<pair<string, string> > KeyVal;
struct Header { string name; string value; ...};
struct Param { string name; string value; ...};

/* Something that contains headers */
template<typename T> class WithHeaders {
  KeyVal headers;
public:
  virtual T &operator <<(const Header &h) {
    headers.push_back(pair<string, string>(h.name, h.value));
    return static_cast<T&> (*this);
  }
};

/* Something that contains query params */
template<class T> class WithQuery {
    KeyVal query_params;

public:
    virtual T &operator <<(const Param &q) {
      query_params.push_back(pair<string, string>(q.name, q.value));
      return static_cast<T&> (*this);
    } …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates operator-overloading

6
推荐指数
1
解决办法
1004
查看次数