|
|
Hi all,
Having a problem with polymorphism and returning a class from a function.
Suppose I have base class B which is used to derive D1, D2, D3, D4, etc..
Suppose a function of B, myFunc(), has return of type B, and myFunc() creates within it a D1, D2, D3, or D4 and returns it.
If I were to try the following in a separate, or main(), function somewhere else,...
B rtn_instance = B::myFunc();
....how come rtn_instance is still of type B and not of type D1, D2, D3, etc... ?
I thought the polymorphism would make rtn_instance of type D1, D2, etc...
Maybe references would be helpful, but doesn't using references pose a memory leak risk, or do the pointed to objects get destroyed on exit from the calling function?
Many thanks for any help.
|
|
|
|
|
figured it out...
i should be using references.
Hurrah !
|
|
|
|
|
Let's do some actual code...
class B
{
public:
virtual B & myFunc () { return *this; }
virtual void SayMyName () { printf ("B"); }
};
class C : public B
{
public:
virtual B &myFunc () { return *this; }
virtual void SayMyName () { printf ("C"); }
}
main ()
{
C c;
B b = c.myFunc;
b.SayMyName ();
}
OK, pretty artificial, and I'm missing out on stuff.
It's pretty plain to me that a temporary B would be constructed, using it's copy constructor. Then if the compiler is not too clever, that temporary B would be copied to b.
If you want a polymorphic return, use pointers.
class B
{
public:
virtual B *myFunc () { return this; }
virtual void SayMyName () { printf ("B"); }
};
class C : public B
{
public:
virtual B *myFunc () { return this; }
virtual void SayMyName () { printf ("B"); }
}
main ()
{
C c;
B *b = c.myFunc;
b->SayMyName ();
}
Give that a go, I hope it helps.
Iain.
I have now moved to Sweden for love (awwww).
If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), or need cotract work done, give me a job! http://cv.imcsoft.co.uk/[ ^]
|
|
|
|
|
In addition to my answer (yours was a clue)
{
C c;
B &b = c.myFunc ();
}
would have been fine too.
I have now moved to Sweden for love (awwww).
If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), or need cotract work done, give me a job! http://cv.imcsoft.co.uk/[ ^]
|
|
|
|
|
Polymorphism works through pointers, for instance, try:
#include < iostream >
using namespace std;
class B
{
protected:
int b;
public:
B(int b){this->b = b;}
virtual void out(){cout << "hi frm B, data value is " << b << endl;}
};
class D1 : public B
{
public:
D1(int i):B(i){}
virtual void out(){cout << "hi frm D1, data value is " << b << endl;}
};
class D2 : public B
{
public:
D2(int i):B(i){}
virtual void out(){cout << "hi frm D2, data value is " << b << endl;}
};
B * myFunc(int i)
{
switch (i%3)
{
case 0:
return new B(i);
case 1:
return new D1(i);
case 2:
return new D2(i);
}
}
void main()
{
B * p, *q;
p= myFunc(5);
q= myFunc(6);
p->out();
q->out();
}
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]
|
|
|
|
|
Thanks for all your help guys. I'm gonna stick with references because I'm trying to write something for users. I don't want to return actual pointers which require the "->" operator.
Cheers
|
|
|
|
|
hi want to make a simple tic tac toe game using simple 2d arrays.without pointers.if anyone have this code .then please reply
|
|
|
|
|
sam_2009 wrote: hi want to make a simple tic tac toe game...if anyone have this code .then please reply
So if somene else gave you the code, would you still be making it?
Why don't you start, and then ask for help when you get stuck on a specific task?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
make == compile
|
|
|
|
|
I don't have this code. Does that help?
|
|
|
|
|
You can make it Or try with Google for get logic for develop code.
Thanks and Regards,
Selvam,
http://www.wincpp.com
|
|
|
|
|
|
You should post your request in the 'plz sir send me codez plz plz' one.
BTW you lost 100 man points for attempting to avoid pointers.
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]
|
|
|
|
|
CPallini wrote: BTW you lost 100 man points for attempting to avoid pointers.
HA! 5.
|
|
|
|
|
|
Hi all,
Please let me , if you know anything about windows time providers(http://msdn.microsoft.com/en-us/library/ms725475%28VS.85%29.aspx[^]).
i want to
1. Sync my local machine time with any NTP server.
Is there any other method to do that.
I think, this is possible only through Windows Time Providers...
Thanks in advance
BIJU
|
|
|
|
|
Creating and implementing a time provider is different than simply communicating with a NIST server.
bijumanjeri wrote: i want to
1. Sync my local machine time with any NTP server.
Using sockets, you can use the DAYTIME protocol to connect to 129.6.15.28, port 13. I assume using the NTP protocol (port 123) is just as easy.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Thank you David for your very quick response.
OK. Let me check with your suggestion.
BTW..
by going through the MSDN, I think, by using w32time.dll, we can control the Windows Time Service.
like forcefully sync with external NTPs. But how can we access the Windows Time Service?
Is there any APIs available for controlling the Windows Time Service?
Please help me.
Thanks
BIJU
|
|
|
|
|
bijumanjeri wrote: like forcefully sync with external NTPs.
Just for clarification purposes, NTP is the protocol used to communicate (ie, synchronize) with a (NIST) computer.
bijumanjeri wrote: Is there any APIs available for controlling the Windows Time Service?
To control services via code, see here.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Sorry David.. Sorry for the mistake. Hope you understand what i meant.
Let me try my luck..
Regards,
BIJU
|
|
|
|
|
|
Thank you guys.. Thank you for your suggestions..
|
|
|
|
|
Here is a void* problem to challenge you. I have a very large application A. Sometimes it needs to use a little application B. B instantiates an external dll I didn't write, we can call it G. B uses DllExport to export some functions I can call with DllImport. Whenever I call B it makes a new G and deletes it again. Fine.
Now they changed the rules. I can only have one G. Easy, I think. Add an Init function to B's external interface, call it to instantiate my single instance of G. Return a void* to B, so whenever I want to call it it reuses the instance of B and thus of G. A is not allowed to know what happens in B, so the void* is perfect. I keep void* mPointerToB as a member of A.
B has some internal classes, we can call one ClassM. So B contains a member ClassM mM. When I hop into B it runs for a while until it tries to call mM.function. Then I get a buffer overrun.
So I googled and found this statement. "A void pointer cannot point to a class member in C++." Is my void pointer to B losing its internal objects along the way? Where can I find out more? Am I doing this in the wrong way?
This is C++ in Visual Studio 2008. I use void* in other places to access pointers to things in Ada functions in the same way, so it should work, I think.
-------------
Ave computer! Hackitura te salutat!
(I just made this up. Just to show you can hack Latin as well as C++.)
|
|
|
|
|
RedSonja wrote: When I hop into B it runs for a while until it tries to call mM.function. Then I get a buffer overrun.
What do you mean by 'hop into B', how are you executing the functions in B?
RedSonja wrote: So I googled and found this statement. "A void pointer cannot point to a class member in C++." Is my void pointer to B losing its internal objects along the way? Where can I find out more? Am I doing this in the wrong way?
I'm not sure if this statement applies here. As you say you can use a void* to hold a pointer to anything, it is only when you use it to access the object it points to, that you may run into problems. If the void* points to an instance of a class then you would have to cast it to a CLASS* in order to use the object.
Buffer overrun can be caused by a multitude of sins and if it is in the dll then you probably need to talk to the dll's developers for assistance.
|
|
|
|