简体   繁体   中英

Private member variable is null when passed from constructor to member function

I'm pretty new to OOP in c++ so bear with me here.

In my header that defines my member variables;

class AntibodyJunction
{
private:  
    //raw seq
    seqan::Dna5 _raw_sequence;
    //private funcs
    void _setVGeneQueryStartTranslation();

public:
    //V D and J constructor
    AntibodyJunction(AlignAntibody &, AlignAntibody &, AlignAntibody &, seqan::Dna5 &);
    ~AntibodyJunction() {};
};

and in cpp

AntibodyJunction::AntibodyJunction(AlignAntibody & VGene, AlignAntibody &JGene,AlignAntibody & DGene, seqan::Dna5 & raw_sequence)
{
    //...some other declaration...//
    seqan::Dna5String _raw_sequence = raw_sequence;
    std::cout <<  "constructor parameter\n" << raw_sequence << std::endl;
    std::cout << "Template dna5\n"  << _raw_sequence << std::endl;
    _setVGeneQueryStartTranslation();

};

void AntibodyJunction::_setVGeneQueryStartTranslation(){
    std::cout << "other raw seq\n" << _raw_sequence << std::endl;
    //...lots of other stuff
}

and the output ->

constructor parameter
CAGCGATTAGTGGAGTCTGGGGG
Template dna5
CAGCGATTAGTGGAGTCTGGGGG
other raw seq

the member variable _raw_sequence is blank when I try to access it within a class function. I understand that I could just do everything in the constructor, but I'd like to understand why it is resetting. seqan::Dna5 is just a template container for dna strings from the seqan library for working with biological data. It holds dna sequences. Here is the doc. It's confusing because everything else I'm accessing in this function seems to be available.

As commented above, you instantiate a _raw_sequence variable in your constructor rather than referencing your member variable like so:

this->_raw_sequence = raw_sequence ; //this pointer used to indicate member var

I suggest initializing member variables through initializer lists in the future to avoid such problems. You could rewrite your constructor as such:

AntibodyJunction::AntibodyJunction( const AlignAntibody & VGene, const AlignAntibody &JGene, const AlignAntibody & DGene,const  seqan::Dna5 & raw_sequence)
: _raw_sequence(raw_sequence)
{
//....
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM