Move the same unique_ptr into function in a loop - c++

Whats the best way to redesign the following error prone code:
void ClassA::methodA(std::unique_ptr<ClassB::ISomeInterface> obj){
for (int i = 0; i < 10; i++) {
methodB(std::move(obj)); // the obj pointer is undefined on second iteration here after the move
}
}
void ClassA::methodB(std::unique_ptr<ClassB::ISomeInterface> obj){
..........
}
The goal is to pass the same unique_ptr to function multiple times.

If you don't want to transfer ownership just pass the raw pointer or a reference. If the functions are going to store the pointer a shared_ptr would be more appropriate:
void ClassA::methodA(std::unique_ptr<ClassB::ISomeInterface> obj){
for (int i = 0; i < 10; i++) {
methodB(*obj);
}
}
void ClassA::methodB(ClassB::ISomeInterface& obj){
..........
}

Pass it by (optionally const) reference to methodB.
So instead of having
void ClassA::methodB(std::unique_ptr<ClassB::ISomeInterface> obj);
you can have either of the following
void ClassA::methodB(const ClassB::ISomeInterface& obj);
or
void ClassA::methodB(ClassB::ISomeInterface& obj);

Related

C++ copy object

I have a class Game which contains an other object GameGrid:
Game.h
class Game
{
public:
Game(uint32_t GridSize);
private:
GameGrid _CurrentGrid;
}
Game.cpp
Game::Game(uint32_t GridSize) : _CurrentGrid(GridSize)
{
}
GameGrid.h
class GameGrid
{
public:
GameGrid(uint32_t GridSize);
void setGrid(const Grid& Grid);
const GameGrid::Grid getGrid(void) const;
private:
Grid _Grid;
}
GameGrid.cpp
GameGrid::GameGrid(uint32_t GridSize)
{
uint32_t i = 0;
uint32_t j = 0;
_Grid.assign(GridSize, std::vector<unsigned int>(GridSize));
for (i = 0; i < GridSize; i++)
{
for (j = 0; j < GridSize; j++)
{
_Grid.at(i).at(j) = 0;
}
}
}
void GameGrid::setGrid(const GameGrid::Grid& Grid)
{
_Grid = Grid;
}
const GameGrid::Grid GameGrid::getGrid(void) const
{
return _Grid;
}
Now I have my application, which uses the game class
Game* MyGame = new Game(4);
How can I create a copy function for this pointer to the Game-Object, so that I can clone the object.
I´ve tried it with the assignment operator
Game& operator=(Game const& Ref);
Game& Game::operator=(Game const& Ref)
{
if (&Ref != this)
{
this->~Game2048();
new (this) Game2048(Ref);
}
return *this;
}
But this solution doesn´t work and my original object got changed too, when I change the clone.
Does someone has a hint for me?
Thank you!
Don't create the copy constructor yourself and let the compiler do it for you, then just use objects:
Game MyGame{4};
Game gameClone = MyGame;
There's no reason to use a raw pointer here.
I agree to what #SombreroChicken hast said.
I just want to point out the difference between a deep copy and shallow copy.
If your class Contains some pointer as private member data, you have to be aware, that only the pointer is copied and not the data to which is being pointed. Here is a nice in depth explanation of what I said.

Member-Function Pointer in Threads

