C++ Conversion functions for pointers

From: Paul (paulmmm01_at_hotmail.com)
Date: 04/30/04


Date: 29 Apr 2004 20:21:38 -0700

Hi group,
I have worked out a way to return different types by subscript from a
Variant class with the prerequesites that UDT's
a)inherit from a Variant class and..
b)they oveload the assignment operator to take an argument of type
Variant&.

However I have stumbled upon a problem when I try to convert an
integer pointer to a Variant pointer. I can think other ways to
achieve my goal but I would like to build the Variant class in such a
way that is can implicitly convert basic pointer types such as integer
pointers into a Variant pointer.

Here is some code stripped down to only have conversion for integers:
#include <iostream>

class Variant{
public:
        Variant(){std::cout<<"Construcing Variant(Default).\n";}

        /*Conversions for basic types*/
        Variant(int x):intData(x){std::cout<<"Construcing Variant(int).\n";}
        operator int(){return intData;}
        int& operator=(const int& rhs){ intData = rhs ; return intData;}
        
        virtual Variant& operator=(Variant&){return *this; }
        virtual ~Variant(){std::cout<<"Destrucing Variant.\n";}
public:
        /* list of basic types*/
        int intData;
};

/* A UDT must derive form Variant */
/* Also must overload the assignment operator to take a Variant&*/
class ObjA:public Variant{
public:
        ObjA():itsData(0){std::cout<<"Construcing ObjA(Default).\n";}
        
        ObjA& operator=(Variant& rhs){
                if(this == &rhs)
                        return *this;
                itsData = static_cast<ObjA&>(rhs).itsData;
                return *this;
        }
        
        ObjA& operator=(ObjA& rhs){
                if(this==&rhs)
                        return *this;
                itsData = rhs.itsData;
                return *this;
        }

        void setData(int x){itsData = x;};
        ~ObjA(){std::cout<<"Destrucing ObjA.\n";}
private:
    int itsData;
};

template<class DEFAULT_TYPE=Variant>
class ObjArray{
public:
        ObjArray():itsSize(0){std::cout<<"Construcing(Default ObjArray).\n";}
        ObjArray(int size):itsSize(size){
                std::cout<<"Construcing ObjArray.\n";
                itsArray = new DEFAULT_TYPE[size];
        }
        ~ObjArray(){std::cout<<"Destrucing ObjArray.\n"; delete [] itsArray;}
        
        ObjArray(const ObjArray& rhs){std::cout<<"Copying ObjArray.\n";
        itsSize = rhs.itsSize;
        itsArray = new Variant[itsSize];
        for(std::size_t i=0; i<itsSize; i++)
        itsArray[i]= rhs.itsArray[i];
        }

        ObjArray& operator=(const ObjArray& rhs){std::cout<<"Assigning
ObjArray.\n";
        if(this == &rhs)
                return *this;
        /*will need to delete and reallocate*/
        /* but working on a more efficient solution*/
        for(std::size_t i=0; i<itsSize; i++)
        itsArray[i]= rhs.itsArray[i];
                return *this;
        }
        Variant& operator[](std::size_t index){
                if(index< itsSize){return (Variant&)itsArray[index];}
        }
private:
        std::size_t itsSize;
        Variant* itsArray;
};

int main()
{
        ObjArray<ObjA> objArr1(3);
        /* uncomment to see error */
        /*ObjArray<int> objArr2(3);*/
}

Ok the idea is to have an array of different types all convertable to
Variant types, which works fine as tested using individual types
withing the class i.e: An integer will convert to a Varaint.

The problem is ..how do I convert an integer pointer to a Variant
pointer, so I can initialise dynamically? As the error shows when line
3 in main is uncommented.

AHA TIA
Paul.

Note: In case your wondering I haven't worked out yet the best way for
changing a particular indexes type, probably have a few different
methods i.e: template functions , non template functions and
constructors. So for example you can construct an Array of mixed types
with a template arguments list i.e:
ObjArray<int, double , MyObject, bool> myVariableArray(10);
Where the last 6 elements would be default types.
And functions like....
ChangeTypeAtIndex<MyNumberObj>( 0 );



Relevant Pages

  • Re: How to casting to VARIANT*
    ... > The second parameter to be passed is a range 'object'. ... but I wonder why MSWORD is designed this way. ... in there that just take a plain VARIANT. ... optional are also passed by pointer. ...
    (microsoft.public.vc.atl)
  • Re: Pass arguments with IDispatch.Invoke
    ... Marshal.StructureToPtr(v, LibraryVersion, true); ... I'm getting pointer to the structure from C, ... Alexander Nickolov wrote: ... The problem is a VARIANT does ...
    (microsoft.public.vc.atl)
  • Re: How to casting to VARIANT*
    ... Microsoft MVP, MCSD ... but I wonder why MSWORD is designed this way. ... > in there that just take a plain VARIANT. ... > optional are also passed by pointer. ...
    (microsoft.public.vc.atl)
  • Re: Variants
    ... For UDTs, arrays, objects etc, a ... Variant always contains a pointer. ... But strings in Variants are pointers, ...
    (microsoft.public.vb.general.discussion)
  • Re: want to resize window in vbs
    ... variant using the varptr function. ... UserWrap.MessageBoxA Null, CStr, "From DynaWrap Object", 3 ... i get "0" for the pointer. ... > for byref parameters, BUT ONLY STRING parameters. ...
    (microsoft.public.scripting.wsh)