小编Pek*_*kov的帖子

将派生类传递给基类参数的函数

我有以下代码:

#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)

c++ inheritance pointers reference c++11

4
推荐指数
1
解决办法
3323
查看次数

语法`const char(&x)[]`的含义

const char (&x)[]C++中语法的含义是什么,它是通过引用函数调用来传递指针吗?

它是否相同const char x[],将x定义为const char*?如果两者都是同一个,我应该在哪里使用const char (&x)[]而不是const char x[]

c++ pointers reference

3
推荐指数
1
解决办法
887
查看次数

标签 统计

c++ ×2

pointers ×2

reference ×2

c++11 ×1

inheritance ×1