Toon posts:

[C++ VS6] Merkwaardige ambiguous operator

Pagina: 1
Acties:

Verwijderd

Topicstarter
OK na lang zoeken dan toch mn eerste thread op GoT

Het probleem is als volgt : Ik wil van een eigen string class het eerste karakter opvragen. Als ik dat door de compiler heen trek (Visual C++ 6 SP5) dan blijf ik de volgende melding krijgen :
h:\xlib\sources\xbase\xhugeint.cpp(30) : error C2666: '[]' : 3 overloads have similar conversions
Het stukje code waar dit in gebeurt is

C++:
1
2
3
4
XHugeInt::XHugeInt ( const XString &xstr )
{
  // Check if the number in the string is negative or positive
  if ( xstr[0] == '-' )


Dit lijkt te komen door de volgende declaraties in de XString class

C++:
1
2
3
4
5
6
7
8
9
10
11
12
  ///////////////////////////////////////////////////////////////////////////
  // Casts the XString object to a character pointer.
  ///////////////////////////////////////////////////////////////////////////
  virtual operator const char * const ( ) const;
  virtual operator const wchar_t * const ( ) const;

  ///////////////////////////////////////////////////////////////////////////
  // Returns the character at position 'pos' in the string, which is a zero
  // based index. If 'pos' is greater then the length of the string, it 
  // returns 0.
  ///////////////////////////////////////////////////////////////////////////
  virtual char operator[] ( unsigned long pos ) const;


Het lijkt wel of de compiler serieus overweegt xstr eerst naar een const char * const of een const wchar_t * const te kasten en 'm daarna pas te dereferencen i.p.v. gewoon de member functie van XString te gebruiken. Als ik de casting operators eruit gooi vind de compiler het namelijk ineens wel goed.
Alleen... ik WIL die casting operators helemaal niet kwijt.

Aangezien de MSDN niet echt behulpzaam was bij het oplossen van dit probleem heb ik eens gekeken in de source van CString. Daar zitten namelijk ook casting operators en een [] operator

C++:
1
2
3
4
// return single character at zero-based index
TCHAR operator[](int nIndex) const;
// return pointer to const string
operator LPCTSTR() const;


fyi een TCHAR is zonder #define UNICODE gewoon een char en een LPCTSTR is gewoon een const TCHAR *

Ik dacht : als ik de tweede const weghaal uit de casting operators (dus naar const char * ( ) const en const wchar_t * ( ) const ) dan werkt het mischien wel, maar nee dus...

Iemand enig idee hoe ik dit oplos? Ik dacht altijd dat resolution naar class members voorrang had boven implicit casts maar dat gaat hier dus niet op?

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 31-08 15:26

.oisyn

Moderator Devschuur®

Demotivational Speaker

hmm weird, het zou imho gewoon goed moeten gaan...
post je hele class definition van XString eens

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Verwijderd

Topicstarter
oisyin : you asked for it :P

C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#ifndef XSTRING_INC
#define XSTRING_INC

#include "xserializable.h"

class LD_XBASE XString : virtual public XSerializable
{
public:
  /*************************************************************************/
  /* Construction/destruction                                              */
  /*************************************************************************/

  ///////////////////////////////////////////////////////////////////////////
  // Default constructor, constructs an empty XString object.
  ///////////////////////////////////////////////////////////////////////////
  XString();
  
  ///////////////////////////////////////////////////////////////////////////
  // Constructs a XString object from a character pointer. If the pointer
  // is 0, an empty XString object will be constructed.
  ///////////////////////////////////////////////////////////////////////////
  XString(const char * const sz);
  XString(const wchar_t * const wsz);

  ///////////////////////////////////////////////////////////////////////////
  // Constructs a XString object from another XString object
  ///////////////////////////////////////////////////////////////////////////
  XString(const XString &string);

  ///////////////////////////////////////////////////////////////////////////
  // Constructs a XString object from a signed 32 bit integer
  ///////////////////////////////////////////////////////////////////////////
  XString(long l);

  ///////////////////////////////////////////////////////////////////////////
  // Constructs a XString object from an unsigned 32 bit integer
  ///////////////////////////////////////////////////////////////////////////
  XString(unsigned long ul);

  ///////////////////////////////////////////////////////////////////////////
  // Constructs a XString object from a single character
  ///////////////////////////////////////////////////////////////////////////
  XString(char ch);
  XString(wchar_t wch);

  virtual ~XString();

  /*************************************************************************/
  /* Modification routines                                                 */
  /*************************************************************************/

  ///////////////////////////////////////////////////////////////////////////
  // Clears the contents of the member string, effectively making it empty.
  // Returns a reference to the current XString object.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString &clear();

  ///////////////////////////////////////////////////////////////////////////
  // Replaces the contents of the member string with the string.
  // Returns a reference to the current XString object.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString &operator =(const char * const sz);
  virtual XString &operator =(const wchar_t * const wsz);
  virtual XString &operator =(const XString &xsz);

  ///////////////////////////////////////////////////////////////////////////
  // Replaces the contents of the string with the character.
  // Returns a reference to the current XString object.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString &operator =(char ch);
  virtual XString &operator =(wchar_t wch);

  ///////////////////////////////////////////////////////////////////////////
  // Replaces the contents of the member string with the string 
  // representation of the signed or unsigned 32 bit integer.
  // Returns a reference to the current XString object.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString &operator =(long l);
  virtual XString &operator =(unsigned long ul);

  ///////////////////////////////////////////////////////////////////////////
  // Inserts a string (either from character pointer or XString object) into 
  // the member string at position 'pos', which is a zero-based index (so 0 
  // will place the string entirely string before the first character of the 
  // member string).
  // If the indicated position is greater than the length of the member 
  // string, the string is inserted behind the last character of the member 
  // string.
  // Returns a reference to the current XString object.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString &insert(const char * const sz,unsigned long pos);
  virtual XString &insert(const wchar_t * const wsz,unsigned long pos);
  virtual XString &insert(const XString &xsz,unsigned long pos);

  ///////////////////////////////////////////////////////////////////////////
  // Inserts a character into the string at position 'pos'. If the position
  // is greater then the length of the string, the character is appended
  // to the string.
  // Returns a reference to the current XString object.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString &insert(char ch,unsigned long pos);
  virtual XString &insert(wchar_t wch,unsigned long pos);

  ///////////////////////////////////////////////////////////////////////////
  // Inserts the string representation of a signed or unsigned 32 bit integer
  // into the string at position 'pos'. If the position is greater then the 
  // length of the string, the number is appended to the string.
  // Returns a reference to the current XString object.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString &insert(long l,unsigned long pos);
  virtual XString &insert(unsigned long ul,unsigned long pos);

  ///////////////////////////////////////////////////////////////////////////
  // Concatenates the string to the member string.
  // Returns a reference to the current XString object.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString &concat(const char * const sz);
  virtual XString &concat(const wchar_t * const wsz);
  virtual XString &concat(const XString &xsz);
  virtual XString &operator +=(const char * const sz);
  virtual XString &operator +=(const wchar_t * const wsz);
  virtual XString &operator +=(const XString &xsz);

  ///////////////////////////////////////////////////////////////////////////
  // Concatenates a character to the string.
  // Returns a reference to the current XString object.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString &concat(char ch);
  virtual XString &concat(wchar_t wch);
  virtual XString &operator +=(char ch);
  virtual XString &operator +=(wchar_t wch);

  ///////////////////////////////////////////////////////////////////////////
  // Concatenates the string representation of the signed or unsigned 32 bit
  // integer to the string.
  // Returns a reference to the current XString object.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString &concat(long l);
  virtual XString &concat(unsigned long ul);
  virtual XString &operator +=(long l);
  virtual XString &operator +=(unsigned long ul);

  ///////////////////////////////////////////////////////////////////////////
  // Removes 'len' characters from the string, starting at 
  // position 'pos' (which is a zero-based index). If 'pos' is greater then
  // the length of the string, no characters are removed. If 'len' is greater
  // then the number of remaining characters behind 'pos', then the remainder
  // of the string is removed.
  // Returns a reference to the current XString object.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString &remove(unsigned long pos,unsigned long len);

  ///////////////////////////////////////////////////////////////////////////
  // If the string (either character pointer or XString object)
  // is found within the member string, it is removed from the member string. 
  // Note that all occurrences of the string are removed, not just the
  // first one.
  // Returns a reference to the current XString object.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString &remove(const char * const sz);
  virtual XString &remove(const wchar_t * const sz);
  virtual XString &remove(const XString &xsz);

  ///////////////////////////////////////////////////////////////////////////
  // If the character is found within the string, it is removed from the 
  // string. Note that all occurrences of the character are removed, not just 
  // the first one.
  // Returns a reference to the current XString object.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString &remove(char ch);
  virtual XString &remove(wchar_t wch);

  ///////////////////////////////////////////////////////////////////////////
  // If the string representation of the signed or unsigned 32 bit integer
  // is found within the member string, it is removed from the member string. 
  // Note that all occurrences of the number are removed, not just 
  // the first one.
  // Returns a reference to the current XString object.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString &remove(long l);
  virtual XString &remove(unsigned long ul);

  ///////////////////////////////////////////////////////////////////////////
  // Replaces all occurrances of the first string within the member string
  // with the second string.
  // Returns a reference to the current XString object.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString &replace(const char * const szSrc,const char * const szTgt);
  virtual XString &replace(const wchar_t * const wszSrc,const wchar_t * const wszTgt);
  virtual XString &replace(const XString &xszSrc,const XString &xszTgt);
  virtual XString &replace(const char * const sz,const XString &xsz);
  virtual XString &replace(const wchar_t * const wsz,const XString &xsz);
  virtual XString &replace(const XString &xsz,const char * const sz);
  virtual XString &replace(const XString &xsz,const wchar_t * const wsz);

  ///////////////////////////////////////////////////////////////////////////
  // For each character in the first character array, the string is parsed.
  // If that character is found in the string, it is replaced with the
  // corresponding character from the second character array.
  // Returns a reference to the current XString object.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString &translate(char *szSrc,char *szTgt);
  virtual XString &translate(wchar_t *wszSrc,wchar_t *wszTgt);

  ///////////////////////////////////////////////////////////////////////////
  // Converts all characters in the string to uppercase.
  // Returns a reference to the current XString object.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString &toUpper();

  ///////////////////////////////////////////////////////////////////////////
  // Converts all characters in the string to lowercase.
  // Returns a reference to the current XString object.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString &toLower();

  /*************************************************************************/
  /* Substring querying routines                                           */
  /*************************************************************************/

  ///////////////////////////////////////////////////////////////////////////
  // Returns the position of the first character in the member string of 
  // the substring in that member string that matches the string entirely.
  // If the string isn't contained in the member string, the function 
  // returns -1
  ///////////////////////////////////////////////////////////////////////////
  virtual long pos(const char * const sz) const;
  virtual long pos(const wchar_t * const wsz) const;
  virtual long pos(const XString &xsz) const;

  ///////////////////////////////////////////////////////////////////////////
  // Returns the position of the first character in the string that matches 
  // the given character.
  // If the character isn't contained in the member string, the function 
  // returns -1
  ///////////////////////////////////////////////////////////////////////////
  virtual long pos(char ch) const;
  virtual long pos(wchar_t wch) const;

  ///////////////////////////////////////////////////////////////////////////
  // Returns the position of the first character in the member string of 
  // the substring in that member string that matches the string 
  // representation of the signed or unsigned 32 bit integer entirely.
  // If the number isn't contained in the member string, the function 
  // returns -1
  ///////////////////////////////////////////////////////////////////////////
  virtual long pos(long l) const;
  virtual long pos(unsigned long ul) const;

  ///////////////////////////////////////////////////////////////////////////
  // Returns the substring from the meber string starting at position 'pos'
  // and with length 'len'.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString substr(unsigned long pos,unsigned long len) const;
  
  ///////////////////////////////////////////////////////////////////////////
  // Tests whether the string is found entirely in the member string.
  // i.e. "BCD" is contained in "ABCDEF", but "BDE" is not
  ///////////////////////////////////////////////////////////////////////////
  virtual bool contains(const char * const sz) const;
  virtual bool contains(const wchar_t * const wsz) const;
  virtual bool contains(const XString &xsz) const;

  ///////////////////////////////////////////////////////////////////////////
  // Tests whether the character is found in the string.
  ///////////////////////////////////////////////////////////////////////////
  virtual bool contains(char ch) const;
  virtual bool contains(wchar_t wch) const;

  ///////////////////////////////////////////////////////////////////////////
  // Tests whether the string representation of the signed or unsigned
  // 32 bit integer is found in the member string.
  ///////////////////////////////////////////////////////////////////////////
  virtual bool contains(long l) const;
  virtual bool contains(unsigned long ul) const;

  /*************************************************************************/
  /* Conversion routines                                                   */
  /*************************************************************************/
  
  ///////////////////////////////////////////////////////////////////////////
  // Returns the signed or unsigned 32 bit integer value which is represented
  // by the string. If conversion is not possible (the string does not
  // represent a number), 0 is returned.
  ///////////////////////////////////////////////////////////////////////////
  virtual long toLong() const;
  virtual unsigned long toUlong() const;

  ///////////////////////////////////////////////////////////////////////////
  // Casts the XString object to a character pointer.
  ///////////////////////////////////////////////////////////////////////////
  virtual operator const char * const() const;
  virtual operator const wchar_t * const() const;

  /*************************************************************************/
  /* Comparison routines                                                   */
  /*************************************************************************/
  
  ///////////////////////////////////////////////////////////////////////////
  // Returns true if the string is empty.
  ///////////////////////////////////////////////////////////////////////////
  virtual bool isEmpty() const;

  ///////////////////////////////////////////////////////////////////////////
  // Returns true if the string exactly matches the member string. This
  // operator is case sensitive.
  ///////////////////////////////////////////////////////////////////////////
  virtual bool operator ==(const char * const sz) const;
  virtual bool operator ==(const wchar_t * const wsz) const;
  virtual bool operator ==(const XString &xsz) const;
  friend bool operator ==(const char * const sz,const XString &xsz);
  friend bool operator ==(const wchar_t * const wsz,const XString &xsz);

  ///////////////////////////////////////////////////////////////////////////
  // Returns true if the string does not exactly match the member string. 
  // This operator is case sensitive.
  ///////////////////////////////////////////////////////////////////////////
  virtual bool operator !=(const char * const sz) const;
  virtual bool operator !=(const wchar_t * const wsz) const;
  virtual bool operator !=(const XString &xsz) const;
  friend bool operator !=(const char * const sz,const XString &xsz);
  friend bool operator !=(const wchar_t * const wsz,const XString &xsz);

  ///////////////////////////////////////////////////////////////////////////
  // Returns true if the string is alfabatically indexed before the member
  // string.
  // This operator is case sensitive.
  ///////////////////////////////////////////////////////////////////////////
  virtual bool operator >(const char * const sz) const;
  virtual bool operator >(const wchar_t * const wsz) const;
  virtual bool operator >(const XString &xsz) const;
  friend bool operator >(const char * const sz,const XString &xsz);
  friend bool operator >(const wchar_t * const wsz,const XString &xsz);
  
  ///////////////////////////////////////////////////////////////////////////
  // Returns true if the string is alfabatically indexed after the member
  // string.
  // This operator is case sensitive.
  ///////////////////////////////////////////////////////////////////////////
  virtual bool operator <(const char * const sz) const;
  virtual bool operator <(const wchar_t * const wsz) const;
  virtual bool operator <(const XString &xsz) const;
  friend bool operator <(const char * const sz,const XString &xsz);
  friend bool operator <(const wchar_t * const wsz,const XString &xsz);

  ///////////////////////////////////////////////////////////////////////////
  // Returns true if the string is alfabatically indexed before or equally
  // to the member string.
  // This operator is case sensitive.
  ///////////////////////////////////////////////////////////////////////////
  virtual bool operator >=(const char * const sz) const;
  virtual bool operator >=(const wchar_t * const wsz) const;
  virtual bool operator >=(const XString &xsz) const;
  friend bool operator >=(const char * const sz,const XString &xsz);
  friend bool operator >=(const wchar_t * const wsz,const XString &xsz);

  ///////////////////////////////////////////////////////////////////////////
  // Returns true if the string is alfabatically indexed after or equally
  // to the member string.
  // This operator is case sensitive.
  ///////////////////////////////////////////////////////////////////////////
  virtual bool operator <=(const char * const sz) const;
  virtual bool operator <=(const wchar_t * const wsz) const;
  virtual bool operator <=(const XString &xsz) const;
  friend bool operator <=(const char * const sz,const XString &xsz);
  friend bool operator <=(const wchar_t * const wsz,const XString &xsz);

  /*************************************************************************/
  /* Direct member querying                                                */
  /*************************************************************************/

  ///////////////////////////////////////////////////////////////////////////
  // Returns the number of characters in the string.
  ///////////////////////////////////////////////////////////////////////////
  virtual DWORD length() const;

  ///////////////////////////////////////////////////////////////////////////
  // Returns the character at position 'pos' in the string, which is a zero
  // based index. If 'pos' is greater then the length of the string, it 
  // returns 0.
  ///////////////////////////////////////////////////////////////////////////
  virtual char operator[](unsigned long pos) const;

  /*************************************************************************/
  /* Cloning routines                                                      */
  /*************************************************************************/

  ///////////////////////////////////////////////////////////////////////////
  // Returns a copy of the string.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString clone() const;

  ///////////////////////////////////////////////////////////////////////////
  // Clones the current XString object and adds a string to the clone.
  ///////////////////////////////////////////////////////////////////////////
  virtual XString operator +(const char * const sz) const;
  virtual XString operator +(const wchar_t * const wsz) const;
  virtual XString operator +(const XString &xsz) const;
  friend XString operator +(const char * const sz,const XString &xsz);
  friend XString operator +(const wchar_t * const wsz,const XString &xsz);
  friend XString operator +(char ch,const XString &xsz);
  friend XString operator +(wchar_t wch,const XString &xsz);
  friend XString operator +(long l,const XString &xsz);
  friend XString operator +(unsigned long ul,const XString &xsz);

  /*************************************************************************/
  /* Interface requirements                                                */
  /*************************************************************************/

  ///////////////////////////////////////////////////////////////////////////
  // Functions of the serializable interface.
  // Override these if you add additional data members to your
  // derived class of XString.
  ///////////////////////////////////////////////////////////////////////////
  virtual unsigned long byteSize() const;
  virtual BYTE *serialize(BYTE *pBuf) const;
  virtual BYTE *deserialize(BYTE *pBuf);

protected:
  char * m_sz;
  mutable wchar_t * m_wsz;
  unsigned long m_ulLen;

};

#endif

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 31-08 15:26

.oisyn

Moderator Devschuur®

Demotivational Speaker

hmm, hier compiled het prima met alleen die 3 methoden die je in je eerste post gaf (MSVC 7)
misschien een compiler bug?

vraagje: waarom zijn al je methoden trouwens virtual? Ga je m nog subclassen?

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Verwijderd

Het probleem is hoogst waarschijnlijk dat je de index in de [] operator niet als een int maar als een unsigned int definieert. Daardoor moet de compiler de int index eerst naar unsigned casten en heeft de [] operator geen precedence meer over de cast operators. (Overigens vraag ik me af waarom je operators als virtual definieert).
C++:
1
char operator[] ( int pos ) const;

Dit zou moeten werken volgens mij.

Verwijderd

Topicstarter
OK ten eerste wil ik jullie bedanken voor de gedane moeite.

Oisyn : kan compilerafhankelijk zijn, mischien zelfs wel een bug maar ik ben verder zeer tevreden over MSVS6 en heb momenteel geen mogelijkheid om aan MSVS7 te komen.

Mietje : je hebt gelijk. De volgende code
C++:
1
if ( xstr[ ( unsigned long ) 0] == '-' ) { }

doet het namelijk wel. Dan kies ik daarvoor en niet voor jouw oplossing om er dan maar een int van te maken, en wel omdat het hier om een zero-based-index gaat. Door het unsigned te laten hoef ik niet te checken of de gevraagde index >= 0. Overigens is dat natuurlijk gewoon een keuze maar ik vind het functioneel correcter, een negatieve index is nou eenmaal onzin in deze context :)

