digitalmars.D.learn - How to implement private constructor
- Vinod K Chandran (21/21) Apr 24 2022 Hi all,
- =?UTF-8?Q?Ali_=c3=87ehreli?= (17/18) Apr 24 2022 Looks good to me.
- Vinod K Chandran (3/5) Apr 25 2022 Thanks a lot. All I wanted to implement more than ctor with
- bauss (3/24) Apr 25 2022 Yes and in addition to Ali's message then remember it's private
- bauss (4/32) Apr 25 2022 Oops typo.
- Vinod K Chandran (3/10) Apr 25 2022 Thanks for the reply. Got it. All I wanted to implement more than
Hi all,
Please take a look at this code. Is this the right way to use
private constructors ?
```d
class Foo {
int p1 ;
string p2 ;
bool p3 ;
private this(int a, string b, bool c) {
this.p1 = a
this.p2 = b
this.p3 = c
}
this(int a) {
this(a, "some string", true);
}
this(int a, string b) {
this(a, b, true);
}
}
```
Apr 24 2022
On 4/24/22 17:18, Vinod K Chandran wrote:
private this(int a, string b, bool c) {
Looks good to me.
There are other ways as well:
class Foo {
private:
// ...
public:
// ...
}
Or:
class Foo {
private {
// ...
}
// ...
}
Ali
Apr 24 2022
On Monday, 25 April 2022 at 02:22:42 UTC, Ali Çehreli wrote:Looks good to me. There are other ways as well:Thanks a lot. All I wanted to implement more than ctor with different parameters and avoid code duplication.
Apr 25 2022
On Monday, 25 April 2022 at 00:18:03 UTC, Vinod K Chandran wrote:
Hi all,
Please take a look at this code. Is this the right way to use
private constructors ?
```d
class Foo {
int p1 ;
string p2 ;
bool p3 ;
private this(int a, string b, bool c) {
this.p1 = a
this.p2 = b
this.p3 = c
}
this(int a) {
this(a, "some string", true);
}
this(int a, string b) {
this(a, b, true);
}
}
```
Yes and in addition to Ali's message then remember it's private
for the module only.
Apr 25 2022
On Monday, 25 April 2022 at 07:18:44 UTC, bauss wrote:On Monday, 25 April 2022 at 00:18:03 UTC, Vinod K Chandran wrote:Oops typo. What I meant is that private is module level, so it's __not__ private in the module, but it is for other modules.Hi all, Please take a look at this code. Is this the right way to use private constructors ? ```d class Foo { int p1 ; string p2 ; bool p3 ; private this(int a, string b, bool c) { this.p1 = a this.p2 = b this.p3 = c } this(int a) { this(a, "some string", true); } this(int a, string b) { this(a, b, true); } } ```Yes and in addition to Ali's message then remember it's private for the module only.
Apr 25 2022
On Monday, 25 April 2022 at 07:19:31 UTC, bauss wrote:Thanks for the reply. Got it. All I wanted to implement more than ctor with different parameters and avoid code duplication.Yes and in addition to Ali's message then remember it's private for the module only.Oops typo.What I meant is that private is module level, so it's __not__ private in the module, but it is for other modules.
Apr 25 2022









Vinod K Chandran <kcvinu82 gmail.com> 