I have a class which has a member of type std:vector
private:
std::vector<int> myVector;
I have created Get method to access myVector
1. const std::vector<int> GetMyVector() const;
2. const void GetMyVector(std::vector<int>& vec) const;
The implementations are as follows respectively:
1. const std::vector<int> MyClass::GetMyVector() const
{
return myVector;
}
2. const void MyClass::GetMyVector(std::vector<int>& vec) const
{
vec = myVector;
}
Which one of the two Get methods is better and why?
I'd prefer option 3:
const std::vector<int>& MyClass::GetMyVector() const
{
return myVector;
}
Your option 1 returned a copy of myVector. This returns a const (so read-only) reference to the class member.
Why return the vector at all?
int MyClass::GetItem(const size_t index) const
{
return myVector[index];
}
First of all you are exposing your implementation when you return a private member of a class from a member function, which usually is bad design. Take a look at #JoachimPileborg's solution for an example of how to avoid this.
If you want to return a copy then you should return by value.
If you want to return a reference to an object then return by reference. However, bear in mind that when the object is destructed you will end up with a dangling reference, e.g.
class Foo {
public:
std::vector<int>& getVec() {
return myVec;
}
private:
std::vector<int> myVec;
};
int main() {
Foo* f = new Foo();
std::vector<int>& myRef = f->getVec();
delete f;
std::cout << myRef.size(); // The demons come! Dangling reference!
}
Because of this, it is often the right thing to return a copy instead of a reference.
If you are returning an object by copy then there is no any sense to declare it as const. So instead of
const std::vector<int> MyClass::GetMyVector() const
{
return myVector;
}
I would write
std::vector<int> MyClass::GetMyVector() const
{
return myVector;
}
The second declaration is worst than the first one because it only confuses users. It is not clear whether the corresponding data member of the class is assigned to the parameter or this method makes some changes of the parameter without assigning the corresponding data member to the parameter.
So considering suggested variants by you I would choise declaration
std::vector<int> MyClass::GetMyVector() const
{
return myVector;
}
As a rule of thumb always try to return const reference of a class member .
So use const std::vector<int> & MyClass::GetMyVector() const
Related
I have a class A containing a vector of shared_ptr<B>.
I implemented a getter to this vector.
In some cases, it would be nice to ensure that the content in B does not change (make B read only or a const reference).
If I would not have used vector<shared_ptr<B>> but rather vector<B> I could simply write two getters, one returning a const reference (read only), and one returning a reference only (manipulation possible). #
Is there a way to do the same thing with a vector<shared_ptr<B>>?
Maybe it is easier to understand the problem in this code:
#include <vector>
#include <memory>
using namespace std;
class B{
public:
explicit B(int i) : i_{i} {}
void set_i(int i){i_ = i;}
private:
int i_ = 0;
};
class A{
public:
const vector<shared_ptr<B>> &get_vb(){return vb;}
// const vector<shared_ptr<const B>> &get_vb_const(){return vb;} // I would like to return a const vector with const elements in some cases
private:
vector<shared_ptr<B>> vb{make_shared<B>(1), make_shared<B>(10), make_shared<B>(100)};
};
int main() {
A a;
const auto &vb = a.get_vb();
vb[0]->set_i(2);
// const auto &vb_const = a.get_vb_const(); // somehow I would like to gain this vector without being able to modify the elements
// vb_const[0]->set_i(2); // should throw error
return 0;
}
You need to construct a new vector with the desired elements:
const vector<shared_ptr<const B>> get_vb_const() const {
return vector<shared_ptr<const B> > {vb.cbegin(), vb.cend()};
}
Note that the function doesn't return a reference now because we are creating a temporary and returning it.
transform vector of shared_ptr with non const elements to vector of shared_ptr with const elements
You can use the constructor of vector that accepts a pair of iterators to perform the conversion.
You can avoid the overhead of allocating and copying a vector by implementing a custom const iterator for your class.
I have a c++ class that has a private array member.
class array_test
{
public:
std::tuple<double*, int> get_vector_member();
private:
double test_array[3];
};
In C++11 is there a better method call to get a double[] that doesn't involve grabbing double as a pointer. I am worried that either myself or someone else will delete the pointer and cause some strange undefined behavior.
I hope the fact I didn't include an implementation for get_vector_member is an issue.
How about providing typical C++ field accessor functions, with a const and non-const overload, returning a reference to the field?
class X
{
double data[3];
public:
// Accessors
double (&get())[3] { return data; }
const double (&get() const)[3] { return data; }
};
In C++14 this code is a bit easier to write:
auto & get() { return data; }
const auto & get() const { return data; }
You could make it an std::array. Some reasons why they're superior to raw arrays:
Easy element access with the front and back member functions.
They're copy constructible and assignable.
Optional bounds-checked access.
Cool comparison operations like ==, != and <.
The fill member function.
No weird syntax when passing by reference.
Then return that by reference:
class array_test
{
public:
const std::array<double, 3>& get_array_member() const;
private:
std::array<double, 3> test_array;
};
Suppose I have a vector of shared pointers of objects of class A. I want to collect all the return values of method A::foo for all of the A objects and store them in some container, say another vector.
Can this be done with std::transform or std::for_each and std::bind or some boost function ?
class A {
public:
string foo(int p);
};
std::vector<shared_ptr<A>> vec;
std::transform is fine for this:
std::vector<std::string> string_vec;
const int magic_number = 42;
std::transform(std::begin(vec),
std::end(vec),
std::back_inserter(string_vec),
[magic_number](std::shared_ptr<A> a){ return a->foo(magic_number); } );
Obviously, you need to figure out which int you want to pass to A::foo(int).
In C++03, you can replace the lambda by a functor or function:
struct get_foo
{
get_foo(int n) : n_(n) {}
bool operator() (std::shared_ptr<A> a) const { return a->foo(n); }
private:
int n_;
};
const int magic_number = 42;
std::vector<std::string> string_vec;
std::transform(std::begin(vec),
std::end(vec),
std::back_inserter(string_vec),
get_foo(magic_number));
I have a question related to operator overloading, and it is easy to define a class as well as its operator overloading function as the following codes illustrate:
typedef std::vector<std::vector<int> > ARRAY;
class ABC
{
public:
ABC():a(0)
{
};
int a;
ABC& operator = (int value)
{
a = value;
return *this;
}
ABC(int value)
{
a = value;
}
};
void obtain_priority_array(const std::vector<double> &weighting, const ABC &priority_array=NULL)
{
}
int main()
{
vector<double> weighting;
weighting.push_back(0.8);
weighting.push_back(0.9);
weighting.push_back(0.6);
weighting.push_back(0.3);
weighting.push_back(0.5);
ABC test;
obtain_priority_array(weighting, test);
return 0;
}
In the above example, class ABC redefined operator = so that the function void obtain_priority_array(const std::vector<double> &weighting, const ABC &priority_array=NULL) can have a default argument const ABC &priority_array=NULL. My question is if the last parameter in the function comes from STL, for example, const std::vector<int> &priority_array=NULL, how can we redefine operator =. Thanks!
EDIT:
void obtain_priority_array(const std::vector &weighting, const std::vector<int> &sample=NULL) failed!
Your misconceptions start with the proposal to add operator= to allow for a default argument of that type. In your example, it is not operator= being called, but ABC(int).
The reason your code isn't being accepted when using std::vector is that NULL translates to 0 (at least it does almost all of the time you'll see it), and the only constructor of std::vector that can take 0, the one taking a count of how many items, is marked explicit.
To fix the immediate problem, the syntax could be changed to:
const std::vector<int> &priority_array = std::vector<int>(0)
However, this introduces different semantics. By your use of NULL, it looks like you were expecting it to represent no vector. This version will provide an empty vector for use if none is given. It will not be no vector at all. If you want that distinction, you should use boost's optional library, or a simple pointer, as references are not the right tool.
References cannot be NULL, your problem has nothing to do with operator overloading. If you want to be able to handle NULL as the default value, switch the parameter type from reference to pointer.
void obtain_priority_array( const std::vector<double>& weighting,
const ABC *priority_array = NULL)
{
if( priority_array == NULL ) {
// blah
} else {
// more blah
}
}
Another option is to use something like Boost.Optional to represent the optional parameter.
typedef boost::optional<ABC> maybe_ABC;
void obtain_priority_array( const std::vector<double>& weighting,
const maybe_ABC& priority_array = maybe_ABC() )
{
if( !priority_array ) {
// blah
} else {
// more blah
}
}
When you use = to create a reference, you're not calling operator= at all. You're initializing the reference.
Instead of using NULL you can create a static instance of the class to represent a null value.
static const ABC ABC_NULL;
void obtain_priority_array(const std::vector<double> &weighting, const ABC &priority_array=ABC_NULL)
{
if (&priority_array == &ABC_NULL) // the default was used
Of course it would be easier to just use a pointer instead of a reference.
See the code below - I am trying to put a const object into a vector. I know the answer is "STL containers require objects to be assignable and copy constructable", but, without citing the standard, can anyone explain what the problem with doing this is? I don't understand why a class like this could not be copied (besides that c++ doesn't allow it).
All it is is a value stored that is not allowed to be changed - why can't putting it in a vector simply create another one of these objects?
#include <vector>
// Attempt 1
// /home/doriad/Test/Test.cxx:3:8: error: non-static const member ‘const int MyClass::x’, can’t use default assignment operator
// struct MyClass
// {
// int const x;
// MyClass(int x): x(x) {}
// };
//
// int main()
// {
// std::vector<MyClass> vec;
// vec.push_back(MyClass(3));
// return 0;
// }
// Attempt 2
// /home/doriad/Test/Test.cxx:28:23: error: assignment of read-only member ‘MyClass::x’
struct MyClass
{
int const x;
MyClass(int x): x(x) {}
MyClass& operator= (const MyClass& other)
{
if (this != &other)
{
this->x = other.x;
}
return *this;
}
};
int main()
{
std::vector<MyClass> vec;
vec.push_back(MyClass(3));
return 0;
}
EDIT:
It is possible to do this with std::set and std::list. I guess it is the sort() function in std::vector that uses assignment. This is not UB right?
#include <set>
// Attempt 1
struct MyClass
{
int const x;
MyClass(int x): x(x) {}
bool operator< (const MyClass &other) const;
};
bool MyClass::operator<(const MyClass &other) const
{
if(this->x < other.x)
{
return true;
}
else if (other.x < this->x)
{
return false;
}
}
int main()
{
std::set<MyClass> container;
container.insert(MyClass(3));
return 0;
}
EDIT2: (Removing a bunch of stuff that doesn't have to work) The C++11 standard states that the insert method for vector and deque (and the default implementation of push_back for that matter) requires the value type to be CopyAssignable, i.e., the value supports:
t= v;
Classes and structs with const members are not CopyAssignable by default, so what you want to do won't work.
This doc (n3173) has an explanation for the various container requirements.
One possible solution would be to store pointers to the objects in the vector, because pointers are assignable and copy constructable.
Another possible solution would be to declare x without the const keyword, but ensure that it cannot be modified through encapsulation (i.e. you should declare it as private and don't modify from anywhere outside the constructor)..
When you place an object of type MyClass in the std::vector, the vector will make a copy of the object for storage, and not the object you passed to it.