Re: data corruption on pointer cast
On Jul 16, 4:32 pm, Robert Bauck Hamar <roberth+n...@ifi.uio.no>
wrote:
Old Wolf wrote:
On Jul 16, 5:56 pm, gara.m...@gmail.com wrote:
Heyllo,
template<class T>
class Element
{
public:
virtual int operator == (T) = 0;
virtual int hash() = 0;
};
int exists(Element<T> * elem)
{
int h = elem->hash()%M;
for (int i=0; i < size_t[h]; i++)
This is a syntax error -- size_t is a keyword,
No, it's not. From the standard's point of view, it's a defined type from
the standard library. <cstddef>, <cstdio>, <cstring> and <ctime> should all
define std::size_t, and thus, their .h version puts size_t in the global
namespace. As std::size_t is a typedef from the C libraries, ::size_t is
also a reserved name. But it's not a keyword; maybe you're thinking of
wchar_t?
you can't apply array indexing to it
Though a poorly chosen name, it is legal. Size_t is a data member of the
class.
--
rbh
Hey guys, thanks for all the replies. I'll try to address each issue
brought up. First I've received almost universal criticism on using
size_t, which I agree in whole, what can I say? It seemed like a good
idea at the time ;)
I've heard some discontent in some poster with my use of realloc, but
coming from a C background I tend to like to manually allocate memory.
I am fairly certain allocating space for an array of pointers works
just the same in C++ as in C, since are they not at the very core just
integers (in particular data types with no constructors?)
Next, some people disapprove of me using "so many" pointers in my
code, and I would love to take your advice on this topic because I'm
used to C. Can you be more specific, for example, do you mean I should
not pass pointers to classes as parameters but rather pass the classes
by reference? What other advantages other than a cosmetic change to
code readability does this have over the other way? I do, however,
oppose the idea of storing a set of elements in the class rather than
a set of pointers to the elements since I may want to keep the
"actual" objects elsewhere.
Next, I'd like to thank james for pointing out the std::set class, I
was totally oblivious to it, although I knew about vector and list, go
figure. Reinventing the wheel is painful, but it is instructive,
although I make no claims that my code is half as efficient as the std
code.
Also Erik in your suggestion of the implementation, how would you pass
in the hash function as a parameter? Just a regular function pointer?
And how can you guarantee that the == operator would be implemented,
other than the obvious default operator?
Last but not least, old wolf, I think you may have pointed out what
I've been doing wrong, I'll try it out.