Thread overview
D for Linux: Class instance Error
Jul 02, 2003
Simon J Mackenzie
Jul 02, 2003
Andrew Edwards
Re: D for Linux: Class instance Error - Documentation needs updating
Jul 04, 2003
Simon J Mackenzie
Jul 04, 2003
Andrew Edwards
Jul 04, 2003
Simon J Mackenzie
Jul 04, 2003
Andy Friesen
Jul 05, 2003
Ilya Minkov
July 02, 2003
Can someone please check this code?  It compiles ok under Mandrake Linux 9.0 but generates a segment fault after the first printf.

According to the D Class documentation this code should work!

In file a.d I have...

module a;

import c.stdio;

class Abc
{
	void print() { printf("Hello from module a!\n"); }
}

void main()
{
	Abc a;

	printf("Main:\n");
	a.print();
}

Simon

July 02, 2003
the line: "Abc a;"
should read: "Abc a = new Abc;"

Good luck,
Andrew


July 04, 2003
Andrew Edwards wrote:
> the line: "Abc a;"
> should read: "Abc a = new Abc;"
> 
> Good luck,
> Andrew
> 
> 
Thanks for the reply...
So am I correct in saying that I can't treat a class definition like a basic data type when creating an instance of that class?
eg.
int ANumber;
AClassDeftn AnInstanceOfClass;
...I must always use new
ie.
AClassDeftn AnInstanceOfClass = new AClassDeftn;
...because the class examples indicate that this can be done.
...An extract from the class documentation examples reads...

>In C++, it is common practice to define a field, along with >"object-oriented" get and set functions for it:
>
>        class Abc
>        {       int property;
>                void setProperty(int newproperty) { property = >newproperty; }
>                int getProperty() { return property; }
>        };
>
>        Abc a;
>        a.setProperty(3);
>        int x = a.getProperty();
>
>All this is quite a bit of typing, and it tends to make code unreadable >by filling it with getProperty() and setProperty() calls. In D, get'ers >and set'ers take advantage of the idea that an lvalue is a set'er, and >an rvalue is a get'er:
>        class Abc
>        {       int myprop;
>                void property(int newproperty) { myprop = newproperty; >} // set'er
>                int property() { return myprop; }       // get'er
>        }
>
> which is used as:
*******************************************************************
>        Abc a;  <<<<<< then this should be >>>>>> Abc a = new Abc;
*******************************************************************
>        a.property = 3;         // equivalent to a.property(3)
>        int x = a.property;             // equivalent to int x = >a.property()
>

Using "new" seems to be a cumbersome way to declare an instance of a derived class type given that Walter argues for the above D code over and against C++ on the grounds of excessive typing.
If "new" is needed all the time what's the benefit over C++?

Simon

July 04, 2003
Comments embedded.

"Simon J Mackenzie" <project.d@smackoz.fastmail.fm> wrote ...

> ...I must always use new
> ie.
> AClassDeftn AnInstanceOfClass = new AClassDeftn;


Yes.

> Using "new" seems to be a cumbersome way to declare an instance of a
> derived class type given that Walter argues for the above D code over
> and against C++ on the grounds of excessive typing.
> If "new" is needed all the time what's the benefit over C++?
>
> Simon


While I agree that the documentation must be updated to reflect the current language rules (i.e. Abc a = new Abc; vice Abc a;), the specific example to which you aluded, reffers to getters and setters and has nothing to do with class instanciation. The benifit gained is the reduced overhead that one would normally incurr from making function calls. Additinal benifits includes less typing and improved readability.

the following example illustrates the intended use:

class MyClass
{
    int size()  // getter
    {
        return mysize;
    }
    void size(int newsize) // setter
    {
        mysize = newsize;
    }
private:
    int mysize;
}

int main()
{
    MyClass a = new MyClass;
    int num;
    a.size = 1000;    // illegal in C++  // not yet implemented in D
    num = a.size;     // illegal on C++ // not yet implemented in D
    printf("%d", num);
    return 0;
}

Note that the a.size() is used a both an lvalue and an rvalue and is illegal in C++. You cannot, however, use this feature in D because it is not yet implemented.

If you read further down the page, you will find the following statement:

    "Instances of class objects are created with NewExpressions:
                	A a = new A(3);"

Which states exactly how to instanciate a given class.

Regards,
Andrew


July 04, 2003
Andrew Edwards wrote:

> int main()
> {
>     MyClass a = new MyClass;
>     int num;
>     a.size = 1000;    // illegal in C++  // not yet implemented in D
>     num = a.size;     // illegal on C++ // not yet implemented in D
Noted...
>     printf("%d", num);
>     return 0;
> }
> 
> Note that the a.size() is used a both an lvalue and an rvalue and is illegal
> in C++. You cannot, however, use this feature in D because it is not yet
> implemented.
As I found out in my test code...
> 
> If you read further down the page, you will find the following statement:
> 
>     "Instances of class objects are created with NewExpressions:
>                 	A a = new A(3);"
Yep read this too.
> 
> Which states exactly how to instanciate a given class.
> 
> Regards,
> Andrew
> 
> 

Thanks for the comments
Simon

July 04, 2003
Simon J Mackenzie wrote:
> blah blah blah
> 
> Using "new" seems to be a cumbersome way to declare an instance of a derived class type given that Walter argues for the above D code over and against C++ on the grounds of excessive typing.
> If "new" is needed all the time what's the benefit over C++?
> 
> Simon
> 

I'd like to see a shorthand syntax added to D, if it doesn't complicate the parser overmuch.

Foo foo;   // reference.  Must be new'd
Foo bar(); // same as Foo bar = new Foo();
           // bar is still a reference, and its object is still on the
           // heap.

--andy

July 05, 2003
Andy Friesen wrote:
> I'd like to see a shorthand syntax added to D, if it doesn't complicate the parser overmuch.
> 
> Foo foo;   // reference.  Must be new'd
> Foo bar(); // same as Foo bar = new Foo();
>            // bar is still a reference, and its object is still on the
>            // heap.
> 

This would be evil. Look, are you sure you can remember exactly where you have classes and where structs? New makes you think whether you possibly want to use delete, but its absense doesn't.

-i.