Reputation: 9649
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> fst, snd;
vector<int> trd = fst;
cout << boolalpha << (fst == snd) << endl;
}
The operator== is overloaded for vectors fst, snd to check element-wise equality.
Instead, how to check that fst references to the same object as trd? (In this case the keyword is used in Python.)
@EDIT
As all the answers and comments said, object, object pointer and object reference are distinguished concepts in C++:
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
int main() {
// object pointer, heap allocated
shared_ptr<vector<int>> fst(new vector<int>);
auto snd = fst;
fst->push_back(10);
cout << boolalpha << (fst->size() == snd->size()) << endl;
snd->push_back(20);
cout << (fst->size() == snd->size()) << endl;
fst = make_shared<vector<int>>();
cout << (fst == snd) << endl;
// object reference, aka alias
vector<int> trd{3};
auto &foth = trd;
trd[0] = 30;
cout << (trd[0] == foth[0]) << endl;
foth[0] = 40;
cout << (trd[0] == foth[0]) << endl;
trd = {};
cout << (trd == foth) << endl;
// object copy, stack allocated
vector<int> fith{5};
auto sith = fith;
fith[0] = 50;
cout << (fith[0] == sith[0]) << endl;
sith[0] = 60;
cout << (fith[0] == sith[0]) << endl;
fith = {};
cout << (fith == sith) << endl;
}
Upvotes: 1
Views: 107
Reputation: 16850
In order to experiment around the question, here is an example
that tries to mimic the behavior of Python.
The values are only accessible through references (smart-pointers
here) so you have to explicitly say if you want to compare the
references (addresses) or the values.
/**
g++ -std=c++17 -o prog_cpp prog_cpp.cpp \
-pedantic -Wall -Wextra -Wconversion -Wno-sign-conversion \
-g -O0 -UNDEBUG -fsanitize=address,undefined
**/
#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>
using vec_t = std::vector<std::shared_ptr<int>>;
int
main()
{
const vec_t v1={std::make_shared<int>(1),
std::make_shared<int>(2),
std::make_shared<int>(3)};
const vec_t v2={std::make_shared<int>(1), // the three same values
std::make_shared<int>(2), // but it's just a
std::make_shared<int>(3)}; // coincidence...
const vec_t v3=v1; // references to the same 3 data are shared between both vectors
std::cout << std::boolalpha;
std::cout << "compare references (addresses):\n";
std::cout << "v1==v2 --> " << (v1==v2) << '\n'; // false
std::cout << "v1==v3 --> " << (v1==v3) << '\n'; // true
const auto same_values=
[&](const auto &lhs, const auto &rhs)
{
return std::equal(cbegin(lhs), cend(lhs), cbegin(rhs), cend(rhs),
[&](const auto &l, const auto &r)
{
return *l==*r; // dereference before comparing the values
});
};
std::cout << "compare values:\n";
std::cout << "same_values(v1, v2) --> " << same_values(v1, v2) << '\n'; // true
std::cout << "same_values(v1, v3) --> " << same_values(v1, v3) << '\n'; // true
return 0;
}
Upvotes: 1
Reputation: 473407
You are using C++, not Python. In C++, a variable of object type is the object; it doesn't "reference" anything. fst
and snd
are separate variables and therefore are separate objects. The only valid comparison is to ask the value of their objects if they are equivalent.
Variables of reference type don't provide the operation you're looking for either. A reference variable is intended to act (as much as practical) exactly like the object it references. References are commonly said to be a different name for the same object. So asking the question if two reference variables reference the same object is considered irrelevant in C++.
If you need to make this distinction between "referencing the same object" and "objects with the same value" (and you rarely do), then you need to use the C++ mechanism that provides that distinction: pointers. There, you can check for pointer equivalence (and thus "references the same object") or dereference the pointers and check for object equivalence.
Upvotes: 3