User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Aug 2004
Posts: 2
Reputation: mmmmar is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mmmmar mmmmar is offline Offline
Newbie Poster

Solution need help in creating class string

  #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:
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2004
Location: Hanover
Posts: 152
Reputation: cosi is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: need help in creating class string

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

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);
}
Reply With Quote  
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation: Chainsaw is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: need help in creating class string

  #3  
Aug 16th, 2004
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*
}
Reply With Quote  
Join Date: Aug 2004
Posts: 2
Reputation: mmmmar is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mmmmar mmmmar is offline Offline
Newbie Poster

Re: need help in creating class string

  #4  
Aug 17th, 2004
wow! thanks!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:35 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC