3

Reviewing some of the JUCE sample code, I ran across this function call:

Arpeggiator()
{
    addParameter (speed = new AudioParameterFloat ("speed", "Arpeggiator Speed", 0.0, 1.0, 0.5));
}

Is this just an assignment within a function call? Or is there some other kind of c++ magic going on here?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
gmcerveny
  • 191
  • 1
  • 17
  • 6
    I cannot tell because `=` operator may be overloaded and there are no declaration/definition of `speed` and its type. – MikeCAT Jun 02 '16 at 16:51
  • Typically this is used to both assign the value of `speed` and pass this value on to `addParameter()` on the same line. – Havenard Jun 02 '16 at 16:54
  • also addParameter might be a macro, not a function, maybe – johannes Jun 02 '16 at 16:54
  • @MikeCAT The `operator=()` can't be overloaded for a `AudioParameterFloat *`. Well, `speed` could be something else, but according conventions the overloaded `operator=()` should return a reference to that instance. – πάντα ῥεῖ Jun 02 '16 at 16:59

1 Answers1

6

Is this just an assignment within a function call?

Yes.

Or is there some other kind of c++ magic going on here?

Not much magic there, the return value of an assignment operation usually is a reference to the instance assigned.

So it's equivalent to (I suspect that is constructor code):

class Arpeggiator : public Decorator {
public:
     Arpeggiator() {
         speed = new AudioParameterFloat ("speed", "Arpeggiator Speed", 0.0, 1.0, 0.5);
         addParameter (speed); // Probably inherited from Decorator 
     }
private: 
    AudioParameterFloat* speed;
};

As the question came up, what happens if there's an overloaded assignment operator.

As stated in this answer in the canonical Operator overloading:

Assignment Operator

There's a lot to be said about assignment. However, most of it has already been said in GMan's famous Copy-And-Swap FAQ, so I'll skip most of it here, only listing the perfect assignment operator for reference:

X& X::operator=(X rhs)
{
   swap(rhs);
   return *this;
}
Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190