简体   繁体   English

类中的C ++比较方法

[英]C++ Compare Method within a Class

while(getline(INPUT,textLine))
  {
    Object* s = new Object(textLine);
    }

How would I hold a reference to the Object of the previous textLine so that I could compare the two in some compare method 我如何持有对上一个textLine对象的引用,以便可以通过某些compare方法比较两者

s.Compare(***); <-- comparing the second iteration to the first <-比较第二个迭代与第一个迭代

Note that *** is of type Object 请注意, ***是对象类型

You want to keep track of the previous instance of Object? 您要跟踪Object的先前实例吗?
Put it in a variable (called t below), and use the variable when you need it. 将其放在变量中(以下称为t ),并在需要时使用该变量。
Remember to delete everything you allocate. 切记删除分配的所有内容。

Object* s;
Object* t = NULL;

while(getline(INPUT,textLine))
{
    s = new Object(textLine);
    s->Compare(t);

    delete t; 
    t = s;
}

first, see strcmp() 首先,请参见strcmp()

if you want to incorporate this into your "Object" class, which is a bad naming choice, overload the operators !=, ==, > or <, all of them, or only one of them, as you wish/need. 如果要将其合并到“ Object”类中(这是一个不好的命名选择),请根据需要/或全部重载操作符!=,==,>或<,或仅重载其中的一个。 Comparison would then go like: if (s > s2) or similar. 然后进行比较:如果(s> s2)或类似。

Try reading a decent c++ book which covers basic topics as operator overloading and so on. 尝试阅读一本不错的c ++书,其中涵盖了操作员重载等基本主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM