what does "... discards qualifiers" mean ?

This is a discussion on what does "... discards qualifiers" mean ? within the C++ Programming forums, part of the General Programming Boards category; Hi, I have this piece of code: Code: #include <iostream> using namespace std; class Float { public: Float() : Value(0.0f) ...

  1. #1
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190

    what does "... discards qualifiers" mean ?

    Hi,

    I have this piece of code:

    Code:
    #include <iostream>
    using namespace std;
    
    class Float
    {
    	public:
    		Float() : Value(0.0f) {}
    		Float(float NewValue) :Value(NewValue) {}
    		~Float() {}
    		
    		const Float operator+ (Float F)
    			{
    				return Float(F.Value+Value);
    			}
    	
    	protected:
    		float Value;
    };
    
    class Vector2D
    {
    	public:
    		Vector2D() : X(0.0f), Y(0.0f){}
    		Vector2D(Float NewX, Float NewY) : X(NewX), Y(NewY) {}
    		~Vector2D() {}
    		
    		Float X, Y;
    		
    	protected:
    };
    
    class SomeThing
    {
    	public:
    		SomeThing() {}
    		~SomeThing() {}
    		
    		//if I take away 'const' in the beginning of next line, everything is ok
    		const Vector2D getVector() const {return Vector;}
    		
    	protected:
    		Vector2D Vector;
    };
    
    int main() 
    {
    	SomeThing Thing;
    	
    	Float K=Thing.getVector().X + Thing.getVector().Y;
    }
    When I try to compile it, I get following error:

    main.cpp:49: error: passing `const Float' as `this' argument of `const Float
    Float::operator+(Float)' discards qualifiers
    What does that mean?

    But if I take away the const modifier before SomeThing::getVector() - as the comment in the code above indicates - everything compiles fine...

    thanx :)
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    it means that in order for it to do what you are telling it, it would have to discard the qualifier const that your function is returning. My suggestion is that your return value does not, and propably should not be const. Any user of your class would expect to be able to modify the result of an addition operation.

    Float operator+ (Float F);

    is a better solution unless you have a specific reason to make it const then you would have to:

    return const Float(F.Value+Value);

  3. #3
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    thanks alot
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. discards qualifiers
    By EstateMatt in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2009, 03:05 PM
  2. Replies: 8
    Last Post: 05-29-2008, 01:47 PM
  3. error: passing [...] discards qualifiers
    By carlorfeo in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2008, 09:45 AM
  4. passing discards qualifiers? overloading =...
    By Captain Penguin in forum C++ Programming
    Replies: 9
    Last Post: 10-07-2002, 06:38 PM
  5. Discards qualifier from pointer target type?
    By pdstatha in forum C Programming
    Replies: 1
    Last Post: 04-16-2002, 03:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21