C++クラスでのリロード
7496 ワード
関数リロードのレビュー
関数のリロードの本質は
互いに独立した異なる関数
C++で通過
関数名と#カンスウメイ#
関数パラメータ決定関数呼び出し{{かんすうぱらめーた:かんすうよびだし}}
関数名からリロードされた関数のエントリアドレスを直接取得できません
関数のリロードは必然的に発生します.
同じ役割ドメイン
クラス内のメンバー関数はリロードできます
コンストラクション関数のリロード
一般メンバー関数のリロード
静的メンバー関数の再ロード
質問:グローバル関数、一般メンバー関数、および静的メンバー関数の間でリロードを構成できますか?
リロード関数の本質は複数の異なる関数である
関数名とパラメータリストは一意のIDです
関数の再ロードは、同じ役割ドメインで発生する必要があります.
クラスで、
通常のメンバー関数と静的メンバー関数の間には、リロード関係を構成できますが、グローバル関数とクラス内のメンバー関数は、リロード関係を構成できません.
グローバル関数はグローバルのネーミングスペースにあり、メンバー関数はクラスにあり、それらの間の役割ドメインはすでに異なります.
#include
class Test
{
int i;
public:
Test()
{
printf("Test::Test()
");
this->i = 0;
}
Test(int i)
{
printf("Test::Test(int i)
");
this->i = i;
}
Test(const Test& obj)
{
printf("Test(const Test& obj)
");
this->i = obj.i;
}
static void func()
{
printf("void Test::func()
");
}
void func(int i)
{
printf("void Test::func(int i), i = %d
", i);
}
int getI()
{
return i;
}
};
void func()
{
printf("void func()
");
}
void func(int i)
{
printf("void func(int i), i = %d
", i);
}
int main()
{
func();
func(1);
Test t; // Test::Test()
Test t1(1); // Test::Test(int i)
Test t2(t1); // Test(const Test& obj)
func(); // void func()
Test::func(); // void Test::func()
func(2); // void func(int i), i = 2;
t1.func(2); // void Test::func(int i), i = 2
t1.func(); // void Test::func()
return 0;
}
:
pi@raspberrypi:~ $ g++ main.cpppi@raspberrypi:~ $ ./a.outvoid func()void func(int i), i = 1Test::Test()Test::Test(int i)Test(const Test& obj)void func()void Test::func()void func(int i), i = 2void Test::func(int i), i = 2void Test::func()
関数のリロードには何の意味がありますか?
関数名による関数機能のプレゼンテーション
パラメータリストから関数の使い方をヒントにする
拡張システムにすでに存在する関数機能
#include
#include <string.h>
// ,
char* strcpy(char* buf,const char*str,unsigned int n)
{
return strncpy(buf,str,n);
}
int main()
{
const char*str = "hello";
char buf[3] = {0};
strcpy(buf,str,sizeof(str)-1);
printf("%s
",buf);
return 0;
}
:
pi@raspberrypi:~ $ g++ main.cpppi@raspberrypi:~ $ ./a.outhelpi@raspberrypi:~ $