•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,556 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,441 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2999 | Replies: 3
![]() |
•
•
Join Date: Aug 2004
Posts: 2
Reputation:
Rep Power: 0
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?
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:
String helloString("Hello Dave");
cout << helloString.length();But if you must know, this is how you pass a string into a function:
int length(char* string) {
...
}
void main() {
char string[26] = "Hi....";
length(string);
}•
•
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation:
Rep Power: 5
Solved Threads: 10
Sure you can pass a string as a reference! Return one too!
// 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*
}![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- String class help (Java)
- Help on Creating Class in VB6 (Visual Basic 4 / 5 / 6)
- error in user defined string class (C++)
- new and in need (ASP)
Other Threads in the C++ Forum
- Previous Thread: Using Resources in Visual C++
- Next Thread: file extraction and insertion


Linear Mode