www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - const(type) vs. const type

reply Mike Linford <mike.linford.reg gmail.com> writes:
I'm playing with QtD, and I tried to override a QWidget's sizeHint() 
function, which is declared as const QSize sizeHint(). I tried to 
override it by declaring my function as override const(QSize) sizeHint
() . I got a compiler error that it was "not covariant" with const QSize, 
and when I changed it to that it worked fine. I've skimmed through the 
documentation but don't understand what the difference is. Any help?



-- 
Mike Linford
Jul 20 2010
next sibling parent Jonathan M Davis <jmdavisprog gmail.com> writes:
On Tuesday, July 20, 2010 15:57:41 Mike Linford wrote:
 I'm playing with QtD, and I tried to override a QWidget's sizeHint()
 function, which is declared as const QSize sizeHint(). I tried to
 override it by declaring my function as override const(QSize) sizeHint
 () . I got a compiler error that it was "not covariant" with const QSize,
 and when I changed it to that it worked fine. I've skimmed through the
 documentation but don't understand what the difference is. Any help?
IIRC, const QSize sizeHint() is synonymous with QSize sizeHint() const which means that the function is const. However, const(QSize) sizeHint() means that the QSize returned is const rather than the function. - Jonathan M Davis
Jul 20 2010
prev sibling next sibling parent reply div0 <div0 users.sourceforge.net> writes:
On 20/07/2010 23:57, Mike Linford wrote:
 I'm playing with QtD, and I tried to override a QWidget's sizeHint()
 function, which is declared as const QSize sizeHint(). I tried to
 override it by declaring my function as override const(QSize) sizeHint
 () . I got a compiler error that it was "not covariant" with const QSize,
 and when I changed it to that it worked fine. I've skimmed through the
 documentation but don't understand what the difference is. Any help?
const QSize sizeHint() means the object on which you are calling the function has to be const and the returned QSize is mutable (I think, it might also be const as well, I don't use that format of method signature...), whilst const(QSize) sizeHint() means a mutable object which returns a const object. Const is allowed before & after the method signature which is confusing and annoying. In c++ it would be: (assuming QSize is a D class) QSize& sizeHint() const { return _sh; } vs const QSize& sizeHint() { return _sh; } -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Jul 20 2010
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
div0:
 Const is allowed before & after the method signature which is confusing 
 and annoying.
We have asked Walter to improve this situation, and he has refused, saying that keeping all D "attributes" syntax uniform is better. See also: http://d.puremagic.com/issues/show_bug.cgi?id=4040 Bye, bearophile
Jul 20 2010
prev sibling parent Jonathan M Davis <jmdavisprog gmail.com> writes:
On Tuesday, July 20, 2010 16:23:23 div0 wrote:
 Const is allowed before & after the method signature which is confusing
 and annoying.
TDPL had an explanation for that that IIRC made fair sense, but I'd have to look it up again since I don't remember exactly what it was. Certainly, at first glance, it's a poor design decision. I'll obviously have to look it up again to see why it wasn't as bad a decision as it seems at first glance. - Jonathan M Davis
Jul 20 2010
prev sibling next sibling parent Trass3r <un known.com> writes:
 I'm playing with QtD, and I tried to override a QWidget's sizeHint()
 function, which is declared as const QSize sizeHint(). I tried to
 override it by declaring my function as override const(QSize) sizeHint
 () . I got a compiler error that it was "not covariant" with const QSize,
http://digitalmars.com/d/2.0/const3.html In that case it is a const member function which means it is not allowed to change any part of the object through the member function's this reference.
Jul 20 2010
prev sibling next sibling parent torhu <no spam.invalid> writes:
On 21.07.2010 00:57, Mike Linford wrote:
 I'm playing with QtD, and I tried to override a QWidget's sizeHint()
 function, which is declared as const QSize sizeHint(). I tried to
 override it by declaring my function as override const(QSize) sizeHint
 () . I got a compiler error that it was "not covariant" with const QSize,
 and when I changed it to that it worked fine. I've skimmed through the
 documentation but don't understand what the difference is. Any help?
In the first case, it's the function itself that ends up being const, not its return value. It's like putting 'const' after the parameter list in C++. In the second case, only the return value is const.
Jul 20 2010
prev sibling parent Mike Linford <mike.linford.reg gmail.com> writes:
Thanks for the responses guys, I appreciate it.

-- 
Mike Linford
Jul 21 2010