www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Translate C/C++ patern: return a pointer

reply biocyberman <biocyberman gmail.com> writes:
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
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
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
parent reply biocyberman <biocyberman gmail.com> writes:
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:
 [...]
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.
Thanks for the hints. `Read` in C++ and D are both classes. And the function is inside the class definition itself.
May 24 2018
parent reply Jacob Carlborg <doob me.com> writes:
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
parent biocyberman <biocyberman gmail.com> writes:
On Thursday, 24 May 2018 at 17:44:19 UTC, Jacob Carlborg wrote:
 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.
Thanks. I did that and it worked correctly.
May 26 2018