| | |
need help in creating class string
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2004
Posts: 2
Reputation:
Solved Threads: 0
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:
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:
You seem to have two questions on your mind.
1) How do I have a member function length?
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:
But if you must know, this is how you pass a string into a function:
1) How do I have a member function length?
C++ Syntax (Toggle Plain Text)
class String { char* fString; public: // constructors String(); String(const char*); int getLength(); .... } int String::getLength() { // put code in here for length... }
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)
String helloString("Hello Dave"); cout << helloString.length();
But if you must know, this is how you pass a string into a function:
C++ Syntax (Toggle Plain Text)
int length(char* string) { ... } void main() { char string[26] = "Hi...."; length(string); }
Sure you can pass a string as a reference! Return one too!
C++ Syntax (Toggle Plain Text)
// Like anything else, a string can be passed by reference // Generally this allows setting the POINTER to the variable, // rather than filling in the variable as strcpy() would. // Think of this like passing char** theName void GetName( char* & theNameByRef, char** theNameByPtr ) { theNameByRef = "Chainsaw"; // yes, theName now POINTS TO the literal *theNameByPtr = "Chainsaw"; // same effect, but the syntax is different } char** TheNamePtr( char* n ) { return &n; // a pointer to a pointer to chars } char*& TheName( char* n ) { return n; // same effect, but simpler syntax. This returns a REFERENCE to the char* }
![]() |
Similar Threads
- Help on Creating Class in VB6 (Visual Basic 4 / 5 / 6)
- Creating a Class Library (VB.NET)
- Help creating a class??? (Java)
- Database Connectivity by Creating a Class (C#)
- Creating a ref to a string ? (Python)
Other Threads in the C++ Forum
- Previous Thread: Using Resources in Visual C++
- Next Thread: file extraction and insertion
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





