Thread: Full operator overloading in BCB5

  1. #1
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317

    Thumbs down Full operator overloading in BCB5

    I have the following operator overloading functions for a class named cRoom. The constructor of this class simply sets default values for some of it's data members.

    Code:
          bool operator >(const cRoom& aRoom) const { return (this->area() > aRoom.area()); }
          //bool operator >(const double& value) const { return (this->area() > value); }
    When, in main(), I wote the following expression, I was expecting an error with my compiler:

    if(oKitchen > 127.0) { //... do something}

    The fact is that not only I had no error, but it also worked perfectly.
    I found it strange and proceed to step into my code. What I found amazing was that the expression above, actually called the constructor of the cRoom class, passed 127.0 to the first argument, did the operator overloading and then called the destructor.

    How come an object was created when the only thing that happens in the right side of the > sign is a constant?

    More... I did the following and it behaved the exact same way:

    float a = 127.0;
    if(oKitchen > a) { //... do something}

    When I removed the comment from the second operator overload, it behaved as expected and proceed without creating an object. But why did the first form worked?
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Im not familar with BCB5, but it seems as if that is a feature of the compiler. It sees you comparing an Object to a constant or variable, but it also knows you have a constructor that takes that single variable, so it puts 2 and 2 together, and creates a temp object for you.
    That is pretty nifty, but its not portable code, and i dont think that would compile in msvc with the second version of > commented out.

  3. #3
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    It can be a feature, but considering it passes the constant to the first argument of the constructor, can you imagine if the constructor is not set so that this works out?... imagine the following constructor

    cRoom(char* pDesc, double height, double width, double length);

    Can you see what will happen?
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    It is standard C++.

    Code:
    #include <iostream>
    
    class test {
    public:
    	test(int i_) : i(i_) {
    		std::cout << "test(int)" << std::endl;
    	}
    	~test() {
    		std::cout << "~test()" << std::endl;
    	}
    	bool operator < (const test& t) {
    		return i < t.i;
    	}
    private:
    	int i;
    };
    
    int main() {
    	test t(1);
    	std::cout << "t < 0\t" << (t < 0) << std::endl;
    
    	return 0;
    }
    That compiled on both MSVC and mingw. There are many ways for objects to "change" types in C++... perhaps too many, and that is one of them.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Ok. I guess I'll have to accept it as it is... but it certainly caught me by surprise. Function overloading is the exact opposite to this.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You can force the compiler to only accept an already constructed object by making the parameter a non-const reference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  2. hashing help
    By alokin in forum C Programming
    Replies: 17
    Last Post: 10-28-2002, 06:33 PM
  3. The glass is half full
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-30-2002, 03:08 PM
  4. Running in full screen mode!
    By Skute in forum Game Programming
    Replies: 0
    Last Post: 12-10-2001, 03:56 AM