943,686 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4673
  • C++ RSS
Aug 14th, 2004
0

need help in creating class string

Expand Post »
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:
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mmmmar is offline Offline
2 posts
since Aug 2004
Aug 16th, 2004
0

Re: need help in creating class string

You seem to have two questions on your mind.

1) How do I have a member function length?
C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  1. String helloString("Hello Dave");
  2. cout << helloString.length();

But if you must know, this is how you pass a string into a function:
C++ Syntax (Toggle Plain Text)
  1. int length(char* string) {
  2. ...
  3. }
  4.  
  5.  
  6. void main() {
  7.  
  8. char string[26] = "Hi....";
  9. length(string);
  10. }
Reputation Points: 17
Solved Threads: 1
Junior Poster
cosi is offline Offline
153 posts
since Aug 2004
Aug 16th, 2004
0

Re: need help in creating class string

Sure you can pass a string as a reference! Return one too!
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Aug 17th, 2004
0

Re: need help in creating class string

wow! thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mmmmar is offline Offline
2 posts
since Aug 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Using Resources in Visual C++
Next Thread in C++ Forum Timeline: file extraction and insertion





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC