Sign in to follow this  
maxest

Calling base class functions and temp results

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
(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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this