【问题标题】:Inputting into a pointer and returning the methods of that pointer?输入一个指针并返回该指针的方法?
【发布时间】:2011-11-22 22:57:04
【问题描述】:

对不起标题,我不知道如何措辞。基本上我有一个指向形状对象的空指针,当输入一些输入时,我希望它们成为这个形状对象的值;

shape *s = (shape *) 0;
cin >> *s;
if (s)
    cout << "Shape" << (*s) << '\n';
else if (! cin.bad())
    cout << "Read reached EOF\n";
else
    cout << "Read failed\n";

我已经像这样重载了

std::ostream& operator<< (std::ostream &os, shape &s){
    s.draw();
    return os;
}

我正在努力将我的值读入指针。我的数据的输入类型是这样的;

< Circle: (3,1) 1 >

我已经重载了 >> 操作符;

std::istream& operator>> (std::istream &is, shape &s){
  char lessthan, greaterthan, opbrkt, clsbrkt, comma;
  string shape_type;
  double x1, y1, r, x2, y2, x3, y3;

  if (is >> lessthan) {
    if ('<' != lessthan) {
      is.setstate(ios::badbit);
      return is;
    }

    //Circle implemntation
    if(is >> shape_type && shape_type == "Circle:"){
      if(!((is >> opbrkt) && (opbrkt = '('))) {
        is.setstate(ios::badbit);
        return is;
      }

      cout << "Yes" << i++;
      if(!(is >> x1)){
        is.setstate(ios::badbit);
        return is;
      }

      if(!(is >> comma && comma == ',')){
        is.setstate(ios::badbit);
        return is;
      }

      if(!(is >> y1)){
        is.setstate(ios::badbit);
        return is;
      }


      if(!(is >> clsbrkt && clsbrkt == ')')) {
        is.setstate(ios::badbit);
        return is;
      }

      if(!(is >> r)){
        is.setstate(ios::badbit);
        return is;
      }

      if(!(is >> clsbrkt && clsbrkt == '>')) {
        is.setstate(ios::badbit);
        return is;
      }
    }

    return is;
  }

  return is;
}

这基本上表明它是一个圆圈并将相关的变量放入变量中以供以后使用,但是我如何将这些数据放入's'指针以指向现在应该是一个圆圈对象?

【问题讨论】:

    标签: c++ pointers operator-overloading


    【解决方案1】:

    哎哟!!!

    shape *s = (shape *) 0;
    cin >> *s;
    

    您刚刚取消引用了一个空指针。那是未定义的行为。你需要的是一个工厂方法。

    shape* input()
    {
        string name;
        cin >> name;
        if(name == "circle")
        {
           double radius;
           cin >> radius;
           return new Circle(radius)
        }
        else if(name == "Rectangle")
        {
            ....
            return new Rectangle(...);
        }
        else if .... 
    
    }
    

    【讨论】:

    • 感谢阿门的回复!我应该把这个函数放在哪里,我会在>>的重载中调用它吗??
    • 这可能是一个独立的函数。
    • Armen,对不起,我在这里绝对是业余爱好者!如何调用 'cin>> *s' 部分的 input()?!
    • @robb077: shape *s = input();
    • Armen,再次感谢,但我无法更改代码中的 'shape *s = (shape *) 0' 部分,这将用于测试我们的程序!我可以在其他任何地方实现吗?
    【解决方案2】:

    您无法读取指针。你只能读入一个对象:

    shape s;
    std::cin >> s;
    
    // if you need a pointer for some reason:
    shape * p = &s;
    

    如果实际对象本质上是多态的,您可能希望生成一个包装类,可能像这样:

    class Shape
    {
      ShapeImpl * impl;  // or better, `std::unique_ptr<ShapeImpl>`
      //
    }
    
    class ShapeImpl { virtual ~ShapeImpl() { } };
    class Circle : public ShapeImpl { /* ... */ }
    

    否则,您可能会放弃重载流提取运算符的想法,而是提供自己的反序列化函数:

    std::unique_ptr<Shape> read_shape(std::istream & is)
    {
      // ...
      std::unique_ptr<Shape> p(new Circle);
      // ...
      return p;
    }
    
    std::unique_ptr<Shape> s(read_shape(std::cin));
    

    【讨论】:

      猜你喜欢
      • 2010-09-14
      • 2013-10-28
      • 2011-11-01
      • 1970-01-01
      • 2021-09-21
      • 2020-01-12
      • 1970-01-01
      • 2016-03-02
      • 1970-01-01
      相关资源
      最近更新 更多