Thread overview
Class member functions
Sep 30, 2005
Tommy
Sep 30, 2005
JT
Sep 30, 2005
Tommy
Sep 30, 2005
JT
Oct 01, 2005
Tommy
Oct 01, 2005
Hasan Aljudy
Sep 30, 2005
David L. Davis
private import was: Re: Class member functions
Oct 01, 2005
Tommy
Oct 01, 2005
Chris Sauls
Re: private import
Oct 01, 2005
Tommy
September 30, 2005
In a C++ class I can put the body of a function outside the class definition, e.g.:

// compiles fine as C++

class foo
{
public:
void bar();
};

void foo::bar() // ERROR if compiled as D source
{
// do something
}

int main(){}

//--- End of code

In D, however, I get two errors on the marked line:

- semicolon expected, not ':'
- Declaration expected, not ':'

What's the equivalent in D?

Thanks,
Tommy


September 30, 2005
class foo
{
    public:

    void bar()
    {
        // do something
    }
}



Tommy wrote:
> In a C++ class I can put the body of a function outside the class
> definition, e.g.:
> 
> // compiles fine as C++
> 
> class foo
> {
> public:
> void bar();
> };
> 
> void foo::bar() // ERROR if compiled as D source
> {
> // do something
> }
> 
> int main(){}
> 
> //--- End of code
> 
> In D, however, I get two errors on the marked line:
> 
> - semicolon expected, not ':'
> - Declaration expected, not ':'
> 
> What's the equivalent in D?
> 
> Thanks,
> Tommy
> 
> 
September 30, 2005
In article <dhk7fe$2dqh$1@digitaldaemon.com>, JT says...

>class foo
>{
>     public:
>
>     void bar()
>     {
>         // do something
>     }
>}

Is that supposed to mean "You have to write the function body into the class definition, you simply can't put it outside the class definition"?

Tommy


September 30, 2005
In article <dhk5a7$2agq$1@digitaldaemon.com>, Tommy says...
>
>In a C++ class I can put the body of a function outside the class definition, e.g.:
>
>// compiles fine as C++
>
>class foo
>{
>public:
>void bar();
>};
>
>void foo::bar() // ERROR if compiled as D source
>{
>// do something
>}
>
>int main(){}
>
>//--- End of code
>
>In D, however, I get two errors on the marked line:
>
>- semicolon expected, not ':'
>- Declaration expected, not ':'
>
>What's the equivalent in D?
>
>Thanks,
>Tommy
>

# // foo.d
# private import std.stdio;
#
# class foo
# {
#     public:
#     void bar()
#     { std.stdio.writefln("foo.bar called!"); }
# }
#
# int main()
# {
#     foo oFoo = new foo;
#     oFoo.bar();
#     return 0;
# }

Output:
--------
C:\dmd>dmd foo.d
C:\dmd\bin\..\..\dm\bin\link.exe foo,,,user32+kernel32/noi;

C:\dmd>foo
foo.bar called!

C:\dmd>

I don't think you can declare the class method outside of the class body itself, but I can tell you that D doesn't use the "::" (double semi-colons) like C++ does. Not sure if the example code above will be useful to you or not, but it's there just in case. ;)

David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
September 30, 2005
well... yeah. D rules like that! you no longer have to hastle with headers.....


Tommy wrote:
> 
> Is that supposed to mean "You have to write the function body into the
> class definition, you simply can't put it outside the class
> definition"?
> 
> Tommy
> 
October 01, 2005
Tommy wrote:
> In article <dhk7fe$2dqh$1@digitaldaemon.com>, JT says...
> 
> 
>>class foo
>>{
>>    public:
>>
>>    void bar()
>>    {
>>        // do something
>>    }
>>}
> 
> 
> Is that supposed to mean "You have to write the function body into the
> class definition, you simply can't put it outside the class
> definition"?
> 
> Tommy
> 
> 

Yeah, and believe me, that's alot better!
October 01, 2005
OK, thanks, I see!

Tommy

In article <dhk9v1$2ft4$1@digitaldaemon.com>, JT says...

>well... yeah. D rules like that! you no longer have to hastle with headers.....


October 01, 2005
In article <dhk8q2$2ep1$1@digitaldaemon.com>, David L. Davis says...

># private import std.stdio;

What's the difference between this and:

import std.stdio;

By the way, I seem to remember it is called std.c.stdio, not std.stdio?!

Tommy


October 01, 2005
Tommy wrote:
> In article <dhk8q2$2ep1$1@digitaldaemon.com>, David L. Davis says...
> 
>># private import std.stdio;
> 
> What's the difference between this and:
> 
> import std.stdio;

The difference is in symbol propagation.  A public import (the default) in a module Foo is visible to any other module that imports Foo.  A private import in Foo is hidden from any other module importing foo.

>
> By the way, I seem to remember it is called std.c.stdio, not std.stdio?!
>

Actually, both of these modules exist.  Module std.c.stdio is the old stdio.h from C. Module std.stdio is D's new writef()/writefln() functions and their file/function variations.  See: http://digitalmars.com/d/phobos/std_stdio.html .

-- Chris Sauls
October 01, 2005
In article <dhm4ev$g00$1@digitaldaemon.com>, Chris Sauls says...
> [...]

Thanks for your answers! I think I understand it now.
Tommy