Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a pointer to a class within a class

this is the first time I've done something like this so I'm a little uncertain how I need to do this. I have a very simple class which contains some simple values and some getters:

class Nucleotide{
    private:
        char Base;
        int Position;
        int Polymorphic;
    public:
        Nucleotide(char ch, int pos);
        int getPos();
        char getBase();
        int getPoly();
};

This class is present in another class that contains a vector of them:

class NucleotideSequence{
    private:
        std::string Name;
        std::vector<Nucleotide> Sequence;
    public:
        NucleotideSequence(std::string name, std::vector<Nucleotide> seq);
        std::string getName();
        Nucleotide getBase(int pos1);
};

I want the method of the second class called getBase to be able to take a integer - say 1, and return the first Nucleotide object in the vector. What I've written is below:

Nucleotide NucleotideSequence::getBase(int pos1)
{
    for(std::vector<Nucleotide>::iterator i = Sequence.begin(); i != Sequence.end(); i++)
    {
        if(pos1 == (*i).getPos())
        {
            return i; // Return a pointer to the correct base.
        }
    }
}

I've got Nucleotide as the return type but I was wondering really how I should change this - since if I return nucleotide because of pass by value would it not just return a copy of the object at that place in the vector? So I'd rather return a pointer/reference. I'm using an iterator in the loop so should I just return a pointer with the value of the iterator? How do I do this? In the function I return i but should I be returning i&? I'm uncertain about the specifics - presumably if I'm returning a pointer my return type needs to be Nucleotide* or perhaps Nucleotide& since & means address of? I've thought this through and read Cpp tuts but I'm still slightly unsure of the right answer.

Thanks, Ben.

like image 727
Ward9250 Avatar asked Nov 13 '13 12:11

Ward9250


People also ask

How do you return a pointer to a class object?

double *MyClass::Mymethod(); is a method return the address of a double (returning a pointer to a double). So, yes, it is correct. double * MyClass::Mymethod();

How do you return a pointer to an object from a function?

You should create a new instance to return, i.e., Foo* foo_ptr = new Foo(); This will create an object in the heap and will live until you call delete on it.

How do you return a pointer in Java?

You can use Java 8 method reference. Since you didn't show any code, it's impossible to know what your method signature is, so I'll assume void X() , i.e. no parameters and no return value. If the signature (excl. method name) is different, you need to use a functional interface other than Runnable .

Can you return a pointer to a local variable?

The return statement should not return a pointer that has the address of a local variable ( sum ) because, as soon as the function exits, all local variables are destroyed and your pointer will be pointing to someplace in the memory that you no longer own.


1 Answers

You have to return the Nucleotide by reference:

Nucleotide & NucleotideSequence::getBase(int pos1)
{
    for(std::vector<Nucleotide>::iterator i = Sequence.begin(); i != Sequence.end(); i++)
    {
        if(pos1 == (*i).getPos())
        {
            return *i; // Notice the *i instead of i
        }
    }
}

A reference works very similarly to pointer (allows you to pass the actual object, not its copy), but cannot be null and cannot point to non-existing object, so it's a lot safer than pointer.

Note though, that if you don't find the desired Nucleotide, you don't return anything, what generally is not a good idea. In this case using pointers may actually be a better idea:

Nucleotide * NucleotideSequence::getBase(int pos1)
{
    for(std::vector<Nucleotide>::iterator i = Sequence.begin(); i != Sequence.end(); i++)
    {
        if(pos1 == (*i).getPos())
        {
            return &(*i); 
        }
    }

    return nullptr;
}
like image 152
Spook Avatar answered Sep 24 '22 17:09

Spook