digitalmars.D.learn - Translate C/C++ patern: return a pointer
- biocyberman (31/31) May 24 2018 Some C and C++ projects I am working on use pointers and
- Nicholas Wilson (11/46) May 24 2018 it looks like Read is a D class? in which case it already returns
- biocyberman (3/16) May 24 2018 Thanks for the hints. `Read` in C++ and D are both classes. And
- Jacob Carlborg (6/8) May 24 2018 In that case specifying the type as `Read` is the correct thing to do.
- biocyberman (2/8) May 26 2018 Thanks. I did that and it worked correctly.
Some C and C++ projects I am working on use pointers and references extensively: to pass as function arguments, and to return from a function. For function argument I would use `ref`, but for return types, I can't use `ref` and can't return a pointer. What should be the proper way to handle this? Do I have to change function signature (i.e. return type) For example, the following function: ``` //C++ version, from: https://github.com/bioslaD/fastp/blob/orig/src/read.cpp#L69 Read* Read::reverseComplement(){ Sequence seq = ~mSeq; string qual; qual.assign(mQuality.rbegin(), mQuality.rend()); string strand = (mStrand=="+") ? "-" : "+"; return new Read(mName, seq, strand, qual); } // D version: Read reverseComplement(){ Sequence seq = ~mSeq; dchar[] qual = cast(dchar[])mQuality.dup; reverse(qual); string strand = (mStrand=="+") ? "-" : "+"; Read newRead = new Read(mName, seq, strand, cast(string)qual); // return &newRead does not work: returning `& newRead` escapes a reference to local variable newRead return newRead; } ``` Let's not focus on the function body, I don't know how to handle the return type in cases like this for the D version.
May 24 2018
On Thursday, 24 May 2018 at 08:16:30 UTC, biocyberman wrote:Some C and C++ projects I am working on use pointers and references extensively: to pass as function arguments, and to return from a function. For function argument I would use `ref`, but for return types, I can't use `ref` and can't return a pointer. What should be the proper way to handle this? Do I have to change function signature (i.e. return type) For example, the following function: ``` //C++ version, from: https://github.com/bioslaD/fastp/blob/orig/src/read.cpp#L69 Read* Read::reverseComplement(){ Sequence seq = ~mSeq; string qual; qual.assign(mQuality.rbegin(), mQuality.rend()); string strand = (mStrand=="+") ? "-" : "+"; return new Read(mName, seq, strand, qual); } // D version: Read reverseComplement(){ Sequence seq = ~mSeq; dchar[] qual = cast(dchar[])mQuality.dup; reverse(qual); string strand = (mStrand=="+") ? "-" : "+"; Read newRead = new Read(mName, seq, strand, cast(string)qual); // return &newRead does not work: returning `& newRead` escapes a reference to local variable newRead return newRead; } ``` Let's not focus on the function body, I don't know how to handle the return type in cases like this for the D version.it looks like Read is a D class? in which case it already returns by reference. If you make Read a struct then all you need do is change the function signature from Read reverseComplement() to Read* reverseComplement() about the function body use mQuality.dup.representation.reverse;dchar[] qual = cast(dchar[])mQuality.dup; reverse(qual); mQuality = cast(string)qual;does not do what you want it to do.
May 24 2018
On Thursday, 24 May 2018 at 08:58:02 UTC, Nicholas Wilson wrote:On Thursday, 24 May 2018 at 08:16:30 UTC, biocyberman wrote:Thanks for the hints. `Read` in C++ and D are both classes. And the function is inside the class definition itself.[...]it looks like Read is a D class? in which case it already returns by reference. If you make Read a struct then all you need do is change the function signature from Read reverseComplement() to Read* reverseComplement() about the function body use mQuality.dup.representation.reverse;[...]does not do what you want it to do.
May 24 2018
On 2018-05-24 11:10, biocyberman wrote:Thanks for the hints. `Read` in C++ and D are both classes. And the function is inside the class definition itself.In that case specifying the type as `Read` is the correct thing to do. Note that `new` always allocates on the heap and returns a pointer or reference type. -- /Jacob Carlborg
May 24 2018
On Thursday, 24 May 2018 at 17:44:19 UTC, Jacob Carlborg wrote:On 2018-05-24 11:10, biocyberman wrote:Thanks. I did that and it worked correctly.Thanks for the hints. `Read` in C++ and D are both classes. And the function is inside the class definition itself.In that case specifying the type as `Read` is the correct thing to do. Note that `new` always allocates on the heap and returns a pointer or reference type.
May 26 2018