![]() |
| | #16 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 16,078
| Why is it so difficult to learn C++ and not VB or Java (unsure about this one) or such languages, I wonder? Because everything is neatly organized into one place. If a programmer has to rely on algorithms to get things done (and I mean simple tasks), or go browse through a namespace that's not named in the docs, things DO get harder. Difficulty is what I try to avoid. No wonder C++ is difficult to learn. And some don't seem to understand my point either... Classes, like functions, can be extended. It's your job to make sure they can be. They should be. Otherwise you did something wrong. One class should be one thing. Don't add everything into it. But a string class should have certain things, such as ToLower/ToUpper, which the standard library seem to lack. Classes can grow just as free functions can grow. Now, the problem is to find a way to make it possible for the classes to grow similar to free functions. I don't think I'd use static members, though. They would appear inside the class namespace. I'd rather make them free and move them into the Strings namespace... Neatly organized. Perhaps another question is: how do I break out the implementation from the definition of a specialized template? Instead of: Code: template<> class StrConversion<wchar_t>
{
public:
CStringExA ToANSI() { /*... */ }
private:
virtual const wchar_t* GetData() = 0;
};
Code: template<> class StrConversion<wchar_t>
{
public:
CStringExA ToANSI();
private:
virtual const wchar_t* GetData() = 0;
};
template<> CStringExA strConversion<wchar_t>::ToANSI() { /* ... */ }
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x "Thanks Elysia. You're a programming master! How the hell do you know every thing?" "Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff." Quoted... at least once. Quote:
Last edited by Elysia; 05-11-2008 at 10:54 AM. | |
| Elysia is offline | |
| | #17 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 12,459
| Quote:
Quote:
So, what's wrong with placing your free functions in an appropriate namespace? After all, they will be neatly organised, and you cited organisation as a reason for wanting to use classes instead of free functions.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | ||
| laserlight is offline | |
| | #18 |
| Staff software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 6,014
| By making ToANSI() a member function you are making the artificial statement that only a String has the business of converting characters from one character set to another. But what if you only had a single character, not a whole string, and you wanted to convert just that single character? Is any of this getting through? |
| brewbuck is offline | |
| | #19 | ||||
| Mysterious C++ User Join Date: Oct 2007
Posts: 16,078
| Quote:
I mean, have you had a look at MSDN? Sure, it documents std::string, but nothing of the free functions or algorithms you want to use to use it better. Easy to use, then, if you haven't been taught properly? Hardly. Quote:
Otherwise you will put half in a class and half in a namespace, which makes it disorganized. Or if you put everything in the namespace, we're back to plain C. Quote:
I don't think so. It is necessary to provide options for manually processing the string. That means, that if you don't like what the class can offer, you can extract the data and process it yourself. This is evident by the GetBuffer function where you can get the data, run it through an algorithm and put it back. I go by the design to not limit choices mostly. The class can be a convenience. But it shouldn't limit functionality because of it. If you need power, or more flexibility or want to do something with it, then you must be allowed to do so. But perhaps a member function that converts a range isn't that bad either. It's convenience. The class does in no refuse to loan its data. But you can ask for permission to do so by asking nicely.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x "Thanks Elysia. You're a programming master! How the hell do you know every thing?" "Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff." Quoted... at least once. Quote:
| ||||
| Elysia is offline | |
| | #20 | |
| Staff software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 6,014
| Quote:
| |
| brewbuck is offline | |
| | #21 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 16,078
| And how exactly does it help if ToANSI was free? It would still convert the whole string.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x "Thanks Elysia. You're a programming master! How the hell do you know every thing?" "Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff." Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #22 |
| Staff software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 6,014
| |
| brewbuck is offline | |
| | #23 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 16,078
| Ah, so you're basically after a sort of general function that convert an infinite number of string types to ansi. A free function could be a boon for you, and for those only looking in the class, the member function could also be a boon, so they both have their purposes. Sure, I could make a free function, too, but it is general and have nothing to do with the actual class (since I'm guessing a general function must abstract away the type passed as an argument). How many hundreds of those free functions that does the same would then exist? Let's see the member as convenience and the free one as a better solution, so as to get rid of the need to make a new member function for every class.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x "Thanks Elysia. You're a programming master! How the hell do you know every thing?" "Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff." Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #24 |
| Afraid of widths Join Date: Apr 2008 Location: Chicago
Posts: 889
| C++ is hard to learn because of the legacy C style and paradigms that professors and authors seemingly can't disabuse themselves of, and manual memory management. The overwhelming majority of newbie problems on this and other forums have to do with raw pointers and arrays. |
| medievalelks is offline | |
| | #25 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 16,078
| And if newbies could have have found appropriate functions and solutions more easily...? I dare say that if std::string was more OOP, it would have been far greater to use for me. It requires a lot of education to use it properly the way it is.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x "Thanks Elysia. You're a programming master! How the hell do you know every thing?" "Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff." Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #26 | |
| Staff software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 6,014
| Quote:
Imagine: Code: template <typename CharacterSet>
class String
{
...
String<ANSI> ToANSI()
{
...
}
// Or more generically
template <typename NewCharSet>
String<NewCharSet> ConvertCharacterSet()
{
...
}
}
| |
| brewbuck is offline | |
| | #27 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 16,078
| Quote:
Again, you're relying on tedious algorithms and a lot, think, bloated code to reduce the amount of code that goes into classes and/or functions. I suppose we have your attitude to thank for that made the standard library into what it was today. A disorganized, hard-to-use library that takes several lines to do something easy. You seem to sacrifice everything for less code. And your sample is not unlike what I figured you were after. A generic function to perform an action on an undefined amount of types, regardless of its interface. I do not doubt it is a good thing. It is. But it doesn't exactly make the code "beautiful" or whatever to call it. If I'd put wrappers to those functions for higher-level functions, then everything would become oh-so-much better. Your code approach reminds me of Win32 API and C. Complicated crap with 20 lines to do one thing when it could be done with maybe 1, 2 lines.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x "Thanks Elysia. You're a programming master! How the hell do you know every thing?" "Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff." Quoted... at least once. Quote:
Last edited by Elysia; 05-11-2008 at 11:34 AM. | ||
| Elysia is offline | |
| | #28 | |||
| Staff software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 6,014
| Quote:
Code: template <typename CharSet>
class String
{
template <typename NewCharSet>
String<NewCharSet> ConvertToCharSet()
{
String<NewCharSet> converted;
for_each_char(ch) // however you want to write this
{
converted.append(CharSetConvert<CharSet, NewCharSet>(ch))
}
}
};
Quote:
Quote:
Putting the complexity of the actual conversion in another layer isn't a "trick," it's a proper design. There is no reason why a string should care whatsoever about what its character set is, whereas only the character sets themselves have the knowledge to convert one to another. | |||
| brewbuck is offline | |
| | #29 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 16,078
| It's funny how you agree now. Functions that are generic and can do a lot of things are good. Very good. You agree. Yet for my classes, I'd rather write high-level wrappers and put them as members. The members are merely wrappers for the generic function. You agree wrappers are good. To further add, I have never been against algorithms or powerful functions such as a generic one as you show, but the problem comes when you must use those in your code instead of higher level wrappers for simple functionality. Should I need to do something that isn't/wasn't available, I'd write another high-level function for what I need to wraps an algorithm to do it. This goes back to the original problem... Is it possible to write a wrapper member function for specialized types of the string class? One for UTF16 to ANSI for the wchar_t specialization and one ANSI to UTF16 for the char version. Having a UTF16 member function for wchar_t wouldn't make any sense since I can't convert UTF16 to UTF16.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x "Thanks Elysia. You're a programming master! How the hell do you know every thing?" "Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff." Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #30 | ||||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 12,459
| Quote:
Quote:
Quote:
Quote:
EDIT: Oh, and if the application of a generic algorithm would be inefficient, only then would you provide an efficient implementation, possibly by providing a member function instead. An example of this would be std::sort versus std::list::sort.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way Last edited by laserlight; 05-11-2008 at 11:52 AM. | ||||
| laserlight is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| WS_POPUP, continuation of old problem | blurrymadness | Windows Programming | 1 | 04-20-2007 06:54 PM |
| Laptop Problem | Boomba | Tech Board | 1 | 03-07-2006 06:24 PM |
| implementation file | bejiz | C++ Programming | 5 | 11-28-2005 01:59 AM |
| Sorting problem.. well actually more of a string problem | fatdunky | C Programming | 5 | 11-07-2005 11:34 PM |
| Memory Problem - I think... | Unregistered | C Programming | 4 | 10-24-2001 12:14 PM |