13,192,426 members (48,371 online)
Forgot your password?
Sign in with
Chapters and Sections
Article Help Forum
Submit an article or tip
Post your Blog
Ask a Question
View Unanswered Questions
View All Questions...
All Message Boards...
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Design and Architecture
C / C++ / MFC
ATL / WTL / STL
Objective-C and Swift
Hardware & Devices
Hosting and Servers
Silverlight / WPF
Site Bugs / Suggestions
Spam and Abuse Watch
The Insider Newsletter
The Daily Build Newsletter
Most Valuable Professionals
The Insider News
The Weird & The Wonderful
General Indian Topics
General Chinese Topics
What is 'CodeProject'?
Ask a Question
Bugs and Suggestions
Article Help Forum
Advertise with us
About our Advertising
Comments by geoyar (Top 5 by date)
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?
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;
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.
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
Yes I do:
typedef std::basic_string<tchar> string_t.
Reason for my vote of 2
Is it much easier to write DelegateCreateThread() than CreateThread(...)?
I do not agree.
Web01 | 2.8.171017.2 | Last Updated 1 Jan 1900
All Rights Reserved.
Terms of Service