I have made a class which uses an array of member-functions to initialize an array of threads.
I do not know how to pass the function pointer to the thread constructor. There is few documentation about this topic.
class.h
#define N_FUNCTIONS 23
class TradingData
{
public:
void EXECUTE();
void Book();
void Charts();
void Company();
void Dividends();
void Earnings();
void EffectiveSpread();
void Financials();
void KeyStats();
void LargestTrades();
void List();
void Logo();
void News();
void OHLC();
void Peers();
void Previous();
void Price();
void Quote();
void Relevant();
void Splits();
void TimeSeries();
void VolumeByVenue();
void S_Previous();
void S_Symbols();
private:
std::thread p_thread[N_FUNCTIONS];
typedef void (TradingData::*overall)();
overall p_overall[N_FUNCTIONS] = {
&TradingData::Book,
&TradingData::Charts,
&TradingData::Company,
&TradingData::Dividends,
&TradingData::Earnings,
&TradingData::EffectiveSpread,
&TradingData::Financials,
&TradingData::KeyStats,
&TradingData::LargestTrades,
&TradingData::List,
&TradingData::Logo,
&TradingData::News,
&TradingData::OHLC,
&TradingData::Peers,
&TradingData::Previous,
&TradingData::Price,
&TradingData::Quote,
&TradingData::Relevant,
&TradingData::Splits,
&TradingData::TimeSeries,
&TradingData::VolumeByVenue,
&TradingData::S_Symbols,
&TradingData::S_Previous
};
class.cpp
void TradingData::EXECUTE()
{
for (int i = 0; i < N_FUNCTIONS; i++) {
p_thread[i] = std::thread((this->*p_overall[i])()); //here is the problem
}
for (int i = 0; i < N_FUNCTIONS; i++) {
p_thread[i].join();
}
std::cout << "finished successfully" <<std::endl;
}
I got the next error:
Error C2440 '': cannot convert from 'void' to 'std::thread'
You should write call;
p_thread[i] = std::thread(TradingData::p_overall[i], this);
If you call a member function the class name will be include in the call.
p_thread[i] = std::thread((this->*p_overall[i])());
This will pass the return value of the member function being called to the thread constructor. But as you do not return something callable, this will even fail to compile, of course.
Be aware that the object on which you call a member function actually is passed (transparently for you) as first parameter to the function being called. And this is what you need to reflect when creating the thread:
p_thread[i] = std::thread(p_overall[i], *this);
The thread upon starting will now call the member function with *this as first argument. Be aware that member functions in reality accept a reference, although this, inside the function, is defined as pointer, thus dereferencing the this-pointer...
Sometimes, a lambda can be useful, which looks like this here:
std::thread t(this, i {(this->*p_overall[i])(); });
Sure, overkill in given case, but might be useful in other situation sometime in future...

C++; class method pointer; lambda; passing lambda as member function pointer;

Here is what I try to achieve:
class MyClass
{
public:
using Callback = void(MyClass::*)(uint8_t idx);
void forEach(Callback callback);
private:
int m_buf[64];
int m_x;
int m_y;
MyObject m_object;
}
void MyClass::forEach(Callback callback)
{
size_t s = m_object.size();
for(size_t i = 0; i < s; i++)
callback(i);
}
void MyClass::f1()
{
forEach([this](uint8_t idx)
{
m_buf[idx]++;
});
}
void MyClass::f2()
{
forEach([this](uint8_t idx)
{
m_buf[idx] = m_x + m_y * idx;
});
}
So there are a bunch of ways to modify m_buf[]. In order to avoid copying and pasting 'get size + for loop', I want to add a forEach method and pass lambdas as callbacks.
this is captured to have access to class members.
What is the right way to achieve the same result?
Thanks.
PS: compilation of this example returns error 'cannot convert ::lambda ....'
ANSWER:
With "Passer By" answer, I finished with the code:
// Class declaration in header
using Callback = std::function<void(uint8_t)>;
void forEach(Callback callback);
// forEach() is as above
// forEach() call looks like
forEach([this](uint8_t idx) {
m_buf[idx] = m_x + m_y * idx;
});
Also I found some related questions-anwers which might be useful
Passing lambda as function pointer - "5gon12eder" answer.
C++ lambda with captures as a function pointer
You have mistaken the semantics of a member function pointer
void (MyClass::*)(uint8_t idx)
is a pointer to a member function of MyClass that accepts a uint8_t, it is not anything else. You call it as
MyClass::Callback c = &MyClass::SomeMemberFunction;
MyClass mc;
(mc.*c)(0); // equivalent to...
mc.SomeMemberFunction(0);
Secondly, the purpose of for_each is so that the callable object passed in need not know the internals of the object, as such, you shouldn't pass in an index in the loop.
What you actually want is to pass in a callable object accepting a reference called on each object. This can be done in two ways
template<typename Callable>
void for_each1(Callable&& call)
{
for(size_t i = 0; i < size(); i++)
call(m_buf[i]);
}
#include<functional>
void for_each2(std::function<void (int&)>&& fn)
{
for(size_t i = 0; i < size(); i++)
fn(m_buf[i]);
}
Both can be called with a lambda
MyClass mc;
mc.for_each1([](int& i) { i++; });
mc.for_each2([&mc](int& i) { i += mc.increment(); });
where mc.increment is what that instance wants to be incremented by.

copy local objects by reference

Here's my problem,
Class MClass {
public:
void Add(OtherClass* objects) {
_objects = objects;
}
private:
OtherClass* _objects;
}
//otherfile.cpp
void Setup() {
MClass myObj;
OtherClass obj[NUMBER_OF_OBJECTS];
//obj initialization here
//...
myObj.Add(obj);
}
It will cause a RT error because the *obj diminishes after the end of the function body.
But, how can make this one valid?
I like to initialized first an object before assigning it to other class.
EDIT
I don't want to use storage classes or something here, I just want a raw array since it is very expensive for me to use. Its functionality will not lessen my problem here.
So how do I do that in a raw-array style?
Class MClass {
public:
void Add(std::vector<OtherClass> objects) {
_objects = std::move(objects);
}
private:
std::vector<OtherClass> _objects;
}
//otherfile.cpp
void Setup() {
MClass myObj;
std::vector<OtherClass> obj(NUMBER_OF_OBJECTS);
myObj.Add(std::move(obj));
}
In your example, you store a pointer to a local array. If the method ends, the array goes out of scope and doesn't exist anymore.
This is the reason, your pointer is not valid anymore. If you want to solve this, learn about the scope of variables in C++.
It is not completely clear what you are trying to do, but you could store a collection of objects instead of a pointer:
class MClass
{
public:
void Add(const std::vector<OtherClass>& objects) {
objects_ = objects;
}
void Add(std::vector<OtherClass>&& objects) {
objects_ = std::move(objects);
}
private:
std::vector<OtherClass> objects_;
};
then
void Setup()
{
MClass myObj;
std::vector<OtherClass> obj(NUMBER_OF_OBJECTS);
//obj initialization here
//...
myObj.Add(std::move(obj)); // move obj's contents onto myObs's objects.
}
Stop using raw arrays, and use either std::vector or std::array. Then you don't have to worry about it anymore.
If you really want to do it manually, you have to copy is manually as well. Using e.g. std::vector and std::move is more effective, but here you go:
Class MClass {
public:
MClass()
: _objects(nullptr), _count(0)
{}
MClass(const MClass& other)
: _objects(nullptr), _count(0)
{
Add(other._objects, other._count);
}
~MClass()
{
if (_objects != nullptr)
delete [] _objects;
}
void Add(const OtherClass* objects, const size_t count)
{
if (_objects != nullptr)
delete [] _objects;
_objects = new [count];
for (size_t i = 0; i < count; i++)
_objects[i] = objects[i];
_count = count;
}
MClass& operator=(const MClass& other)
{
Add(other._objects, other._count);
}
private:
OtherClass* _objects;
size_t _count;
};
// ...
myObj.Add(obj, NUMBER_OF_OBJECTS);
As you can see, it's a lot of more code, which makes it harder to follow and debug, and also larger possibility of errors. And not as "effective" as I said above.

Some data lost while passing std::vector as in/out reference parameter

I'm having trouble transferring some data contained in a vector between my functions. The situation is as follows:
void generateObjects(std::vector<MyClass> &objects)
{
objects.clear();
//Fill objects vector
std::vector<MyClass> p;
//This 4-line pattern is repeated a number of times to generate all objects and store them in variable 'objects'
p.clear();
generateSomeOfTheObjects(p); //p is again passed by ref. in/out parameter
for(uint j = 0; j < p.size(); p++){
objects.push_back(p[j]);
}
//Print some members of the objects - works fine
for(uint i = 0; i < objects.size(); i++){
printf("%f ",objects[i].mymember->myElm);
}
}
int main()
{
std::vector<MyClass> objects;
generateObjects(objects);
//Print size of vector - size is correct it is the same as it is in generateObjects func
printf("%lu\n",objects.size());
//Again print members of the objects - some members are retained after the function call, some are lost.
//The one below doesn't work, mymember is a pointer to another object and its member myElm seems not initialized.
for(uint i = 0; i < objects.size(); i++){
printf("%f ",objects[i].mymember->myElm);
}
//Here I need to pass the objects to another read-only function
...
}
I have searched the internet for similar cases and actually found many, but I couldn't apply the same fixes to my code. I'm trying to reach a member of an object pointed to by a member of a MyClass instance (objects[i].mymember->myElm) What possibly am I missing here?
Probably the error lies in the implementation of MyClass. I'd say that this class contains some pointer that is initialized with the address of a local variable, so when you return from some of the functions that pointer points to a destroyed object.
That would undefined behavior but it may work by chance. When you return from the first function, the stack memory is finally overwritten and your data is lost.
UPDATE: Thanks to the insight by #chris in the comments below, the most likely reason is that your MyClass does not have a copy constructor, but it does have a pointer member.
Something like this:
class MyClass
{
public:
Member *mymember;
MyClass()
{
mymember = new Member;
}
~MyClass()
{
delete mymember;
}
};
Now what happens if you use the compiler generated default copy constructor (or the copy operator)?
void foo()
{
MyClass a;
{
MyClass b(a);
}
//a.mymember is no longer valid
}
Both a and b share the same pointer mymember, so when one of them is destroyed, the mymember is deleted and the other one holds a dangling pointer.
That's why we have the rule of three. It states:
Whenever you define a non-default destructor, you most likely will want also a non-default copy-constructor and a non-default copy-operator.
Now you have to decide if you want to share the ownership of the mymember or if you want to copy it. The first one is best done with smart pointers (shared_ptr) and the second one with deep copy.
For example, deep copy:
class MyClass
{
public:
Member *mymember;
MyClass()
{
mymember = new Member;
}
MyClass(const MyClass &c)
{
mymember = new Member(c.mymember);
}
MyClass &operator=(const MyClass &c)
{
if (this != &c) //be aware of self-copy
{
delete mymember;
mymember = new Member(c.mymember);
}
return *this;
}
~MyClass()
{
delete mymember;
}
};
And with shared pointers:
class MyClass
{
public:
std::shared_ptr<Member> mymember; //or boost::shared_ptr if old compiler
MyClass()
:mymember(new Member)
{
}
//no custom-made destructor -> no rule of 3
};
Perhaps unrelated to you question, but this:
void generateObjects(std::vector<MyClass> &objects)
{
objects.clear();
std::vector<MyClass> p;
p.clear();
generateSomeOfTheObjects(p);
for(uint j = 0; j < p.size(); p++){
objects.push_back(p[j]);
}
for(uint i = 0; i < objects.size(); i++){
printf("%f ",objects[i].mymember->myElm);
}
}
Is the same as this:
void generateObjects(std::vector<MyClass> &objects)
{
objects.clear();
generateSomeOfTheObjects(objects);
std::reverse(objects.begin(), objects.end());
for(uint i = 0; i < objects.size(); i++) {
printf("%f ",objects[i].mymember->myElm);
}
}
You copy issues, as #rodrigo mentioned, is that you are not doing deep copies with your copy constructors.