|
Here is a trivial example.
class Car{
public:
virtual string SayName() = 0;
};
class Mercedez : public Car{
public:
string SayName(){
return "Mercedez";
}
};
class Ferrari : public Car{
public:
string SayName(){
return "Ferrari";
}
};
Car* CreateACar(string carType){
if(carType == "Mercedez")
return new Mercedez;
else if(carType == "Ferrari")
return new Ferrari;
} Use it like
Car* car = CreateACar("Mercedez");
std::cout << car->SayName(); Read about factory design pattern.
|
|
|
|
|
I had implemented DummyBase this way only. But there was problem accessing functions of class B & C through main() . After figuring out why, I have another serious problem now. Actual Implementations of
class B and C are:<br />
<pre><code><br />
class DummyBase {};<br />
<br />
template <'class abc'><br />
class Base{<br />
public:<br />
abc func1();<br />
};<br />
<br />
class B : public Base`<datatype1`>, DummyBase{<br />
};<br />
class C : public Base`<datatype2`>, DummyBase{<br />
};<br />
<br />
class A: public Base`<datatype3`>, DummyBase{<br />
public:<br />
B obj1;<br />
C obj2;<br />
DummyBase* search(int condition)
{<br />
if(condition==1)<br />
return &obj1;<br />
if(condition==2)<br />
return &obj2;<br />
if(condition==3)<br />
return this;<br />
}<br />
};<br />
<br />
void main()<br />
{<br />
DummyBase *ptr;<br />
A objA;<br />
int n;<br />
cin>>n;<br />
ptr = objA.search(n);<br />
ptr->func1();
}<br />
Now the problem is that func1() cannot be included in DummyBase since return type of func1() is a template parameter. How to solve this.....
|
|
|
|
|
Plz do reply posting solution to my problem (other than restructuring class Hierarchies). It quite urgent!!
|
|
|
|
|
That's a tricky one. I'd probably try it like this:
Add virtual destructors to DummyBase, B and C, to ensure a v-table. Then you can use dynamic_cast to work out which typ has been returned:
class DummyBase { virtual ~DummyBase() {} };
template <'class abc'>
class Base{
public:
abc func1();
};
class B : public Base`<datatype1`>, DummyBase{
virtual ~B() {}
};
class C : public Base`<datatype2`>, DummyBase{
virtual ~C() {}
};
class A: public Base`<datatype3`>, DummyBase{
public:
B obj1;
C obj2;
DummyBase* search(int condition)
{
if(condition==1)
return &obj1;
if(condition==2)
return &obj2;
if(condition==3)
return this;
}
};
void main()
{
DummyBase *ptr;
A objA;
int n;
cin>>n;
ptr = objA.search(n);
if (B* bPtr = dynamic_cast<B*>(ptr))
{ bPtr->func1(); }
if (C* bPtr = dynamic_cast<C*>(ptr))
{ bPtr->func1(); }
}
Yeah, it sucks because you're taking so many decisions. There are probably better ways, but I've not really put sufficient thought into it to think of a better one.
Possibly something like Boost.Variant or Boost.Any might make things nicer.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Stuart Dootson wrote: Yeah, it sucks because you're taking so many decisions.
Yes, that's a quite heck of a job. Actually search() function is like a framework function which links 2 separate applications. Now if this approach is to be implemented then I have to do a lot of coding at caller side. Also there are about 40-50 functions(may increase with further development) which would call search . And this approach is quite rigid and for instance if new class M object is introduced in class A a lot work would be required. So a more efficient flexible solution is required.
Stuart Dootson wrote: But I've not really put sufficient thought into it to think of a better one.
If possible could you please spare some time to think of a better one??? I have nearly wasted a day trying to find workarounds for it and now i cannot think more since here in India it's midnight now and i'm feeling sleepy.
Stuart Dootson wrote: Possibly something like Boost.Variant or Boost.Any might make things nicer.
I am not familiar with Boost libraries. So how could they be implemented??
|
|
|
|
|
With Boost, this is the best I can think of (note that I've substituted int, cahr, float for the template parameters):
#include <iostream>
#include <boost/variant.hpp>
template <class abc>
class Base{
public:
abc func1();
};
class B : public Base<int>{
public:
virtual ~B() {}
};
class C : public Base<float>{
public:
virtual ~C() {}
};
class A: public Base<char>{
public:
B obj1;
C obj2;
boost::variant<A*, B*, C*> search(int condition)
{
if(condition==1)
return &obj1;
if(condition==2)
return &obj2;
if(condition==3)
return this;
}
};
int main(int, char**)
{
A objA;
int n;
std::cin>>n;
boost::variant<A*, B*, C*> v = objA.search(n);
if (B* b = boost::get<B*>(v))
{
b->func1();
}
else if (C* c = boost::get<C*>(v))
{
c->func1();
}
return 0;
}
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thanks for sparing some time.... But isn't this approach similar to previous approach??? Still I have to code a lot at caller side as in last solution... Is there no way to reduce coding at caller side??
|
|
|
|
|
NeoAks007 wrote: Is there no way to reduce coding at caller side??
Not really - you have different return types for the different implementations of func1 - you have to handle those differently. If you want to handle them n the same way, then why not make them the same?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Stuart Dootson wrote: If you want to handle them n the same way, then why not make them the same?
It cannot be made same because their functionalities are different in different classes... I understand that this is holiday but still if anyone reading this thread comes up with an idea, do post a solution please... I need it seriously..... I am still listening.....
modified on Sunday, March 15, 2009 1:36 PM
|
|
|
|
|
I think you're making this way too complicated. If you change the return type, the code handling that will have to change as well.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
In the End, I had to restructure all my class hierarchies and make the return type same for all functions. So, the problem is now solved!!
|
|
|
|
|
MFC, STUDIO 2008
How to convert CString to int, for example:
CString m_s;
m_s = “1949”;
in i;
i = itoa(m_s); - uncorrectly
char m[5];
strcpy(m,m_s); - uncorrectly
|
|
|
|
|
It's atoi [^]:
CString m_s = _T("1949");
int i = _tstoi(m_s);
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
|
You are welcome.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hi all
I have to develop windows device driver for PCI card.Can anybody suggests a starting point for doing it.
Any examples or link
I am having experience in developing a printer driver.
Thanks in advance.
With regards
Deepu
|
|
|
|
|
How to place a Bitmap in the center of a Cdocument document on startup? If tried to find it in codeproject and MSDN but neither shows what I want. I want to place the Bitmap in the center of the document when the program loads(opens). could someone show me code for doing this in MFC? Please.
Thanks in advance. The ID of the Bitmap is IDB_FRACTK.
A C++ programming language novice, but striving to learn
|
|
|
|
|
Look around for a "splash screen" (here or using google...), is that what you mean?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
No this is NOT what I'm looking for. I once saw an article that displayed a Bitmap in the CDocument/View interface, but I've lost it somehow. I'll keep hoping someone has one and meanwhile I'll keep looking.
A C++ programming language novice, but striving to learn
|
|
|
|
|
I'm still not sure what you mean but...did you try overwriting the OnPaint method of your view class (or OnDraw maybe?) and do something like this:
CBitmap bitmap;
bitmap.LoadBitmap(IDB_WHATEVER);
CDC MemDC;
MemDC.CreateCompatibleDC(&dc_of_view);
CBitmap *originalBitmap = MemDC.SelectObject(&bitmap);
BITMAP bitmapInfo;
bitmap.GetBitmap(&bitmapInfo);
CRect Rect;
GetClientRect(&Rect)
dc.BitBlt(Rect.CenterPoint().x - bitmapInfo.bmWidth / 2,
Rect.CenterPoint().y - bitmapInfo.bmHeight / 2,
bitmapInfo.bmWidth,
bitmapInfo.bmHeight,
&MemDC,
0,
0,
SRCCOPY);
MemDC.SelectObject(originalBitmap);
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
Yes, that's exactly what I was looking for. Question?: everytime the bitmap gets covered what command do I use to redraw it? (UpdateWindow??)
A C++ programming language novice, but striving to learn
|
|
|
|
|
Once again i am not sure what exactly do you mean but to trigger redrawing of your view you can either use RedrawWindow[^] or InvalidateRect[^] (maybe in conjunction with UpdateWindow right after).
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
Thanks again.
A C++ programming language novice, but striving to learn
|
|
|
|
|
Windows has triggered a breakpoint in Gobang.exe.
This may be due to a corruption of the heap, which indicates a bug in Gobang.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while Gobang.exe has focus.
The output window may have more diagnostic information.
The text above is the message That show me when i debug my project.
Do any body ever run across problem like this,help me.thanks in advance.
|
|
|
|
|
Hi all,
I want to use SendMessage(). Through CMainFram class object it works fine but using CWinApp class as base class, it doesn't work. It is not giving any error, but SendMessage() is not called.
I use the following code.
CSampleCMSApp * poApp = (CSampleCMSApp*)AfxGetApp();
SendMessage((HWND)poApp,WM_DISP,0,(long)recv_data);
If i use poApp->SendMessage((HWND)poApp,WM_DISP,0,(long)recv_data);, it give error as
error C2039: 'SendMessageW' : is not a member of 'CSampleCMSApp'.
Can i use SendMessge for the class which is derived from base class as CWinApp?.
If yes, how can i do this.
If no, why i can't?
Please reply.
Thanks,
Abhijit
|
|
|
|