博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ Can't Overload Static Function with Non-Static Function
阅读量:4285 次
发布时间:2019-05-27

本文共 2322 字,大约阅读时间需要 7 分钟。

Ref 

ISO 14882:2003 C++ Standard 13.1/2 – Overloadable declarations

Certain function declarations cannot be overloaded:

  • Function declarations that differ only in the return type cannot be overloaded.
  • Member function declarations with the same name and the same parameter types cannot be overloaded if any of them is a static member function declaration (9.4).

...

[Example:

class X {
static void f(); void f(); // ill-formed void f() const; // ill-formed void f() const volatile; // ill-formed void g(); void g() const; // OK: no static g void g() const volatile; // OK: no static g };

—end example]

...

Besides, it would be ambiguous anyway since it's possible to call static functions on instances:

ISO 14882:2003 C++ Standard 9.4/2 – Static members

A static member s of class X may be referred to using the qualified-id expression X::s; it is not necessary to use the class member access syntax (5.2.5) to refer to a static member. Astatic member may be referred to using the class member access syntax, in which case the object-expression is evaluated. [Example:

class process {
public: static void reschedule(); } process& g(); void f() { process::reschedule(); // OK: no object necessary g().reschedule(); // g() is called }

—end example]

...

So there would be ambiguity with what you have:

class Foo { public: string bla; Foo() { bla = "nonstatic"; } void print() { cout << bla << endl; } static void print() { cout << "static" << endl; } }; int main() { Foo f; // Call the static or non-static member function? // C++ standard 9.4/2 says that static member // functions are callable via this syntax. But // since there's also a non-static function named // "print()", it is ambiguous. f.print(); }

To address your question about whether you can check what instance a member function is being called on, there is the this keyword. The this keyword points to the object for which function was invoked. However, the this keyword will always point to an object i.e. it will never be NULL. Therefore it's not possible to check if a function is being called statically or not à la PHP.

ISO 14882:2003 C++ Standard 9.3.2/1 – The this pointer

In the body of a nonstatic (9.3) member function, the keyword this is a non-lvalue expression whose value is the address of the object for which the function is called.

转载地址:http://resgi.baihongyu.com/

你可能感兴趣的文章
Tesseract.js相关整理
查看>>
echarts(国产)基于html5-canvas的开源图表绘制组件
查看>>
Chart.Js轻量级HTML5图表插件
查看>>
基于Bootstrap的jQuery slider插件的使用bootstrap-slider.js
查看>>
Vue.js数据驱动的组件,为现代化的 Web 界面而生
查看>>
Bootstrap中文网开源项目免费 CDN 服务、cdn.bootcss.com
查看>>
C#发送Get请求(带参数)
查看>>
爬取Ip地址对应的物理位置等信息-百度服务器
查看>>
C# 获取IP地址
查看>>
C#使用ping命令
查看>>
C#域名操作,正则匹配域名
查看>>
VS调试版本和发布版本
查看>>
C#获取Url中的域名
查看>>
C# IP地址和整数之间的转换,IP地址和数字ip地址的转换
查看>>
packagecontrol.io 拒绝了我们的连接请求
查看>>
VSCode前端编辑器 1.7(编辑功能媲美sublime text,HTML等代码格式化很是不错)
查看>>
VsCode插件整理
查看>>
VsCode插件之vscode-icons
查看>>
Framework7特色的HTML框架WebApp开源前端框架
查看>>
VSCode插件之View In Browser/Open in Browser‘在浏览器中查看’
查看>>