Re: [Dev-C++] How would you write a method returning a string ?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Adam J. <aj...@i-...> - 2007-07-23 02:22:24
|
>> The first question to ask is: why would one need that? C++ strings
>> were made thinking of them as a base value, like int.
>>
>> Id rather write a function with
>>
>> const std::string somefunction(const char *val)
>>
>> prototype.
I agree with this prototype function. This is much safer.
>> I have many classes that manipulate the string and then returns it either
as
>> a 'const char*', 'std::string', 'int', and so on and so forth, the method
is
>> just part of the class, so instead of writing a stand alone function, it
is
>> better to pack it up within the class, after all... that is the nature of
>> Object Oriented Programming.
First of all you don't need a standalone function to get a const char * from
a std::string. Try std::string.c_str() it returns a const char*. Second,
that is not what Object Oriented Programming is all about. Object Oriented
programming is about Inheritance, Data hiding, and polymorphism. Being able
to create a base class and inherit from that class to create other objects,
and to be able to hide the implementation and data, along with using
polymorphism. Is what OOP is all about. If you have created a class that
isn't being use for inheritance, data hiding, and polymorphism, then all you
have done is created a class with a bunch of functions in them. If that is
the case, then that is not OOP.
-Adam Jones
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Jamiil
Abduqadir
Sent: Sunday, July 22, 2007 11:39 PM
To: matheus ribeiro
Cc: dev...@li...
Subject: Re: [Dev-C++] How would you write a method returning a string ?
I have many classes that manipulate the string and then returns it either as
a 'const char*', 'std::string', 'int', and so on and so forth, the method is
just part of the class, so instead of writing a stand alone function, it is
better to pack it up within the class, after all... that is the nature of
Object Oriented Programming.
----- Original Message -----
From: "matheus ribeiro" <mfr...@gm...>
Cc: <dev...@li...>
Sent: Sunday, July 22, 2007 5:25 PM
Subject: Re: [Dev-C++] How would you write a method returning a string ?
> The first question to ask is: why would one need that? C++ strings
> were made thinking of them as a base value, like int.
>
> Id rather write a function with
>
> const std::string somefunction(const char *val)
>
> prototype.
>
> 2007/7/22, Jamiil Abduqadir <jal...@gm...>:
>> And how would you suggest one should write a method that receives a
>> 'const
>> char*' and returns a 'std::string&'?
>>
>>
>>
>> ----- Original Message -----
>> From: "matheus ribeiro" <mfr...@gm...>
>> To: <dev...@li...>
>> Sent: Sunday, July 22, 2007 8:45 AM
>> Subject: Re: [Dev-C++] How would you write a method returning a string ?
>>
>>
>> >> std::string& NameSpace::ClassName::MethodName(const char* var){return
>> >> someString = var;}
>> >
>> >
>> > Not a good idea if somestring is a function variable. It will go out
>> > of scope and be destroyed, returning a ref to it will yield undefined
>> > results.
>> >
>> >
-------------------------------------------------------------------------
>> > This SF.net email is sponsored by: Splunk Inc.
>> > Still grepping through log files to find problems? Stop.
>> > Now Search log events and configuration files using AJAX and a browser.
>> > Download your FREE copy of Splunk now >> http://get.splunk.com/
>> > _______________________________________________
>> > Dev-cpp-users mailing list
>> > Dev...@li...
>> > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
>> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>>
>>
>> -------------------------------------------------------------------------
>> This SF.net email is sponsored by: Splunk Inc.
>> Still grepping through log files to find problems? Stop.
>> Now Search log events and configuration files using AJAX and a browser.
>> Download your FREE copy of Splunk now >> http://get.splunk.com/
>> _______________________________________________
>> Dev-cpp-users mailing list
>> Dev...@li...
>> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
>> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems? Stop.
> Now Search log events and configuration files using AJAX and a browser.
> Download your FREE copy of Splunk now >> http://get.splunk.com/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|