Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sample string class question?

Posted on 1999-07-27
4
Medium Priority
?
186 Views
Last Modified: 2010-04-02
Just playing with C++ and made my own class string.

My question is this.  I create my string like so:

string sValue("Test");

and it works fine.  The question I have thought is this:
I have a function
BOOL getText( LPSTR sValue )
{
  sValue = "Testing";
  return TRUE;
}

I would like to do something like this:
string sValue;
if( getText( sValue.size(50) ) == TRUE )
{
  ...
  ...
}
where size is a member of class string
LPSTR size( int nSize );

I know this is like CString in MFC, but I would like to know how this works. As you know my code doesnt work b/c Im not sure what how to write the size() portion of the code.

Any ideas.
When I compile it I get an error
not sure what the inside of size() function should look like.

Any advice.
Thanks
0
Comment
Question by:eric07
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1201210
Since your string is Null terminated, you can use C's strlen()
int String::Size()
{ return strlen(m_TheString); }
0
 
LVL 4

Accepted Solution

by:
nil_dib earned 200 total points
ID: 1201211
I assume your size function should allocate memory for the string ..!

LPSTR size( int nSize )
{
    return new char[nSize];
}

but then getText should COPY the memory.
BOOL getText( LPSTR sValue )
{
  strcpy( sValue, "Testing" );
  return TRUE;
}


0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1201212
In that sense. Well spotted nil_dib!
0
 
LVL 4

Expert Comment

by:nil_dib
ID: 1201213
hope so ;)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question