edit:
even ietsjes meer uitleg


De reden dat ik de meeste zaken virtual gedeclareerd heb is inderdaad omdat ik van plan ben hem nog te subclassen. Er is bovendien ook geen noodzaak ze niet virtual te declareren, dus waarom zou ik dat dan doen? Dat maakt het voor eventuele anderen die hiervan willen subclassen alleen maar onnodig moeilijk. Of zie ik dat verkeerd?

edit:
toch niet handig


Ik denk dat ik op den duur erg moe ga worden van al dat casten naar unsigned long dus dan de int versie er maar gewoon bijgeplakt Afbeeldingslocatie: http://www.xs4all.nl/~curunir/pics/usehead.gif

  • cameodski
  • Registratie: Augustus 2002
  • Laatst online: 06-11-2023
Verwijderd schreef op 03 oktober 2002 @ 17:42:
De reden dat ik de meeste zaken virtual gedeclareerd heb is inderdaad omdat ik van plan ben hem nog te subclassen. Er is bovendien ook geen noodzaak ze niet virtual te declareren, dus waarom zou ik dat dan doen? Dat maakt het voor eventuele anderen die hiervan willen subclassen alleen maar onnodig moeilijk. Of zie ik dat verkeerd?
Ik ga niet zeggen dat je het verkeerd ziet, maar helemaal goed zie je het in ieder geval niet.
Methodes/operator die virtual zijn, zijn minder efficient, omdat er regelmatig gecontroleerd moet worden of er nog overrides zijn van betreffende methode/operator. Dus het is toch beter/netter om niet onnodig virtual te gebruiken.

