Jump to content
  • Advertisement
Sign in to follow this  

Calling base class functions and temp results

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

I actually didn't know how to call that thread... Take a look at that code:
class Base
{
public:
	int a, b, c;

public:
	friend istream & operator >> (istream &_istream, Base &base);
};

istream & operator >> (istream &_istream, Base &base)
{
	_istream >> base.a;
	_istream >> base.b;
	_istream >> base.c;

	return _istream;
}



class Derivative: public Base
{
public:
	int d;

public:
	friend istream & operator >> (istream &_istream, Derivative &derivative);
};

istream & operator >> (istream &_istream, Derivative &derivative)
{
	_istream >> (Base)derivative; // something's wrong
	_istream >> derivative.d;

	return _istream;
}
I just want to make a "shortcut" and assign a, b and c with the function called in line of interest (the one with comment "some's wrong"). However, it just doesn't work. I could do something like:
istream & operator >> (istream &_istream, Derivative &derivative)
{
	_istream >> derivative.a;
	_istream >> derivative.b;
	_istream >> derivative.c;
	_istream >> derivative.d;

	return _istream;
}
But this is only a code duplication which I want to avoid.

Share this post


Link to post
Share on other sites
Advertisement
Yes, it worked of course :)
I suppose it's that I need to cast on the *reference* (Base&), and casting only on (Base) gives *value*..? Could someone explain it in more details, please?

Share this post


Link to post
Share on other sites
(Base) derivative copy constructs a temporary Base object from the derivative object. A temporary can't be bound to a non-const reference.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!