0

Suppose I have objects holding some data:

class myclass
{
  //constructors,destructor,setters,getters
  private:
  int latitude;
  int longitude;
} 

Say I need to perform some chained operations on these objects, so I can write some functions returning a myclass object to be used inside other functions. Example:

myclass sum (myclass* a, myclass* b)
{
    // how to define c?
    c.longitude = a->get_longitude() + b->get_longitude();
    return c;
}

and another function using the returned object:

int anotherfunc (myclass* a, myclass* b)
{
    return a->get_longitude() - sum(a,b).get_longitude();
}

The question is: how should I define the object c needed for these functions ?

The first idea was to create a dummy object with scope on the file where to store the object c. This approach won't work though in a multithread environment where each thread can perform operations at various times it could lead to troubles.

How to deal with chained operations as stated in a multithreaded program?

YSC
  • 38,212
  • 9
  • 96
  • 149
Podarce
  • 473
  • 1
  • 6
  • 22
  • 1) What's `c` in `sum`? It is undeclared, at any point of your example. Please provide [mcve]. 2) Do you want something like [`std::lock_guard`](http://en.cppreference.com/w/cpp/thread/lock_guard)? – Algirdas Preidžius Apr 02 '18 at 10:41
  • 1
    @AlgirdasPreidžius If I understand OP correctly, that's exactly the question: Where to declare `c`. – Max Vollmer Apr 02 '18 at 10:43
  • Sorry, c is a myclass object. With scope on the file I create a dummy myclass object to hold the result of the operation: myclass c(0,0). The question is how to store c in a way such another call on the function doesn't pollute the resoult of the operation. – Podarce Apr 02 '18 at 10:43
  • 1
    Where's the issue with just declaring it as local variable (to the sum function)? – Daniel Jour Apr 02 '18 at 10:44
  • 1
    First of all, don't pass pointers. If you don't want to copy object use (`const`) references. Secondly, unless you explicitly share objects between threads, then I don't really see a problem here. A local non-static variable will not be shared between threads. – Some programmer dude Apr 02 '18 at 10:44
  • @Podarce Just declare `c` inside that method. – Max Vollmer Apr 02 '18 at 10:45
  • Mentioning multithread cause, using my first idea, multiple threads could have access to that global c object. Without the multithread environment, no other entity could pollute this "support" object. Am I wrong ? – Podarce Apr 02 '18 at 10:50
  • ok, updated answer – YSC Apr 02 '18 at 10:51

1 Answers1

4

You use const reference for your inputs, temporary for your output:

// assume longitude and latitude public for sake of simplicity here
myclass sum(myclass const& a, myclass const& b)
{
    myclass result;
    result.longitude = a.longitude + b.longitude;
    result.latitude  = a.latitude  + b.latitude;
    return result;
}

int anotherfunc(myclass const& a, myclass const& b)
{
    return a.longitude - sum(a,b).longitude;
}

Inside sum a temporary myclass object is constructed, set, and returned by the function. This temporary object can be used almost as a standard object in an expression, for instance sum(a,b).longitude will be, as expected, the sum of a and b's longitudes.

Since result is not a global as you feared it would be, sum can be used in a multithreaded environment, as long as different threads work on different a and b at the same time.

YSC
  • 38,212
  • 9
  • 96
  • 149
  • Thank you. Could I ask why is a bad practice to pass pointers to objects ? – Podarce Apr 02 '18 at 10:53
  • 1
    @Podarce of course you can. Here is an excellent Q&A about pointers versus references: https://stackoverflow.com/q/7058339/5470596 – YSC Apr 02 '18 at 10:56