Thread: 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: Per W. <pw...@ia...> - 2007-07-23 13:40:41
|
If you have functions that returns their answers without making use of any
state of a specific class, then I would recommend that you put these
functions into a namespace instead of into a class.
Methods to a class normally perform a service based on the contents of an
object, or changes the state of an object. With no involvement of an
object, they are very strange methods.
/pwm
On Mon, 23 Jul 2007, Jamiil Abduqadir wrote:
> Maize
>
>
> Good day Matheus and Adam, I agree with what you tow have to say about a stand alone function, but I also recognize the validity of my statement.
>
> A class provides not only a way to hide data or polymorphism, this alone is useless unless the class or one of its derivatives provides a service. To bring my point home, let us say that class Abecedarium, provides the sound and the location of the letter in the Abecedarium, and you want to create a class that handles queries about the letters of the alphabet in the English language, well, it would be a good Idea to use Abacedarium as you base class, thus making your point valid, for in this case the hiding data and polymorphism ins an important aspect of OOP. However, this alone does not provide any service to the user, unless the creator of the either class provides a "bunch" of methods to take information in or out of the class to be processed or that has been processed respectively, thus the API of the class. And this brings me to the point of adding the method I suggested to a class. For instance
>
> const std::string& MyClass::Char2Str(const char* x){ return str = x;}
> As Adam said, this is a waist of time 'since std::string::c_str' handles this very nicely, but what if the case is not to convert the letters from char to string alone, but to check the validity of the character-string, for instance that it does not have numbers or punctuation marks in it, inform the client of such a mistake, and so on and so forth, if the character-string is valid, then to remove any white spaces at the beginning or end of the character-string, then and only then the character-string passed as a char can be converted to a 'std::string'. If Abecedarium did not have a method(s) that would handle this operations then you will have to add it to your derived class. Which is my position on this subject.
>
> I would strongly recommend to add what ever function you find necessary to a particular class, MOSTLY for beginners, later on, as the beginner grows more familiar with the paradigm of OOP he or she will find that there are many classes that have the same method, which will make the new programmer create a class that will be the parent class of all those classes that have the same method, thus making his or her skills escalate from beginners to advanced. Later on, as an advance programmer, s/he will learn design and development making him or her a sort of an expert. C++ gurus are class of its own, so we don't talk about them
>
> I know that the are many loopholes in my brief explanation, however, time permited I have done all I can to help the C++ community. I want you to know that it has never been my intention to over shadow your opinion, which is just a valid as mine, but to only help to the best of my abilities.
>
>
> Have a good one.
|
|
From: Adam J. <aj...@i-...> - 2007-07-24 02:12:31
|
>>If you have functions that returns their answers without making use of any
state of a specific class, then I would recommend that you put these
functions into a namespace instead >>of into a class.
I agree whole hearted with this.
>>Methods to a class normally perform a service based on the contents of an
object, or changes the state of an object. With no involvement of an object,
they are very strange >>methods.
I agree again.
Jamiil please note this, you will be known by the source code you produce.
C++ allows you to do things you should not do, but because it allows you to
do it, doesn't mean you should. Others have suggested other ways to
accomplish what you need to do. It also appears that all of the advice you
have been given is to avoid what you are trying to do. But having said that,
it is your code. You have to make the ultimate decision. If you notice, no
one here has agreed with the way you are writing the code. Most people are
here to share their experience with others. Having no one agree with your
implementation should cause you to rethink how you are doing it....IMHO
-Adam Jones
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Per
Westermark
Sent: Monday, July 23, 2007 9:40 AM
To: Jamiil Abduqadir
Cc: dev...@li...
Subject: Re: [Dev-C++] How would you write a method returning a string ?
If you have functions that returns their answers without making use of any
state of a specific class, then I would recommend that you put these
functions into a namespace instead of into a class.
Methods to a class normally perform a service based on the contents of an
object, or changes the state of an object. With no involvement of an
object, they are very strange methods.
/pwm
On Mon, 23 Jul 2007, Jamiil Abduqadir wrote:
> Maize
>
>
> Good day Matheus and Adam, I agree with what you tow have to say about a
stand alone function, but I also recognize the validity of my statement.
>
> A class provides not only a way to hide data or polymorphism, this alone
is useless unless the class or one of its derivatives provides a service. To
bring my point home, let us say that class Abecedarium, provides the sound
and the location of the letter in the Abecedarium, and you want to create a
class that handles queries about the letters of the alphabet in the English
language, well, it would be a good Idea to use Abacedarium as you base
class, thus making your point valid, for in this case the hiding data and
polymorphism ins an important aspect of OOP. However, this alone does not
provide any service to the user, unless the creator of the either class
provides a "bunch" of methods to take information in or out of the class to
be processed or that has been processed respectively, thus the API of the
class. And this brings me to the point of adding the method I suggested to a
class. For instance
>
> const std::string& MyClass::Char2Str(const char* x){ return str = x;}
> As Adam said, this is a waist of time 'since std::string::c_str' handles
this very nicely, but what if the case is not to convert the letters from
char to string alone, but to check the validity of the character-string, for
instance that it does not have numbers or punctuation marks in it, inform
the client of such a mistake, and so on and so forth, if the
character-string is valid, then to remove any white spaces at the beginning
or end of the character-string, then and only then the character-string
passed as a char can be converted to a 'std::string'. If Abecedarium did not
have a method(s) that would handle this operations then you will have to add
it to your derived class. Which is my position on this subject.
>
> I would strongly recommend to add what ever function you find necessary to
a particular class, MOSTLY for beginners, later on, as the beginner grows
more familiar with the paradigm of OOP he or she will find that there are
many classes that have the same method, which will make the new programmer
create a class that will be the parent class of all those classes that have
the same method, thus making his or her skills escalate from beginners to
advanced. Later on, as an advance programmer, s/he will learn design and
development making him or her a sort of an expert. C++ gurus are class of
its own, so we don't talk about them
>
> I know that the are many loopholes in my brief explanation, however, time
permited I have done all I can to help the C++ community. I want you to know
that it has never been my intention to over shadow your opinion, which is
just a valid as mine, but to only help to the best of my abilities.
>
>
> Have a good one.
-------------------------------------------------------------------------
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
|