473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reference base class data from derived class?

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
4 2204
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

"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in...
7
by: Santi | last post by:
I have two classes: Product and Fruit which inherits from Product. If I try to narrow the reference to the base type by a cast, I always get a reference to the inherited type. For example: ...
1
by: Mercede | last post by:
Hi, I've a certain problem. I've created a Class Library project that contains the following - A Factory Class - A base Object A factory can create the Base objects or objects derived from...
9
by: Dennis | last post by:
When a class (myownclass) inheirits another class, how can I get an object reference to the underlyng MyBase class instance from within myownclass. The base class has a method that I want to...
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
5
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
3
by: josh | last post by:
Hi I noticed that when I make a function with a base class parameter that is a reference than at run-time the compiler calls a base class function also if I have passed a derived object. But if...
12
by: siddhu | last post by:
Dear experts, #include <stdio.h> class Base { }; class Der1:public Base {
4
by: Javier | last post by:
Hello, is this possible? I have a pure virtual function in the base class (to force the programmers of the derived classes to have this function implemented) but I want to call the derived class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.