简体   繁体   中英

c++ passing objects to functions

I am working on a project dealing with many different operations with sets. I have a specific question about passing by reference. In the header file I have created an object called SoS which stands for set of strings and its private data members are a vector of strings called m_vos and a boolean called m_cofinite. Here is an example of my confusion with the intersection function I am creating.

SoS::makeIntersection(const SoS& B) const {

}

This is the function I am creating and It needs to create an intersection of the sets. I would approach this by looping through and putting both sets together and then removing elements that arent a part of both original sets therefore giving me the intersection of the two. I know logically how i could do this but I am confused as to what I am passing into the function and how I am able to use two sets when only one appears to be passed in(I was told I can not change the structure of the function so it needs to be this way). Any help is greatly appreciated.

The first set is this and you access its members like this->m_vos... . The second set is the one you passed in and you access its members like B.m_vos... .

However, for brevity, you can omit the this-> unless there is a name conflict between a local variable and a member variable. So you can do simply m_vos... and B.m_vos... .

Also you need to consider what your function will return? Maybe the intersection you create? That would be a third SoS variable that you might return as a result:

// return type SoS
SoS SoS::makeIntersection(const SoS& B) const {

    SoS intersection; // this is the new SoS that will be the intersection

    // fill intersection.m_vos using B.m_vos and this->m_vos

    return intersection; // return the intersection version
}

The object on which you called the function can be accessed through this . The second object is the argument to the function.

The first vector of strings is this->m_vos .
The second vector of strings is B.m_vos .

Your statement:

I was told I can not change the structure of the function so it needs to be this way

does not make sense to me.

The function does not seem to have a return value. Did you forget to include it in the posted code? How will the resultant object, the intersection, be returned to the calling function?

You cant' modify this to be the resultant object since the function is a const member function.

You cant' modify B to be the resultant object since it is passed by const& .

The way to call member functions for a class is through its objects(if not static). To get the intersection of two sets(sos objects) you'll call the function using one of the SoS object and pass the other one as the parameter reference.

Inside the member function the object which you've used to call it can be derefenrence using this pointer. Read more here and here .

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