need help in creating class string

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2004
Posts: 2
Reputation: mmmmar is an unknown quantity at this point 
Solved Threads: 0
mmmmar mmmmar is offline Offline
Newbie Poster

need help in creating class string

 
0
  #1
Aug 14th, 2004
hi guys!
i'm tryng to make a class string...
i'm wondering how to make a member function that can show the string length w/o using strlen..please help!

i already made a program that prints the length of the string w/o using strlen...but the problem is, it's in the main function...

i'm wondering how to pass by reference a string...is it possible?

please help :cry:
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 153
Reputation: cosi is an unknown quantity at this point 
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: need help in creating class string

 
0
  #2
Aug 16th, 2004
You seem to have two questions on your mind.

1) How do I have a member function length?
  1. class String {
  2.  
  3. char* fString;
  4. public:
  5. // constructors
  6. String();
  7. String(const char*);
  8.  
  9. int getLength();
  10. ....
  11. }
  12.  
  13. int String::getLength() {
  14. // put code in here for length...
  15. }


2) How do I pass a string by reference?
In the case where you do have a String class I wouldn't pass a string in at all. Ideally your string class should go like this:

  1. String helloString("Hello Dave");
  2. cout << helloString.length();

But if you must know, this is how you pass a string into a function:
  1. int length(char* string) {
  2. ...
  3. }
  4.  
  5.  
  6. void main() {
  7.  
  8. char string[26] = "Hi....";
  9. length(string);
  10. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: need help in creating class string

 
0
  #3
Aug 16th, 2004
Sure you can pass a string as a reference! Return one too!
  1. // Like anything else, a string can be passed by reference
  2. // Generally this allows setting the POINTER to the variable,
  3. // rather than filling in the variable as strcpy() would.
  4. // Think of this like passing char** theName
  5. void GetName( char* & theNameByRef, char** theNameByPtr )
  6. {
  7. theNameByRef = "Chainsaw"; // yes, theName now POINTS TO the literal
  8. *theNameByPtr = "Chainsaw"; // same effect, but the syntax is different
  9. }
  10.  
  11. char** TheNamePtr( char* n )
  12. {
  13. return &n; // a pointer to a pointer to chars
  14. }
  15. char*& TheName( char* n )
  16. {
  17. return n; // same effect, but simpler syntax. This returns a REFERENCE to the char*
  18. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 2
Reputation: mmmmar is an unknown quantity at this point 
Solved Threads: 0
mmmmar mmmmar is offline Offline
Newbie Poster

Re: need help in creating class string

 
0
  #4
Aug 17th, 2004
wow! thanks!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC