• Create Account

### #ActualPlethora

Posted 18 April 2013 - 11:41 AM

I feel like I should understand this stuff by now... =/

Anyway, lets say I have a class:

class someClass
{
private:
std::vector<Thing> myVector;

public:
std::vector<Thing>& getThings();  //simply returns myVector
};


Now, lets say that I have another class which contains an instance of someClass.  It has a method whose goal it is to get myVector, iterate through it and pull out some subset of the Things contained in it based on some criteria.  It wants to take that subset and put references (preferably) to those Things in a new vector which it will, itself, control.  This was my attempt at that (minus a lot of the details), and it definitely isn't storing references:

std::vector<Thing> otherVector; //Assume these are private data members in the class
someClass myClass;              //HigherClass

void HigherClass::doesSomeThings()
{
std::vector<Thing> &tempVector = myClass.getThings()
//tempVector should be the same as myVector in myClass, yes?  Like tempVector is
//another name for myVector?

for(unsigned int i = 0; i < tempVector.size(); i++)
{
if(***some random condition is met***)
{
Thing &myThing = tempVector[i];
//again, myThing should now be an alternate name for the Thing contained at
//tempVector[i], no?
otherVector.push_back(myThing);
}
}
}


So... where am I going wrong?

EDIT:  Fixed Formatting

EDIT again:  AAAARRGGGHH  apparently you just can't store references in a container like this... which makes perfect sense for very obvious reasons and I'm just an idiot. Someone tell me that my logic at least is correct if this code was for some mythical language that allowed references to be stored in containers.

### #3Plethora

Posted 18 April 2013 - 11:34 AM

I feel like I should understand this stuff by now... =/

Anyway, lets say I have a class:

class someClass
{
private:
std::vector<Thing> myVector;

public:
std::vector<Thing>& getThings();  //simply returns myVector
};


Now, lets say that I have another class which contains an instance of someClass.  It has a method whose goal it is to get myVector, iterate through it and pull out some subset of the Things contained in it based on some criteria.  It wants to take that subset and put references (preferably) to those Things in a new vector which it will, itself, control.  This was my attempt at that (minus a lot of the details), and it definitely isn't storing references:

std::vector<Thing> otherVector; //Assume these are private data members in the class
someClass myClass;              //HigherClass

void HigherClass::doesSomeThings()
{
std::vector<Thing> &tempVector = myClass.getThings()
//tempVector should be the same as myVector in myClass, yes?  Like tempVector is
//another name for myVector?

for(unsigned int i = 0; i < tempVector.size(); i++)
{
if(***some random condition is met***)
{
Thing &myThing = tempVector[i];
//again, myThing should now be an alternate name for the Thing contained at
//tempVector[i], no?
otherVector.push_back(myThing);
}
}
}


So... where am I going wrong?

EDIT:  Fixed Formatting

### #2Plethora

Posted 18 April 2013 - 11:34 AM

I feel like I should understand this stuff by now... =/

Anyway, lets say I have a class:

class someClass
{
private:
std::vector<Thing> myVector;

public:
std::vector<Thing>& getThings();  //simply returns myVector
};


Now, lets say that I have another class which contains an instance of someClass.  It has a method whose goal it is to get myVector, iterate through it and pull out some subset of the Things contained in it based on some criteria.  It wants to take that subset and put references (preferably) to those Things in a new vector which it will, itself, control.  This was my attempt at that (minus a lot of the details), and it definitely isn't storing references:

std::vector<Thing> otherVector; //Assume these are private data members in the class
someClass myClass;              //HigherClass

void HigherClass::doesSomeThings()
{
std::vector<Thing> &tempVector = myClass.getThings()
//tempVector should be the same as myVector in myClass, yes?  Like tempVector is
//another name for myVector?

for(unsigned int i = 0; i < tempVector.size(); i++)
{
if(***some random condition is met***)
{
Thing &myThing = tempVector[i];
//again, myThing should now be an alternate name for the Thing contained at
//tempVector[i], no?
otherVector.push_back(myThing);
}
}
}


So... where am I going wrong?

EDIT:  Ugh, formatting, shouldn't the final post look like what is on the screen when I preview it?

### #1Plethora

Posted 18 April 2013 - 11:32 AM

I feel like I should understand this stuff by now... =/

Anyway, lets say I have a class:

class someClass
{
private:
std::vector<Thing> myVector;

public:
std::vector<Thing>& getThings();  //simply returns myVector
};


Now, lets say that I have another class which contains an instance of someClass.  It has a method whose goal it is to get myVector, iterate through it and pull out some subset of the Things contained in it based on some criteria.  It wants to take that subset and put references (preferably) to those Things in a new vector which it will, itself, control.  This was my attempt at that (minus a lot of the details), and it definitely isn't storing references:

std::vector<Thing> otherVector; //Assume these are private data members in the class               someClass myClass;              //HigherClass

void HigherClass::doesSomeThings()
{
std::vector<Thing> &tempVector = myClass.getThings()
//tempVector should be the same as myVector in myClass, yes?  Like tempVector is
//another name for myVector?

for(unsigned int i = 0; i < tempVector.size(); i++)
{
if(***some random condition is met***)
{
Thing &myThing = tempVector[i];
//again, myThing should now be an alternate name for the Thing contained at                                      //          //tempVector[i], no?
otherVector.push_back(myThing);
}
}
}


So... where am I going wrong?

PARTNERS