Never underestimate the power of


  • .oisyn
  • Registratie: September 2000
  • Laatst online: 31-08 15:26

.oisyn

Moderator Devschuur®

Demotivational Speaker

Verwijderd schreef op 03 oktober 2002 @ 17:42:
OK ten eerste wil ik jullie bedanken voor de gedane moeite.

Oisyn : kan compilerafhankelijk zijn, mischien zelfs wel een bug maar ik ben verder zeer tevreden over MSVS6 en heb momenteel geen mogelijkheid om aan MSVS7 te komen.

Mietje : je hebt gelijk. De volgende code
C++:
1
if ( xstr[ ( unsigned long ) 0] == '-' ) { }

doet het namelijk wel.
hmm in mijn test deed ik idd gewoon int, ik dacht er nog aan maar het leek me onwaarschijnlijk. Nog steeds overigens. Je roept namelijk de [] operator op XString aan, dus op dat moment zou de compiler al voor de geoverloadde [] operator moeten kiezen. Dan geef jij een 0 mee, wat eigenlijk een const int is, maar zonder problemen kan worden gecast naar unsigned long. Dat levert een char op, wat ie vervolgens vergelijkt.

Volgens mietje zou ie eerst naar de vergelijking kijken om daar te zien dat het een char is, en dus operator char * aanroept, en dan de pointer dereferenced dmv array indexing. Maar dat is helemaal niet de juiste volgorde

