pure member functions

mastrost titi.mastro at free.fr
Fri Dec 26 03:11:42 PST 2008


Hello,

I have searched on the D2 documentation pages and on this forum, but I did not really found any documentation about pure member functions, so please excuse me if a make you repeat.

I was very surprised to see this code compile (dmd 2.022):

class A{
    private:
        int x;
    public:
        pure int f() {
            return x;
        }
        int g() {
            ++x;
            return x;
        }
}

void main(){

    A a=new A;
    writefln(a.f()); //prints 0
    a.g();
    writefln(a.f()); //prints 1
}

- So what kind of function can be considered as a pure member function ?

- In my exemple, is A.f really considered as pure by the compiler, because I explicitly added the keyword 'pure' ?

- Is a D compiler a good gift for christmas?

- Is this assertion true:
    "If we wanted to have true purity for member functions, it would mean that as soon as a member function is pure, all other functions in the same module would have to be pure, expect the constructors"

- (equivalent to the previous question) If inside a module only the constructors of the classes are not pure, can I be sure that no behaviour like what we see in my example will happen (the first call to a.f() gives different result from the second call to a.f() ) ?

Thank you all




More information about the Digitalmars-d mailing list