Re: C++ Conversion functions for pointers

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


Date: 30 Apr 2004 06:58:14 -0700


"B. v Ingen Schenau" <bart@ingen.ddns.info> wrote in message news:<c6t5so$8rtcf$1@ID-135549.news.uni-berlin.de>...
> Paul wrote:
>
> > 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&.
>
> The only flaw is that it does not work, because you are invoking undefined
> behaviour. :-(

No your incorrect.

>
> >
> > 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.
>
> You might want to take a look at boost::Any for an example how a proper
> variant class can be created.
>
> >
> > 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;
>
> You might want to use a dynamic_cast<> here. That immediately catches any
> attempt to assign an object that is not really of type ObjA.

Good point.
I'll look into this.

>
> > 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];
>
> Because itsArray always has the type Variant*, this will only work if
> DEFAULT_TYPE happens to be Variant.

No you don't seem to understand what I am trying to do.
Variant can convert to/from built in types. The code posted is
stripped down to simply int types.
Also as I stated, any UDT's must derived form Variant therefore a UDT
can be assigned to a Variant*.

> For any other type, you will either get a compile error, or you invoke UB
> later down the line.

No
I can use types that derives from Variants for UDT's but for built in
types what happens is..
The variant conversion constructor is called for that particular type.
i.e:
Variant(int x):intData(x){std::cout<<"Construcing Variant(int).\n";}
So the object is actually a variant but if all the main operators are
overloaded correctly it will behave like an integer.

But the problem I am having is converting an int pointer to a Variant
pointer.

>
> > }
> > ~ObjArray(){std::cout<<"Destrucing ObjArray.\n"; delete [] itsArray;}
>
> You can not use the delete[] operator if there is a mismatch between the
> static and dynamic types of its operand.
I had worried about this here but i'm not sure what your saying is the
same as what I was worrying about.

SO say I have the classes Class1 and Class2 which both derive from
Varaint.
if I create an array of varaint pointers and assign a mixture of
CLass1's and Class2's to these pointers then I must delete each one
individually?
 

> So, in normal language, you can not use 'delete[] itsArray' if itsArray does
> not point at an array of objects of type Variant.
Or a subclass of Varaint? ( A class that derives from Variant ).

> This is because otherwise the compiler does not know the size of each
> object, which makes it impossible to call the destructors for the objects
> with indices 1 and higher.

But the compiler can use the virtual destructor no?

>
> The same situation arises when you try to index the array.

I dunno what you mean here and I'm not sure you are correct with your
interpretation of the last situation.
>
> <snip>
> >
> > AHA TIA
> > Paul.
> >
> Bart v Ingen Schenau

Thanks Bart for your reply but I'm not sure you seen my question.

I will try to explain better here
Note this is all psuedo code.

class Varaint{
public:
Varaint(int& arg) {itsData(arg);}

public:
int itsData;
};

right here we have a class which will convert to an integer.
so if I use an integer where a Variant is expected everything converts
ok.

but what I want to do is convert and int* so I try this:
class Varaint{
public:
Varaint(int& arg) {itsData(arg);}
Varaint(int* arg { itsData(*arg);}

public:
int itsData;
};

this does not work when I try to create a new.
Ahh I think the solution has just come to me. I need to overload the
new operator.
Thanks for your help I'm going to research this now.
Paul.



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)