Ik heb het nog een keer getest in MSVC7, deze keer met een unsigned long als index, en daar krijg ik idd precies dezelfde error
g++ (2.7.2.1) compiled het echter prima
cameodski schreef op 03 oktober 2002 @ 19:25:
Methodes/operator die virtual zijn, zijn minder efficient, omdat er regelmatig gecontroleerd moet worden of er nog overrides zijn van betreffende methode/operator. Dus het is toch beter/netter om niet onnodig virtual te gebruiken.
er wordt niet 'gecontroleerd', de reden dat het inefficient is, is omdat ie de functie aanroept dmv een pointer (de instantie) naar een pointer (de vtable van die instantie) naar een pointer (de functie in de vtable). Ook kan de cpu geen branch prediction uitvoeren omdat ie niet van tevoren weet waar ie naartoe moet

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Verwijderd

.oisyn schreef op 03 oktober 2002 @ 19:51:
hmm in mijn test deed ik idd gewoon int, ik dacht er nog aan maar het leek me onwaarschijnlijk. Nog steeds overigens. Je roept namelijk de [] operator op XString aan, dus op dat moment zou de compiler al voor de geoverloadde [] operator moeten kiezen.
Dit klopt niet. De compiler moet nu kiezen uit óf een [] operator, óf een cast operator naar een pointer. De compiler zal de "best fit" kiezen, dus in wezen de operator die de minste casts kost. Normalerwijze is dat de [] operator, omdat er dan 0 casts nodig zijn. Nu moet de compiler echter eerst van int naar unsigned casten, en dan worden alledrie de operatoren "even goed" en dus ambigue.
Ik heb het nog een keer getest in MSVC7, deze keer met een unsigned long als index, en daar krijg ik idd precies dezelfde error
g++ (2.7.2.1) compiled het echter prima
g++ 2.95.2 en 3.0.1 geven beiden ook de error. Ik weet echt bijna zeker dat ik hierin goed zit, dit is me zelf ook wel eens overkomen.

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 31-08 15:26

.oisyn

Moderator Devschuur®

Demotivational Speaker

hmmm, vervelend dan :)

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Verwijderd

Topicstarter
toch altijd weer interessant die discussies :)
Eigenlijk is het jammer dat je casting operators niet 'explicit' kan maken, zoals je wel met constructors kan...
En als performance de enige reden is om voor niet-virtual methods te kiezen in dit geval : mwah, kan ik mee leven. Voor echte high-performance string operaties gebruik ik wel gewoon een char pointer als het ECHT nodig is... Hier maakt het weinig uit, ik ga er geen parsers mee bouwen o.i.d.

Verwijderd

Verwijderd schreef op 03 oktober 2002 @ 17:42:

C++:
1
if ( xstr[ ( unsigned long ) 0] == '-' ) { }


edit:
toch niet handig

Ik denk dat ik op den duur erg moe ga worden van al dat casten naar unsigned long
Als je denkt dat je erg moe gaat worden van al het typen, kan je toch ook deze veel kortere vorm gebruiken:
C++:
1
if ( xstr[0UL] == '-' ) { }

Komt op het zelfde neer, maar is toch een boel minder typewerk! :)
Pagina: 1