提问者:小点点

从派生类C++访问整个基类


我理解您可以从派生类访问基类的成员,然而,我有一个函数需要指向我的基类作为一个整体的指针。例如:

#include <iostream>
 
using namespace std;

function foo(Shape &s){
//does something
}

// Base class
class Shape {
   public:
      Shape(int w = 100, int h = 100){
         width = w;
         height = h;
      }
      void setWidth(int w) {
         width = w;
      }
      void setHeight(int h) {
         height = h;
      }
      
   protected:
      int width;
      int height;
};

// Derived class
class Rectangle: public Shape {
   public:
      Rectangle(){
        Shape();
      }
      int getArea() { 
         return (width * height); 
      }
};

int main(void) {
   Rectangle Rect;
   foo(// Pointer Reference to Rect.Shape here);

   return 0;
}

有没有办法从派生类中获得指向这个基类的指针?


共1个答案

匿名用户

这是您的代码的工作版本。我对其进行了一些更改,并添加了注释来解释更改。您的程序需要多态性以达到预期的行为,否则您将‘切片’您的派生对象,而只有一个基对象。

#include <iostream>
#include <string>

// Base class
// Your base should only have things that would be common to all derived classes
// Consider what the width and height of a Circle would be
//
// You may not have gotten to virtual functions and polymorphism yet. This is
// how you would set up an interface for your Derived classes. I am requiring
// any derived class to implement getArea() and identify() if it wants to be a
// 'concrete' class. Otherwise it will be abstract, which means you can't
// declare objects of that type. It is not possible to declare a Shape object
// because of the pure virtual functions
class Shape {
 public:
  virtual ~Shape() = default;          // A virtual destructor is required
  virtual double getArea() const = 0;  // Pure virtual function
  virtual std::string identify() const = 0;
};

// Derived class
class Rectangle : public Shape {
 public:
  // The base class should be initialized in the constructor's
  // initialization section. What you did was declare a temporary Shape that
  // went away when the function ended.
  // All class data should be set in the initialization section
  Rectangle(int w, int h) : Shape(), width(w), height(h) {}
  double getArea() const override { return (width * height); }

  std::string identify() const override { return "Rectangle"; }

 private:
  int width = 0;
  int height = 0;
};

// A new derived class that should work (a circle **is-a** shape), but doesn't
// with your setup. Circles don't have width and height
class Circle : public Shape {
 public:
  Circle(int r) : Shape(), radius(r) {}
  double getArea() const override { return 2 * 3.14 * radius * radius; }
  std::string identify() const override { return "Circle"; }

 private:
  int radius = 0;
};

// Subjective, I moved the function below the class definitions and added a body
void foo(Shape &s) {
  std::cout << "A " << s.identify() << " with area " << s.getArea() << ".\n";
}

int main(void) {
  Rectangle rect(5, 3);
  foo(rect);

  Circle circ(4);
  foo(circ);

  return 0;
}

输出:

A Rectangle with area 15
A Circle with area 100.48

如果我移除所有虚拟的东西,很多东西就会停止工作。现在我必须提供shape函数的实现。这在逻辑上没有多大意义。虽然我可以将派生对象传递给foo(),但它们会被切片,而填充器shape数据会被打印出来。

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(派生类|c++|访问|基|类)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?