Thread: operator overloading

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    7

    operator overloading

    i've been stuck for days now so... i'm trying to compare two objects, one is p of type plane, the other is a plane stored in l[i] of type list. i need to overload the operator< to compare the two types using strcmp so i have:

    Code:
    bool operator< (const plane& p) const
    {
    	//postcondition: returns true if the implicit argument
    	//is less than the explicit argument using a key of
    	//model name, tail number order
    
    	const char* pModel = p.getModel();
    	const char* pNum = p.getNumber();
    
    	if (strcmp(pModel, ????) < 0
    		&& strcmp(pNum, ????) < 0)
    		return true;
    	else
    		return false;
    }
    i can't seem to figure out how i can access list l at position [i] to compare it to the other plane. do i need a friend function? thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, first of all, you should certainly supply the model and number from your current object to strcmp().

    The next thing is that your condition is a bit broken. If two planes are of equal model, then you shouldn't return false, should you? At that point, it should come down to the number.

    So your condition should be:
    Code:
            cmp = strcmp(pModel, model);
            // If it's "less" then we should return true.
    	if (cmp < 0)
                   return true;
            // if it's equal, we defer to the number:
            if (cmp == 0 && strcmp(pNum, ????) < 0)
                return true;
            // All other cases end up here, and of course are false.
            return false;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    thanks that helped with the logic, but just to get it to compile i need to know how to access l[i]'s atrributes from the overloaded operator code. the overload is in a plane.cpp. the list.cpp is where the comparing will be done.
    Code:
    void list::insert(const plane& p)
    {
    	i = 0;
    
    	if (size >= MAX_LIST)
    		throw listException("List is full");
    	
    	while (i <= size && list[i] < p)
    		i++; //...this isn't really done being code...
    }
    so really i want something like this in the list.cpp:
    Code:
    plane p2 = list[i];
    and perhaps
    Code:
    otherVar = p2.getModel();
    then compare the p with the p2. i guess i'm having scope issues as well.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    For one, you have to make operator< be either a member function of plane, or take two parameters. The latter is preferred if you don't have to access private or protected data, or if the plane object allows implicit conversion.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As far as I can tell, if the list consists of an array of plane, then you should be able to use the plane:perator<() function as defined above. The compiler will automatically understand what's going on.

    If your list holds some generic list item, and the plane is derived from the generic list item, as long as the operator<() is defined as a virtual function for the base class, it should work out just fine for the list insertion.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed