开发者

C++ Compare Method within a Class

开发者 https://www.devze.com 2023-04-12 17:58 出处:网络
while(getline(INPUT,textLine)) { Object* s = new Object(textLine); } How would I hold a reference to the Object of the previous textLine so th开发者_JAVA百科at I could compare the two in some compar
while(getline(INPUT,textLine))
  {
    Object* s = new Object(textLine);
    }

How would I hold a reference to the Object of the previous textLine so th开发者_JAVA百科at I could compare the two in some compare method

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?
Put it in a variable (called t below), and use the variable when you need it.
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()

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. Comparison would then go like: if (s > s2) or similar.

Try reading a decent c++ book which covers basic topics as operator overloading and so on.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号