Translate C/C++ patern: return a pointer
biocyberman
biocyberman at gmail.com
Thu May 24 08:16:30 UTC 2018
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.
More information about the Digitalmars-d-learn
mailing list