Advertisement Jump to content
Sign in to follow this  
stylin

Method Chaining vs. Data Struct

This topic is 4901 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Say I have a class like this:
class Amp {
public:
	Amp(): volume_(1.0f), gain_(0.0f), reverb_(0.0f), channel_(1) {};
	Amp& Volume( float volume=1.0f ) { volume_=volume; return *this; };
	Amp& Gain( float gain=1.0f ) { gain_=gain; return *this; };
	Amp& Reverb( float reverb=1.0f ) { reverb_=reverb; return *this; };
	Amp& Distortion( unsigned int channel=2 ) { channel_=channel; return *this; };
private:
	float volume_;
	float gain_;
	float reverb_;
	unsigned int channel_;
};

And I use it like this:
int main() {
   Amp amp;
   Amp* pamp = new Amp;

   amp.Volume( 10.0f ).Gain().Reverb( 3.4f );
   (*pamp).Volume( 8.4f ).Reverb( 2.3f).Distortion();
   ..
   delete pamp;
   return 0;
}
I've got just a couple questions: .) Should I prefer this method over passing a struct filled with my initializations? .) How can I use this method at object-creation/initialization, if at all? Thanks in advance for your time.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Conner McCloud
(1) Making use of constructors are probably the better choice for initializing because it can be done in one go. No partially created amps floating around.

My abridged constructor constructs my derived classes fine. I was just wondering what disadvantages this has when the number of parameters grows large (7-10 items).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!