Click here to Skip to main content
15,743,299 members

Comments by geoyar (Top 5 by date)

geoyar 10-Feb-12 14:30pm View    
Thanks for reply.

1.You are right about virtual methods. Nobody is sure how the dynamic_cast is implemented, but some people hint that it is possible than the first entry in the vtable in some compilers is a pointer to the type info, that was set at compile time, and the dynamic_cast just uses the same vtables as virtual methods do

2. The example does not should be complete, but IMHO should show essentials. People wit very different levels of C++ skills will read it.
By a way, is the Child derived from Base?
3. Because the comparison is not shown, this post is about comparison of the template parameters only. So static_cast is not related to this post. After you have determined the template parameters are the same, you are free do what you want. I would remove the cast.
4.Your method of generating ID leads to different IDs for the same template parameter, because the OS might allocate different memory pages for the different instances of the app. Of course, they are constant inside the given app instance.
5. You are saying that your code is the fastest. Did you have any numbers?
geoyar 9-Feb-12 21:39pm View    
1. You do not want to use run time type info (dynamic_cast), but you are actually using RTTI because you are using virtual functions. The call to the virtual function is resolved at run time: the app determines the dynamic type of the pointer to the base class and looks into the virtual functions table of the basic class. As you remember, if you have BaseClass* bPtr = new ChildClass<t>, the bPtr has a static type BaseClass* and dynamic type ChildClass<t>*.
2. The code you wrote has bugs: no virtual destructors, bool equal() never returns true, the child class has no ancestor.
3. If your intention is to compare types, why do you do a static_cast? Return true if ids are the same, and that's it.
4. In ClassID() you wrote:
static int id;
return (int)&id;
On 64bit system the address is 64 bit. If you truncate it, you can't guarantee uniqueness.

In my opinion, using the ID to compare class types is legit, but your implementation is deeply flawed.
geoyar 30-Jan-12 21:47pm View    
I do not think it is safe and good. All code is based on assumption that somebody have assigned right IDs to both classes. What if the IDs are the same, but the child class is bigger than other (e.g has more data members)? You can't cast up the inheritance ladder, only down.
Because you are not using inheritance, the only sense it makes is to cast between instantiations for the different T of the same class template.
This is a case for using TypeLists and static_asserts.
Set the TypeList for types that allow casts. and overload the cast operator
template <typename t,="" typename="" t1=""> childClass<t> operator ()(const childClass<t1>)
and in body of that operator trow static_assert if the T1 is can't be casted to T
geoyar 9-Nov-11 13:01pm View    
Yes I do:
typedef std::basic_string<tchar> string_t.
geoyar 18-Oct-10 21:20pm View    
Reason for my vote of 2
Is it much easier to write DelegateCreateThread() than CreateThread(...)?
I do not agree.