相关疑难解决方法(0)

C++静态成员方法调用类实例

这是一个小测试程序:

#include <iostream>

class Test
{
public:
    static void DoCrash(){ std::cout<< "TEST IT!"<< std::endl; }
};

int main()
{
    Test k;
    k.DoCrash(); // calling a static method like a member method...

    std::system("pause");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在VS2008 + SP1(vc9)上编译很好:控制台只显示"TEST IT!".

据我所知,不应该在instanced对象上调用静态成员方法.

  1. 我错了吗?从标准角度来看,这段代码是否正确?
  2. 如果它是正确的,那为什么?我找不到为什么会被允许,或者可能是为了帮助在模板中使用"静态或非"方法?

c++ standards visual-c++

38
推荐指数
2
解决办法
6万
查看次数

对重载静态函数的模糊调用

我对这种情况感到困惑,谷歌搜索没有给我答案.基本上我有以下不编译的简单代码:

#include <iostream>

class A
{
public:
    int a(int c = 0) { return 1; }
    static int a() { return 2; }
};

int main()
{
    std::cout << A::a() << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在编制本,GCC 4.2表示调用A::a()main()是与暧昧的两个版本a()有效候选人.Apple的LLVM编译器3.0编译时没有错误.

为什么gcc对我要调用哪个函数感到困惑?我认为这是明显的,通过资格a()A::我要求的static功能的版本.当然,如果我删除该static函数a(),这段代码仍然无法编译,因为A::a()它不是用于调用non的有效语法static a().

谢谢你的评论!

c++

12
推荐指数
1
解决办法
1070
查看次数

为什么静态和非静态方法不能共享相同的签名?

C#提供了在函数重载时使用的以下签名特征.

我们知道,重载只考虑参数; 它们的数量和类型,但多态性的目标是根据调用策略提供相同的名称但不同的用法.

如果我有一个包含两个具有相同名称和签名的方法的类,而一个是静态而另一个不是,则C#编译器会抛出错误; "类已经定义了一个名为'foo'的成员,它具有相同的参数类型".对这两种方法的调用将会有所不同; 一个具有对象名称,另一个具有类名称.因此,呼叫策略没有歧义.那为什么会抛出错误?

 class Example {

    public void foo() { }
    public static void foo() { }

}

class Program
{
    static void Main(string[] args)
    {

        Example e = new Example();
        e.foo(); 

    }
}
Run Code Online (Sandbox Code Playgroud)

c# polymorphism

11
推荐指数
2
解决办法
3078
查看次数

这两个成员函数,为什么不重载?

根据§13.1/ 2,下面的两个成员函数不会超载.为什么是这样?

class X {
    static void f();
    void f() const;
};
Run Code Online (Sandbox Code Playgroud)

编辑:问题不重复.上面的例子显示了一个const函数和一个非const的静态函数.我从§13.1/ 2中知道函数不可重载.我只是想知道为什么标准不允许上面给出的结构.

c++ overloading language-lawyer c++11

5
推荐指数
0
解决办法
135
查看次数

与SFINAE一起在静态上加载

我尝试在Visual Studio 2013中编译以下程序并得到C2686: cannot overload static and non-static member functions错误.

#include <iostream>
#include <type_traits>

struct foo
{
  template<bool P>
  static std::enable_if_t<P> bar()
  {
    std::cerr << "P is true\n";
  }

  template<bool P>
  std::enable_if_t<!P> bar()
  {
    std::cerr << "P is false\n";
  }
};

int main()
{
  foo f;
  f.bar<true>();
}
Run Code Online (Sandbox Code Playgroud)

我熟悉这个编译器错误 - 请参阅此StackOverflow答案,但是很惊讶地发现错误与SFINAE一起,编译器将始终丢弃过载集中的两个重载中的一个.

Visual Studio 2013是否正确遵循此处的标准,或者是否可以与SFINAE一起使静态过载?

编辑:上面的对比示例与返回类型重载

如果没有SFINAE,则无法重载static,也无法在返回类型上重载.但是,Visual Studio 2013支持与SFINAE一起在返回类型上重载.

以下程序与上面的程序相同但删除static并更改了第二个foo::bar声明的返回类型,正确编译.

#include <iostream>
#include <type_traits>

struct foo
{
  template<bool P>
  std::enable_if_t<P> bar() …
Run Code Online (Sandbox Code Playgroud)

c++ sfinae

2
推荐指数
1
解决办法
196
查看次数

c ++静态成员函数重载继承

我有以下代码:

struct CommonVariables
{/*some common variables that need to be proceed*/};

struct CommonHandler
{
    static void foo(const CommonVariables& vars) {/*processed vars*/}
    void bar(const CommonVariables& vars) {/*processed vars*/}
};

struct AnotherVariables
{/*another variables*/};

struct AnotherHandler
{
    static void foo(const AnotherVariables& vars) {/*processed vars*/}
    void bar(const AnotherVariables& vars) {/*processed vars*/}
};

struct Derived : CommonHandler, AnotherHandler
{};
Run Code Online (Sandbox Code Playgroud)

当我尝试调用 Derived::foo(/ any variable type /) 或 Derived d; d.bar(/ any variable type /),编译器给我一个错误:“对'foo(or bar)'的引用不明确”。

然而在下面我有几乎相同的情况:

struct DifferentHandler
{
static void foo(const CommonVariables& vars) {} …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance static-methods

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