Reference base class data from derived class?

J

John Speth

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:

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").

Can anyone please tell me how to accomplish this properly with c++?

Thanks, John
 
Y

Yan

if the base class doesn't expose this data (meaning it's declared
private and there is no accessor/mutator methods provided) then I am
afraid there might not be a way to change this data from a derived
class. It could be though done on purpose (not sure if that's the case
with CString) to accomplish better performance.
 
H

Howard

John Speth said:
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:

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").

It all depends on what a CString is. Since we don't know what the
definition of that class or its functions are, we can't tell you how to
create and use a class derived from it (or if that's even possible).

If you're talking about the MFC class in Visual Studio, then you need to ask
in a Microsoft MFC newsgroup.

-Howard
 
D

Daniel T.

John Speth said:
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();
}
 
J

James Kanze

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

Normally, you can only do so by using the member functions of
the base class.
(see my failed attempt at using "*this").

The only reason this probably failed is that Left returns a
CString, and not a CStringTrim. You can either provide a member
operator=( CString const& ) in your own class, or cast this up
to a CString*. Or call the operator= function explicitly:

CString::eek:perator=( Left( n ) ) ;

Off hand, however, I rather suspect that CString was not
designed to be used as a base class, and that deriving from it
is not very good design. Is there any reason why a free
function, trimComment, wouldn't do the trick. (That's certainly
the route I'd go with std::string.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top