Jump to content
  • Advertisement
Sign in to follow this  
Alpha_ProgDes

Constructor initializers

This topic is 4842 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

#include "time.h"
#include "interval.h"

enum CallType{LOCAL, LONG_DISTANCE};

class PhoneCall
  {
public:
  friend istream& operator>>(istream& in, PhoneCall& phoneCall);
  friend ostream& operator<<(ostream& out, const PhoneCall& phoneCall);
  CallType getCallType() const;
  int callLength() const;
private:
  CallType callType;
  Interval<Time> interval;
  };  
This is my base class. Now I'll admit now this is partially homework, but I need clarification on something. My derived class is this:
#include "phonecall.h"
#include "money.h"

class CostedCall : public PhoneCall
  {
public:
  CostedCall();
  CostedCall(PhoneCall phoneCall, Money cost);
  friend ostream& operator<<(ostream& out, const CostedCall& phoneCall);
private:
  Money cost;
  };
Now I'm not sure how to pass PhoneCall phoneCall to the constructor. Should I do:
CostedCall::CostedCall (PhoneCall phoneCall, Money cost) 
    :PhoneCall(phoneCall), cost(cost) {
    //nothing here
}
Or use the istream overloaded operator I have in the Base Class? Some guidance would be appreciated [smile]

Share this post


Link to post
Share on other sites
Advertisement
Your use of the base class (copy) constructor in the derived class initializer list is appropriate, though I'd take the parameters by const reference if I were you: CostedCall(const PhoneCall& phoneCall, const Money& cost);

Share this post


Link to post
Share on other sites
I suppose you could try this:-

CostedCall::CostedCall (const PhoneCall& phoneCall, Money cost)
:cost(cost) {
*this = phoneCall;
}

Although you would need to override the = operator. Personally though, I think passing a PhoneCall in CostedCall is a bad idea anyway. This is because if you derive some other class from PhoneCall and you create an instance of it, you can convert that instance back up to a PhoneCall then pass it into the CostedCall constructor. And that would be bad when doing anyting with 'this' after *this = phoneCall!

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!