Kar*_*nke 3 c++ pointers function
仍然在C++工作,但这出现在我的书中,我不明白它是什么:
MyClass * FunctionTwo (MyClass *testClass) {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是第一个间接运算符的重要性是什么
(MyClass *[<- this one] FunctionTwo(MyClass *testClass))?
Run Code Online (Sandbox Code Playgroud)
我尝试在代码块中创建一个类似的函数,有和没有第一个*,我没有看到它运行的方式或输出的任何差异:
int *testFunc(int *in) {
cout << in << endl;
cout << *in << endl;
return 0;
}
int testFuncTwo(int *in) {
cout << in << endl;
cout << *in << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在书中找不到任何解释它的地方.
谢谢,
卡尔
Pre*_*gha 10
这MyClass *意味着此函数返回指向MyClass实例的指针.
由于代码实际上返回0(或NULL),这意味着此函数的任何调用者都会返回一个指向MyClass对象的NULL指针.
我认为以下是更好的例子:
int *testFunc(int *in) {
cout << "This is the pointer to in " << in << endl;
cout << "This is the value of in " << *in << endl;
return in;
}
int testFuncTwo(int *in) {
cout << "This is the pointer to in " << in << endl;
cout << "This is the value of in " << *in << endl;
return *in;
}
void test() {
int a = 1;
cout << "a = " << a;
int output = *testFunc(&a); // pass in the address of a
cout << "testFunc(a) returned " << output;
output = testFuncTwo(&a); // pass in the address of a
cout << "testFuncTwo(a) returned " << output;
}
Run Code Online (Sandbox Code Playgroud)
道歉,但我多年没有做过C++,但语法可能有些偏差.