0

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.

merv
  • 67,214
  • 13
  • 180
  • 245
jwillis0720
  • 4,329
  • 8
  • 41
  • 74
  • 2
    `seqan::Dna5String _raw_sequence = raw_sequence;` This creates a local variable called `_raw_sequence` which is destroyed when the constructor returns. Did you mean to store it in the member variable `_raw_sequence`? – Neil Kirk Mar 15 '15 at 02:41
  • oh so it should just be _raw_sequence = raw_sequence? If I declare a type it overwrites? – jwillis0720 Mar 15 '15 at 02:47
  • 2
    Yes. Or use the initialization list. You should also declare reference parameters as const, if they are not modified by the constructor. – Neil Kirk Mar 15 '15 at 02:48
  • 1
    Adding the type makes it a variable declaration with an initializer, instead of an assignment. If you declare a new variable in a scope it hides the one in the outer scope, in this case the class scope. You're using the wrong syntax anyway. Member variables should be initialized via an initialier list on the constructor. – user207421 Mar 15 '15 at 03:01
  • if someone wants to add as answer, i'll be happy to mark – jwillis0720 Mar 15 '15 at 03:40

1 Answers1

1

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)
{
//....
};
Serge
  • 634
  • 4
  • 9