|
Lets say I want to do simulations on a group of animals of class CAnimal. First I define a group of dogs of class CDogs inheriting from CAnimals. Then I want to do the same with cats of class CCats, also inheriting from CAnimals, etc.
Lets say we have the following setup:
- A CAnimal object has member data and a set of pure virtual functions
- A CDog or CCat has only static member data in addition to member functions, including the virtual functions from CAnimal.
I would now like to have the ability to change between dogs and cats, i.e. redefine all my CAnimals from being dogs to cats. If I don't make my CDog and CCat destructors virtual, I can delete the dog/cat part and keep the CAnimal base class, but how can I change type?
I guess one way to do it is to take my array of dog objects and make a new array of cat objects and copy the animal part from dog to cat using assignement operator, and then delete may dog array. Or are there more elegant ways..?
Regards, Haakon S.
|
|
|
|
|
I don't thing you can dynamically change the type of an object at runtime, you will need to do this by hand, like you suggested.
Max.
|
|
|
|
|
This can be done (if I understood your question) implementing an idiom called envelope/letter. Define CAnimal as an envelope class, i.e. a class that forwards all requests to another object:
class CAnimalImpl;
class CAnimal
{
void method1(){impl->method1();}
...
int methodn(...){return impl->methodn(...);}
void setImpl(CAnimalImpl *impl){this->impl=impl;}
}; Now, move the definition of your dogs and cats classes to CAnimalImpl , whose signature mimics that of CAnimal :
class CAnimapImp
{
virtual void method1()=0;
...
virtual int methodn(...)=0;
};
class CDog: public CAnimalImp
{
...
};
... Now, you have the ability to turn a dog into a cat simply by calling setImpl . Some details remain to make this robust, like for instance deciding whose responsibility it is to destroy CAnimalImpl s, and define proper copy constructors.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Very interesting!
This could be what I'm looking for. But it doesn't look dead simple and straightforward, if I may say. There wil be some searches with "letter/envelope" as keyword during the weekend.
But thanks alot!
Haakon.
|
|
|
|
|
Look also for "handle/body" and "pimpl", which are alternate names of this idiom.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
check also for "pimpl" private implementation; the Effective C++ book has a section on this.
Max.
|
|
|
|
|
How about adding copy constructors for in each class for the opposite type like you suggest.
Another method, which I just did, is combine them and add a bool (or enum) stating which animal it is. (Okay, it's ugly but in my case, I needed only one "cat" for every 10 "dogs" and the difference was that the "cat" required two less data members than "dog", but was otherwise identical, so it isn't exactly similar.)
|
|
|
|
|
My original project was using VC98\include\ as the directory. Recently, because I need to use one of defined valuable (PM_QS_PAINT)which is defined in Platform SDK, so I installed SDK with the option "Integrate with VC++" and move the SDK include directory to the top of the directory in options dialog in VC++. When I complied, I got compile errors from StdAfx.h like " error in ..\vc98\include\transact.h: miss ";" before some valuable.". If I removed the SDK directory below the VC++ include directory, those error disappeared, but got error for undefined valuable "PM_QS_PAINT".
Anyone knows how to fix those problem? Thanks.
mIchAel Liu
__________________________________________________________
The secret of business is to know something that nobody else knows.
|
|
|
|
|
I want to know that which message and related parameters gets generated when we bring the mouse at the corners and sides of window and resize it.
|
|
|
|
|
WM_SIZE
read the documentation, sometimes, it helps ...
Max
|
|
|
|
|
There are a number of messages that get sent, and it also depends on whether you have FullWindowDragging set depending on which messages actually get sent. The previous poster answered your question with WM_SIZE, which is true. But there are more messages that get sent when you are resizing a window.
If you give a little detail of what you are trying to do, we can probably give you a better answer that will help you accomplish what you are doing.
Good Luck.
Build a man a fire, and he will be warm for a day Light a man on fire, and he will be warm for the rest of his life!
|
|
|
|
|
Add to the previous replies : you can use Spy++ , it will show all the messages.
|
|
|
|
|
can i calculate width and hegiht of a string with help of log font.
i can not use =pDC->GetOutputTextExtent(*txt); or underlaying API.
some hint or way.
thanks in anticipation.
regards
|
|
|
|
|
|
Yes Performance. i am to calculate font size to display it with in
a rect. i am to do it for at different angles . and for for more than
200 entities at a time.
i have done it with GetOutputTextExtent but it becomes too slow.
my function is like this.
long counter=1;
long Y1 =0;
long Y2 =0;
long X1=0;
long X2=0;
if(Vertical==TRUE)
{
tfont->SetEscapement(900);
Y1=rect->top;
Y2 = rect->bottom;
X1 = rect->left;
X2 = rect->right;
}
else
{
Y1 = rect->left;
Y2 = rect->right;
X1 = rect->top;
X2 = rect->bottom;
}
while(true)
{
font->SetHeight(counter);
CFont * oldfont = pDC->SelectObject(font);
// Problem each time i am selecting it in device context to check for new
// size. that makes it slow
CSize sz =pDC->GetOutputTextExtent(*txt);
if(sz.cx>=(Y2-Y1)||sz.cy>=(X2-X1)) //text size is equal to rect
{
pDC->SelectObject(oldfont);
return counter; // so return counter;
}
pDC->SelectObject(oldfont);
counter++;
}
return counter;
|
|
|
|
|
What about CDC::DrawText with DT_CALCRECT?
Wenn ist das Nunstück git und Slotermeyer? Ja! Beierhund das oder die Flipperwaldt gersput!
|
|
|
|
|
Hi,
I have some relative paths (like "../../mydir/myfile.htm") to convert to absolute paths "http://www.myserver.com/mydir/myfile.htm".
Is there something like _tfullpath for file systems?
Thanks
R.
|
|
|
|
|
It s to good to answer one's own question: InternetCombineUrl suits my needs perfectly
R.
|
|
|
|
|
Hi,
I need to add to my application some stuff for html generation and editing, I'm thinking about using MSHTML ( and make an small editor, with some basic stuff), or using Front Page Automation ...
Is Front Page automation more or less ok ? ( I mean Word Automation is not that bad... but outlook automation seems to be hell...).
Thanks in advance, greetings
Braulio
|
|
|
|
|
If you use VC7 with MFC than you have a very handy class called CHtmlEditView which provides just what it promises: simple Html editing capability.
R.
|
|
|
|
|
Hi,
I try to show a bitmap in my dialog based app. using the static control. I use this code:
CBitmap bmp_1;
bmp_1.LoadBitmap(IDB_BITMAP_1);
m_Bmp.SetBitmap(bmp_1);
m_Bmp.Invalidate();
UpdateData(FALSE);
This works good under Windows XP but not under windows 98.
What do i wrong??
Thnks
|
|
|
|
|
What kind of data type is m_Bmp ??
Rickard Andersson@Suza Computing
C# and C++ programmer from SWEDEN!
UIN: 50302279
E-Mail: nikado@pc.nu
Speciality: I love C#, ASP.NET and C++!
|
|
|
|
|
A static picture control (CStatic).
|
|
|
|
|
Use CPictureBox insted, or what the nbame is..
Rickard Andersson@Suza Computing
C# and C++ programmer from SWEDEN!
UIN: 50302279
E-Mail: nikado@pc.nu
Speciality: I love C#, ASP.NET and C++!
|
|
|
|
|
I know it's a little bit simple, but i can't do it.
Thanks
|
|
|
|