推 MrOrz:應用方式:拿來出考(被拖走 12/24 02:08
int t();
int y( int , int , const char* m) ;
int y( int a , int b = 5 , const char* s = "global," ) ;
int puts( const char* = "test test" );
int main()
{
void kkk( int a=3 );
kkk();
puts(); // <= 注意,沒有傳入字串
y(1);
int y( int a , int b , const char* s = "in main," );
y( 3 , 4 );
t();
{
void kkk( int k = 5 );
kkk();
//int puts( const char* = "123" );
//puts();
//出現 linking error, 不知道如何解釋?
int y( int a = 1 , int b = 3 , const char* s = "in scope,") ;
y();
}
return 0;
}
int y( int a , int b , const char* s )
{
printf( "%s in function y, a = %d, b = %d\n" , s , a , b ) ;
}
int t()
{
int y( int a , int b , const char* s );
y( 1 , -1 , "in function t," );
::y(7); //呼叫global 的 y
}
================ kkk.cpp =============================
#include <cstdio>
void kkk( int a )
{
printf( "%d kkk\n" , a );
}
======================================================
Output:
-----------------------------------------------
3 kkk
test test
global, in function y, a = 1, b = 5
in main, in function y, a = 3, b = 4
in function t, in function y, a = 1, b = -1
global, in function y, a = 7, b = 5
5 kkk
in scope, in function y, a = 1, b = 3
-----------------------------------------------
=======================================================
對於相關的推論在前幾篇大概已經講過了,
另外,
class 中 friend 函數的宣告和global的宣告會有衝突,
也就是他們被視為同一個層級。
還有,在非global的地方(函數內)還是看的到其他檔案的function,
可以宣告他的prototype並加以使用。
但對於標頭檔中的function,
就只能在global的地方設定default argument,在函數內則會出現linking error
不過,這樣寫除了讓人混亂以外,還不太清楚詳細的應用方式...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.228.154.219
===================== main.cpp ============================
#include<stdio.h>
/*
class A
{
public:
friend int y( int a=-1, int b=-2, const char*s="in A," );
會和下方的全域宣告重覆,出現錯誤
};
*/