如下所示,我有一个全局变量curr_obj_to_draw
,其中包含要在实际绘制函数中使用的当前对象。原因是由于
glutDisplayFunc(str_to_fp_mapping[curr_obj_to_draw<T>->get_name()]);
此外,每个绘图功能都是专门的。它们必须是静态的,否则我无法获取对象的方法的指针。假设它们是静态的,一旦curr_obj_to_draw
在void draw(int argc, char **argv, T *obj)
中初始化,它将开始检查所有访问此变量的静态方法的有效性,当然draw_circle
的方法实现与{{ 1}}或draw_square
。到目前为止,您必须关闭那些调用不属于当前初始化对象的方法名称的行。我想要解决这个问题。如何确保draw_rectangle
初始化不会触发所有正在使用它的方法的验证,而不会触发仅应绘制该方法的验证?
还有,是否有一种通用的方法可以一次在窗口中绘制许多对象,而不是一个接一个地绘制,而每次都必须关闭窗口?
Shape.hpp
curr_obj_to_draw
Rectangle.hpp
//
// Created by user on 6/4/19.
//
#ifndef CPP_SHAPE_HPP
#define CPP_SHAPE_HPP
#include <iostream>
/*
* Needs more refining of attributes and intelligent methods
* Explain default and explicit
*
*
* */
template <typename T>
class Shape{
public:
Shape() = default;
explicit Shape(T area = 0, T dimensions = 0, T perimeter = 0){
this->area = area;
this->dimensions = dimensions;
this->perimeter = perimeter;
//this->name = nullptr;
}
void identify_shape(){
std::cout<<"I am a Shape"<<std::endl;
}
T get_area(){
return area;
}
void set_area(T area){
this->area = area;
}
T get_dimensions(){
return dimensions;
}
void set_dimensions(T dimensions){
this->dimensions = dimensions;
}
T get_perimeter(){
return perimeter;
}
void set_perimeter(T perimeter){
this->perimeter = perimeter;
}
std::string get_name(){
return name;
}
virtual void draw(int argc, char** argv) = 0;
protected:
T area;
T dimensions;
T perimeter;
std::string name;
//Surface area?
};
#endif //CPP_SHAPE_HPP
Draw.hpp
//
// Created by user on 6/4/19.
//
#ifndef CPP_RECTANGLE_HPP
#define CPP_RECTANGLE_HPP
#include "Shape.hpp"
#include "../Draw.hpp"
template<typename T>
class Rectangle : public Shape<T> {
public:
Rectangle() {
this->name = "rectangle";
}
explicit Rectangle(T length, T breadth) : Shape<T>(length * breadth, 2, 2 * (length + breadth)) {
this->length = length;
this->breadth = breadth;
this->name = "rectangle";
}
T get_length() {
return length;
}
void set_length(T length) {
this->length = length;
this->area = this->length * this->breadth;
this->perimeter = 2 * (this->length + this->breadth);
}
T get_breadth() {
return breadth;
}
void set_breadth(T breadth) {
this->breadth = breadth;
this->area = this->length * this->breadth;
this->perimeter = 2 * (this->length + this->breadth);
}
void draw(int argc, char **argv) {
std::cout << "I am drawing a rectangle" << std::endl;
Draw<Rectangle<T>> dr;
dr.draw(argc, argv, this);
}
friend std::ostream &operator<<(std::ostream &strm, const Rectangle &a) {
return strm << "Rectangle(" << a.length << ", " << a.breadth << ", " << a.area << ", "
<< a.perimeter << ", " << a.dimensions << ")" << std::endl;
}
protected:
T length, breadth;
};
#endif //CPP_RECTANGLE_HPP