![]() |
| | #16 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
So, you have an example? I'd love to see you thwart the happy little compiler.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| ||
| Elysia is offline | |
| | #17 | |||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
Quote:
Quote:
__________________ 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 | ||
| Registered User Join Date: Jan 2008
Posts: 575
| Quote:
Thwart the compiler?! Are you insane?! I'm suggesting you use the compiler. That's why it exists. What don't you get? You can literally use any method of static assertion or compiler firewall you desire. As I've implied: I use a meta-function to do this for any or all of the operators applied to any number of types--actually limited to 64. The massive overhead would be pointless, but I'll offer a trivial and sloppy way to block an operator for a type you don't own without otherwise affecting the compiler output, messages or errors, from "correct" source. Enjoy. Quote:
Soma Code: class blocker{public: blocker(){} private:blocker(const blocker &){} friend blocker operator - (const my_string &, const my_string &);};
blocker operator -
(
const my_string & lhs,
const my_string & rhs
)
{
blocker return_value;
return(return_value);
}
Last edited by phantomotap; 10-05-2008 at 02:19 AM. | ||
| phantomotap is offline | |
| | #19 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
That, essentially means thwart the compiler in my book
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| ||
| Elysia is offline | |
| | #20 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
__________________ 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 | |
| | #21 | ||
| Registered User Join Date: Jan 2008
Posts: 575
| Quote:
Yea... that would be silly, but I'm not blocking the conversion. I'm telling the compiler that certain constructs are invalid... I'm basically marking conversions in certain contexts as private. Quote:
Soma Last edited by phantomotap; 10-05-2008 at 02:49 AM. | ||
| phantomotap is offline | |
| | #22 | |||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
Quote:
Quote:
__________________ 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; 10-05-2008 at 02:57 AM. | |||
| laserlight is offline | |
| | #23 | |
| Registered User Join Date: Jan 2008
Posts: 575
| Quote:
I think I've spotted a flaw in your understanding. In a perfect world the responsibility, or burden if you like, of disabling conversions for specific constructs belongs to the provider of a type. (You wouldn't, or shouldn't, in using my library, or any other, go into the headers to mark some method private even if it should have been marked private.) Even in our world of shambles, the responsibility belongs to a single type. (Granted, in this case I've offered that it can "fix" an existing provision that didn't account for unwanted implicit conversion by displacing this burden, but that's not really the point.) The relevant source, even without templates, need only be written to disallow problematic conversions for each implicitly converted type for any given class type with such implicit conversions. The 'blocking' type is a "no-op"; it is no more associated with the relevant type nor any more responsible than 'std::string' would be if someone provides 'std::string operator + (const std::vector<?> &, unsigned long)'. Soma PS. And not that it matters, but I'm a big fan of consistency. I teach people to use 'static_cast<?>' even with classes offering implicit conversion. (With an exception of 'operator void *' and 'operator bool' because stream using source seems "right" without it.) Last edited by phantomotap; 10-05-2008 at 04:24 AM. | |
| phantomotap is offline | |
| | #24 | |||
| The larch Join Date: May 2006
Posts: 3,082
| It is interesting that you start by saying Quote:
Quote:
Code: ofsteam fout(static_cast<const char*>(filename)); //over ofstream fout(filename.c_str()); Code: Character c; //Rect r = c.bounding_rect(); Rect r = static_cast<Rect>(c);
__________________ I might be wrong. Quote:
| |||
| anon is offline | |
| | #25 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
What I meant is that since a named conversion function forces the user of the type to explicitly request the conversion, any abuse of this conversion is the user's fault. With an implicit conversion function, any abuse of this conversion is both the user's fault and the type designer's (or implementor's) fault. The former is at fault for a mistake in using the type, and the latter is at fault for failing to disable the constructs that are not sensible.
__________________ 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 | |
| | #26 |
| Registered User Join Date: Oct 2008 Location: Leading lawn gnomes on the path to world domination! ;o)
Posts: 13
| Thank you for explaining Code: operator char*() {return ptr;}
![]() I guess the point of this 'don't use automatic conversions'? I understood only the a = b - c issue, I don't know how widgets work, so I didn't understand it. I've never touched any programming language before, so my total programming experience extends to about 5 months. ^^; I'm reading "C++ without fear", if that indicates how new I am to all of this. So I don't really know how to make an explicit conversion function. I saw this in the "books" sections of cprogramming, so I'm reading those. I looked into other books, but I know so little I didn't know what to go with. So this is the first book I ended up getting. I got the information I came for though, So thanks everyone! (And when I figure out how to write a function to take the place of operator char*(), I'll be sure to use it.) |
| Lawn Gnomusrex is offline | |
| | #27 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| The idea is that you create a function such as: const char* get(); In your class instead of the implicit conversion operator: operator const char* (); This will eliminate the chance for these headaches.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #28 |
| Registered User Join Date: Oct 2008 Location: Leading lawn gnomes on the path to world domination! ;o)
Posts: 13
| Ahhh! Thank you, Elysia! I didn't know if making something like this required more work or not.I appreciate the help, everyone! Thanks! |
| Lawn Gnomusrex is offline | |
| | #29 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,439
| Also, non-const char* is bad for encapsulation. Basically, by returning a non-const char*, you implicitly give clients of your class leave to do with your internal string data whatever they want - not a good idea when the char* really belongs to the string object.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
![]() |
| Tags |
| class, classes, pointers, references, strings |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Can anybody show me why this works?? | tzuch | C Programming | 2 | 03-29-2008 09:03 AM |
| Understanding formulas, shapes, algorithms etc | hardi | General Discussions | 26 | 04-16-2007 01:23 PM |
| Works outside of the loop, not within | Decrypt | C++ Programming | 5 | 08-05-2006 12:22 AM |
| understanding recursive functions | houler | C Programming | 7 | 12-09-2004 12:56 PM |
| Understanding Headers and Inclusions | jdm | C++ Programming | 11 | 04-21-2004 10:11 PM |