what is the equvalent of
CTypedPtrArray<> in borland c++5?
Printable View
what is the equvalent of
CTypedPtrArray<> in borland c++5?
CTypedPtrArray is something very specific to the MFC. Actually it's something very stupid which was added to keep compatibility with older apps but at the same time take advantage of the "new" templates feature that was added to C++ after the original MFC came out, while at the same time writing as little code as possible.
I don't think you will find a direct equivalent in the OWL.
But of course you can use an STL vector.
class TShape:public TStremable
{
private:
..................
.................
pulbic:
virtual void DrawShape(TDC* pDC);
};
class TLine :public TShape
{
public:
virtual void DrawShape(TDC* pDC);
};
class TCircle :public TShape
{
public:
virtual void DrawShape(TDC* pDC);
};
typedef TArray<TShape> TShapes;
typedef TArrayIterator<TShape> TShapeIterator;
now using this
how to draw the corosponding shape(DrawShape());?
You must make the array of type TShape* or TShape& in order to make polymorphism possible, else you can't add shape-derived classes to the array.
In the above example.Even If I use pointer it is giving error
CShape* pShape;
pShape=new CLine(............);
What error?
Cast type error
???
Very strange...
I get the followind errors at pShape=new CLine(............);
1 Type name expected
2 suspicious pointer conversion
Are you sure pShape is declared correctly and in the right scope?
Thank U very much from helping me.
it is working now that I add a containr constructor.But I don't now why we need a container constructor in base class(CShape)?
And why do we need upper and lower and delta?when it can grow and shrink dynamically in the following?
TArrayAsVector( int upper, int lower = 0, int delta = 0 ) ;
Thank U very much from helping me.
it is working now that I add a containr constructor.But I don't know why we need a container constructor in base class(CShape)?
And why do we need upper and lower and delta?when it can grow and shrink dynamically in the following?
TArrayAsVector( int upper, int lower = 0, int delta = 0 ) ;
Makes it more efficient and generic. You can have a from-to array like in VB and you can specify a large delta when you notice it would help your app (e.g. if you always add several elements in short succession). Delta is how much additional memory this thing allocates when it gets too small.
Thanks.
I think form-to-array doesn't grow.It is just to specify the number of item in it and the index is transformed.isn't it?
Can i give the three parameter TArrayAsVector(0,0,0)?
Now I have done
typedef TArray<CShape*> CShapes;
typedef TArrayIterator<CShape*> CShapesIterator;
CShapes m_shapes=new CShapes(5,0,5);
void CMyWindow::OpenFile()
{
ifstream is("c:\\abc.dot");
if (!is)
MessageBox("Unable to open file", "File Error", MB_OK | MB_ICONEXCLAMATION);
else {
CMyApp* app=(CMyApp*)GetApplication();
app->m_shapes->Flush();
unsigned numPoints;
is >> numPoints;
while (numPoints--)
{
CShape point;
is >> point;
app->m_shapes->Add(point);
}
}
Invalidate();
}
it gives erron on
is>>point
what is wrong with it?
Does CShape have an overloaded >> operator? Is it even valid to create a simple CShape object (from an OOP design point of view CShape should be abstract, thus the line
CShape point;
should be invalid)?
No I have no done like that.But I hope it is saing to a file.
This code is giving no error.
void CMyWindow::SaveFile()
{
ofstream os("c:\\abc.dot");
if (!os)
MessageBox("Unable to open file", "File Error", MB_OK | MB_ICONEXCLAMATION);
else
{
CMyApp* app=(CMyApp*)GetApplication();
os << app->m_shapes->GetItemsInContainer();
CShapesIterator i(*app->m_shapes);
while (i)
os<<i++;
}
}
This doesn't mean anything. Just because a class has an overloaded << operator (Has it? Or does the iterator have one? Or does the iterator have an implicit conversion to some pointer type than can be inserted into the stream?) it doesn't mean it also has an overloaded >> operator.
And CShape is CShape. Simple value types allow no polymorphism. You have to think of a different method of serializing your objects.