我有以下代码:
#include <iostream>
class Base {
public:
virtual void sayHello() {
std::cout << "Hello world, I am Base" << std::endl;
}
};
class Derived: public Base {
public:
void sayHello() {
std::cout << "Hello world, I am Derived" << std::endl;
}
};
void testPointer(Base *obj) {
obj->sayHello();
}
void testReference(Base &obj) {
obj.sayHello();
}
void testObject(Base obj) {
obj.sayHello();
}
int main() {
{
std::cout << "Testing with pointer argument: ";
Derived *derived = new Derived;
testPointer(derived);
}
{
std::cout …Run Code Online (Sandbox Code Playgroud) const char (&x)[]C++中语法的含义是什么,它是通过引用函数调用来传递指针吗?
它是否相同const char x[],将x定义为const char*?如果两者都是同一个,我应该在哪里使用const char (&x)[]而不是const char x[]?