C++ member function translations

Bill Baxter dnewsgroup at billbaxter.com
Fri Jul 27 13:18:44 PDT 2007

Daniel919 wrote:
> Classes are always passed by reference, so there is no need for &.
> Structs are value types, they don't have a reference, so a ptr has to be 
> used.

Can't you at least use a 'ref StBar' as a parameter?

And in response to Christian -- I would have expected 'const ref StBar' 
to be the way to translate 'const StBar&' from C++.  That doesn't work? 
  Note that in C++ you can't re-bind a reference, so they're effectively 
like D final.  Thus in terms of acting like C++ it makes sense for const 
to apply to both the ref and the contents.   ..... hmm but I guess 'ref' 
isn't a type constructor in D it's just a parameter passing mechanism. 

Anyway, that's a big disappointment because passing large structs around 
by const references was one of the main reasons I wanted D to get const. 
  But it's not possible.

> It's not possible to overload member functions by attribute.
> So this is not possible:
> class Foo {
>     const void func() {/*don't change any fields*/}
>     void func() {/*changes might happen*/}
> }
> Just like it's neither possible to overload by public / private.

I thought 'const' served as a type constructor not an attribute.  But 
you're saying  D's 'const' applied to a method isn't just changing the 
type of the hidden 'this' parameter, it's an attribute of the whole 
method instead?  Odd.  Does that approach have some great virtue to it?

>  > C++:      const Foo& func(const Bar&) const;
> const const(ClFoo) cfunc(const(StBar)*)
> const const(StFoo)* cfunc(const(StBar)*)
>  > C++:      const Foo& func(Bar&) const;
> const const(ClFoo) cfunc(StBar*)
> const const(StFoo)* cfunc(StBar*)
>  > C++:      const Foo& func(const Bar&);
> const(ClFoo) func(const(StBar)*)
> const(StFoo)* func(const(StBar)*)
>  > C++:      const Foo& func(Bar&);
> const(ClFoo) func(StBar*)
> const(StFoo)* func(StBar*)
>  > C++:      Foo& func(Bar&) const;
> const ClFoo cfunc(StBar*)
> const StFoo* cfunc(StBar*)
>  > C++:      Foo& func(Bar&);
> ClFoo func(StBar*)
> StFoo* func(StBar*)

A lot of parentheses and stars. :-(



More information about the Digitalmars-d-learn mailing list