C 中函式名是常量還是常變數?

時間 2021-08-12 03:54:31

1樓:Yukimura

都不是。

N4861:

6.7.2 Object Model

The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is created by a definition (6.2), by a new-expression (7.

6.2.7), by an operation that implicitly creates objects (see below), when implicitly changing the active member of a union (11.

5), or when a temporary object

is created (7.3.4, 6.

7.7). An object occupies a region of storage in its period of construction (11.

10.4), throughout its lifetime (6.7.

3), and in its period of destruction (11.10.4).

[Note: A function is not an object, regardless of whether or not it occupies storage in the way that objects do. —end note]

clause6

6.1 Preamble

3. An entity is a value, object, reference, structured binding, function, enumerator, type, class member, bit-field, template, template specialization, namespace, or pack.

6. A variable is introduced by the declaration of a reference other than a non-static data member or of an object. The variable』s name, if any, denotes the reference or object.

3.表面 object和function同一level,沒有誰屬於誰。

2樓:C十十20年

函式名代表乙個入口位址,當程式開始執行時,這個入口位址是不變的,所以它應該是乙個常量,這個常量可以用於初始化函式指標變數。能初始化變數的值不是常量就是變數,由前所述它應該是常量。例如:

int f(int x)

void main

雖然lambda表示式常作函式使用,但並不是所有lambda表示式都可當作函式入口位址,因為lambda表示式分為"準物件"與「準函式"兩類,具體用法參見《C++程式設計精要教程》。

3樓:傳統的幻想書屋

C++裡函式不屬於物件,函式的型別屬於一種特殊的型別,也就是函式型別。函式則是函式型別的例項。函式型別和我們常用的變數或引用型別是兩個世界的東西。

所以函式名不是常量也不是變數,它就是乙個函式(或者這麼說,講函式是乙個變數還是常量沒有什麼意義,因為函式型別不是物件型別)。

C++的const只能修飾物件,或者作為物件型別或引用型別的限定符,不存在const限定的函式型別。前面也有人用type traits的例子,表明了函式並不是指標,也不是引用,更沒有const,就是因為函式型別是一種有別於這幾種型別的乙個單獨的型別。

不過函式可以隱式轉換,而產生乙個函式指標,函式指標就是物件型別了,它就跟普通的指標一樣,可以是常量也可以是變數了。

4樓:學生2775

#include

cout

#include

// is_const, is_pointer, is_reference

voidf(

void

){}int

main

()不是指標或引用也沒有被const修飾。

c 中函式宣告時使用void函式名(函式型別)是什麼意思 為什麼要這樣宣告函式?

你看那銳雯 首先去驗證。你測試一下乙個不需要返回值的函式不寫void,會發生什麼?報什麼錯就會發現如果不寫編譯器會認為那你需要返回 int 第二,仔細研究c語言的函式返回值語法,以及同樣類似的問題,編譯器找不到函式宣告會發生什麼?這個時候編譯器是如何判定返回值型別的?這些都在c標準規定了,當然有些是...

一次函式y kx b(k 0)中的k,b是變數還是常數?

迅哥兒1399 一次函式,是所有形如 的一系列對映,此時 是作為引數變數出現的,用於指明 一次函式 這類對映應該具有的形式。實際上,還充當了 一次函式 這類對映的標號集,即每給定一組 便確定了乙個 特定的一次函式 當題目中出現 的範圍時 如 0,b 0 eeimg 1 指的是 取值滿足這個範圍的 構...

c 中 不同 namespace 的變數及函式查詢是怎樣設計的?

能找到test是由於test含有乙個型別S1的引數,所以會在S1所在的命名空間中尋找函式test。這種查詢方式叫做Argument dependent lookup或者Koenig lookup。Argument dependent lookupThese function names are lo...