Quote:
Originally Posted by dmail
I started to edit your source The_Nerd and realised what it potential is and problems which are deeper.
I'm guessing you are wanting this to use for containers to any type of object, would this also include pointers? if so this would create problems if they were classes to polymorhpic types themselves.
Your copy operator would cause splicing of the classes and would be much better implemented as a "copy"/"clone" function as to avoid this. I was also a little confused why you were returing the this pointer to delete it? It would be better to give the base (very base) class a virtual destructor (which it should have anyway) and then just delete the pData in CTypeContainer.
Classes should use the initialisation list.
You need to check the type requested by Object is the correct type.
Then there is the point that there is no need to call if(pointer)delete pointer as its safe in C++ to delete a null pointer.
This sounds like I'm ripping it apart but that is not the intention, as you can see there many things which could go wrong with your implementation.
"I am still green around the ears when it comes to C++"
This actually surprised me as what you are trying is so close to Boost::Any that its quite remarkable.
I would suggest that if this is for anything other than practice, to use or at least look at how boost has done this as it is a stable set of classes(using internal classes). You maybe able to just include the header as you can with some boost files I'm not sure.
http://www.boost.org/doc/html/any.html
http://www.boost.org/boost/any.hpp
|
Thanks for the feedback dmail! I see what you are saying about the deleting/returning
this. You are right, I didn't think about that, I will implement it. I am not using Boost because it is bloated. Besides, since I am still learning a C++, I'd like to figure it out on my own (with the help of the kind people here at LQ.com of course
). The reason this set of classes is such a headache is that I cannot have the type forced when the intance of the object is being created (that would just be a simple template). With what I am doing (an event stack), the object must be able to be created without any type being contained/forced during creation, and then be able to contain any type later on. I am almost there! I just have this one bug to work out, and it should work fine. By the way, this should work for any data type, including pointers, because if you'll notice, it DOES create a copy of the object, and duplicates it exactly.
"You need to check the type requested by Object is the correct type."
I was already planning this, that is why I have "typeid" assigned to TypeString. I'll use this later on (after I fix this current bug), to check and make sure the types match when the user asks for the data contained.
"Then there is the point that there is no need to call if(pointer)delete pointer as its safe in C++ to delete a null pointer."
Thanks! I didn't know that.
"This sounds like I'm ripping it apart but that is not the intention, as you can see there many things which could go wrong with your implementation"
No problem! I am here to learn, and any pointers on anything, no matter how harsh, be assured I'll be paying humble grateful attention.