What is the different between this?

New to FreeBASIC? Post your questions here.
Post Reply
miilvyxg
Posts: 193
Joined: Dec 07, 2021 6:51

What is the different between this?

Post by miilvyxg »

Type Foo

Public Function Test(Byval x as Long) As Long;

Private Sub TestSub();

End Type

vs

Type Foo

Declare Function Test(Byval x as Long) As Long;

Declare Sub TestSub();

End Type

Why sometimes I saw access modifiers (Public, Private,...) being used but sometimes only Declare is used?

My impression is there is no rule and people just code the way they like.

Please tell me the different between them and which is the preferred way to code.

Thanks.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: What is the different between this?

Post by fxm »

The most explicit syntax is as follows:

Code: Select all

Type Foo
    Public:
        Declare Function Test(Byval x as Long) As Long  '' public function
        As Long l                                       '' public variable
    Private:
        Declare Sub TestSub()                           '' private subroutine
        As Integer i                                    '' private variable
End Type
But as by default, a declaration is always public, this other shortened syntax is also valid:

Code: Select all

Type Foo
    Declare Function Test(Byval x as Long) As Long  '' public function
    As Long l                                       '' public variable
    Private:
        Declare Sub TestSub()                       '' private subroutine
        As Integer i                                '' private variable
End Type
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: What is the different between this?

Post by caseih »

miilvyxg wrote:My impression is there is no rule and people just code the way they like.
No, you're just missing some information, which @fxm provided.

It all depends on what you need. If you don't need private or protected members, then by all means use unadorned declares. If you're making a highly-structured class hierarchy, then you'll probably need to use protected and possibly private members to achieve the encapuslation and polymorphism you desire.
miilvyxg
Posts: 193
Joined: Dec 07, 2021 6:51

Re: What is the different between this?

Post by miilvyxg »

What about constructor? Is something like this right?

Type Foo

Public:

Declare Constructor()

Declare Constructor(Byval x As Long Ptr)

End Type
miilvyxg
Posts: 193
Joined: Dec 07, 2021 6:51

Re: What is the different between this?

Post by miilvyxg »

What about constructor with a default parameter like this piece of C++ code?

class Foo {

public:

Foo(int x = 256);

}

How could it be correctly translated to FB code?
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: What is the different between this?

Post by paul doe »

Yes for the first. For the second:

Code: Select all

class Foo {

public:

Foo(int x = 256);

}

Code: Select all

type Foo
  public:
    declare constructor( as long = 256 )
end type
miilvyxg
Posts: 193
Joined: Dec 07, 2021 6:51

Re: What is the different between this?

Post by miilvyxg »

paul doe wrote:Yes for the first. For the second:

Code: Select all

class Foo {

public:

Foo(int x = 256);

}

Code: Select all

type Foo
  public:
    declare constructor( as long = 256 )
end type
Seems Byval or Byref must be omitted? I mean should it be Byval as long = 256 or something like that?
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: What is the different between this?

Post by fxm »

miilvyxg wrote:What about constructor with a default parameter like this piece of C++ code?

class Foo {

public:

Foo(int x = 256);

}

How could it be correctly translated to FB code?

Code: Select all

Type Foo Extends Object
    Public:
        Declare Constructor(Byval x As Long = 256)
End Type
miilvyxg
Posts: 193
Joined: Dec 07, 2021 6:51

Re: What is the different between this?

Post by miilvyxg »

fxm wrote:
miilvyxg wrote:What about constructor with a default parameter like this piece of C++ code?

class Foo {

public:

Foo(int x = 256);

}

How could it be correctly translated to FB code?

Code: Select all

Type Foo Extends Object
    Public:
        Declare Constructor(Byval x As Long = 256)
End Type
When to Extends Object when not?
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: What is the different between this?

Post by fxm »

Many answers to your questions are in the documentation:
FreeBASIC Manual
Maybe you should start there.
miilvyxg
Posts: 193
Joined: Dec 07, 2021 6:51

Re: What is the different between this?

Post by miilvyxg »

fxm wrote:Many answers to your questions are in the documentation:
FreeBASIC Manual
Maybe you should start there.
Indeed I started there (https://www.freebasic.net/wiki/CatPgProgrammer) and can't find the information I want. So I asked.
Post Reply