www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Class member functions

reply Tommy <Tommy_member pathlink.com> writes:
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
Sep 30 2005
next sibling parent reply JT <jtd514 ameritech.net> writes:
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
 
 

Sep 30 2005
parent reply Tommy <Tommy_member pathlink.com> writes:
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
Sep 30 2005
next sibling parent reply JT <jtd514 ameritech.net> writes:
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
 

Sep 30 2005
parent Tommy <Tommy_member pathlink.com> writes:
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.....

Oct 01 2005
prev sibling parent Hasan Aljudy <hasan.aljudy gmail.com> writes:
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!
Sep 30 2005
prev sibling parent reply David L. Davis <SpottedTiger yahoo.com> writes:
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
Sep 30 2005
parent reply Tommy <Tommy_member pathlink.com> writes:
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
Oct 01 2005
parent reply Chris Sauls <ibisbasenji gmail.com> writes:
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
Oct 01 2005
parent Tommy <Tommy_member pathlink.com> writes:
In article <dhm4ev$g00$1 digitaldaemon.com>, Chris Sauls says...
 [...]

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