From 6914871124411142675
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,ae9b53b574601927,start
X-Google-Attributes: gidf78e5,public
From: Fabio Ortiz <jfabio@mailhost.netrunner.net>
Subject: friend operator and the this pointer
Date: 1996/10/18
Message-ID: <3265ACC9.590D@mailhost.netrunner.net>#1/1
X-Deja-AN: 190343276
x-nntp-posting-host: stingray-28.netrunner.net
content-length: 1387
content-type: text/plain; charset=us-ascii; name="friend.txt"
organization: NetRunner
mime-version: 1.0
reply-to: jfabio@mailhost.netrunner.net
newsgroups: comp.std.c++
content-disposition: inline; filename="friend.txt"
originator: clamage@taumet
x-mailer: Mozilla 3.0Gold (Win95; I)


Hi, I have the following:

class A
{
public:
	A( char* name );
	friend ostream& operator<<(ostream& o, A& a)
		{
			a.display( &o);
			return o;
		}
protected:
	char* myName;
private:
	virtual void display( ostream& o)
	{
		o << myName;
	}
}

class B: public A
{
public:
	B( int value, char* name );
	ostream& operator<<(ostream& o, B& b)
	{
		b.display( o );
		return o;
	}
protected:
	int myValue;
private:
	virtual void display( ostream& o)
	{
		o << (A*) this;
		o << myValue;
	}

class C: public B
{
public:
	C( long number, int value, char* name);
	ostream& operator<<(ostream& o, C& c)
	{
		o << c.display( ostream& o);
		return o;
	}
protected:
	long parameter;
private:
	virtual void display( ostream& o)
	{
		o << (B*) this;
		o << parameter;
	}
}

main()
{
C myC( 20, 10, "aname');
cout << C;
}

this code only displays the parameter value. It do not call the other
operators << for some reason. Is There any thing wrong with the cast.

Friend functions are not inherited does this mean that I can not call
them through the this pointer using the cast?.

I am setting some of the fields using cout.setf( io::hex ) after I use
the cast I do not think this has any effect in the problem.

Does it make sense to make a private method virtual, it can not be
called from the derived functions



[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]



