creating a dynamic variable type

Started by
45 comments, last by thre3dee 15 years, 11 months ago
Quote:Original post by thre3dee
there is a way of using a void* dtor* variant type and have type checks.


Given the small fact that your code will cause an access violation/seg fault in the constructor in the exact same way that code you've posted in another thread causes an access violation/seg fault in the constructor, it's very hard to believe that you've actually tested this code. If you want accurate comments about your code, you'll need to post your actual code.
Advertisement
Quote:Original post by thre3dee

Err, what I meant to say was, this is no complete solution. I was merely showing a way of storing any type of object in one class. My actual propety object I made had full type checks, copy constructors and anythnig else I needed.

You'll never learn anything if you don't try it yourself anyway.


And, in interest of learning, I will point out that your design suffers from exactly the same flaws as boost::any. If you try to add copy semantics, it will also use a sizeof(void*) more memory than boost version.

This is why I'd suggest to look the alternative I posted, which requires 2*sizeof(void*) per variable, and doesn't require heap allocations for types with sizeof <= sizeof(void*).
Quote:Original post by SiCrane
Quote:Original post by thre3dee
there is a way of using a void* dtor* variant type and have type checks.


Given the small fact that your code will cause an access violation/seg fault in the constructor in the exact same way that code you've posted in another thread causes an access violation/seg fault in the constructor, it's very hard to believe that you've actually tested this code. If you want accurate comments about your code, you'll need to post your actual code.


I'd love to know how setting three pointers to zero causes any sort of error given the fact that any subsequent operations are based on the assumption that a blank Variant has a null _ptr member.
_type isn't a pointer. If you had actually run the code that you posted, you'd see that passing 0 as a std::string constructor will cause an access violation. Obviously you didn't.
Hahaha woops. Sorry about that error. I'm so used to using plain old char* that I forgot that I'd used a string for this example.

Strings are good just theres certain places you don't need them and I tend to try not to use them if I don't need a copy of a string such as const char* in a function call etc.

Can't believe I missed that.
Quote:...and I tend to try not to use them if I don't need a copy of a string such as const char* in a function call etc.
In such cases you can usually just pass a string object by constant reference. Ta-da! No copy.

(Also, some implementations of std::string are able to avoid unnecessary copies/allocations under certain circumstances, but as this behavior is implementation-dependent it shouldn't really be counted on.)
Quote:Original post by jyk
Quote:...and I tend to try not to use them if I don't need a copy of a string such as const char* in a function call etc.
In such cases you can usually just pass a string object by constant reference. Ta-da! No copy.

(Also, some implementations of std::string are able to avoid unnecessary copies/allocations under certain circumstances, but as this behavior is implementation-dependent it shouldn't really be counted on.)

Exactly. I don't assume that it would it would anyway, so I just ask for a const char* because I know its only a pointer and I prefer it that way.

std::strings aren't the super holy grail that everyone HAS to use ALL the time. I myself don't throw in a vector<> when all is needed is something like int [4] or use a string when I need a char tmp[32] for any temporary strings writes i may do in a function.

If I need a container then I'll use STL (or my own template containers), otherwise I prefer to keep it small and simple and use native types.

This topic is closed to new replies.

Advertisement