By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,514 Members | 1,843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,514 IT Pros & Developers. It's quick & easy.

Reference base class data from derived class?

P: n/a
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
May 24 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

May 24 '07 #2

P: n/a

"John Speth" <jo*******@yahoo.comwrote in message
news:f3**********@aioe.org...
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
May 24 '07 #3

P: n/a
"John Speth" <jo*******@yahoo.comwrote:
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();
}
May 25 '07 #4

P: n/a
On May 24, 8:01 pm, "John Speth" <johnsp...@yahoo.comwrote:
I have what is a simple understanding problem with c++. I just can't seea
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::operator=( 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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 25 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.