-
June 26th, 2013, 07:42 AM
#1
Preventing execution of a function
PHP Code:
class MyClass{}; namespace BI{ class BusinessInterop { public: static MyClass* func(){printf("BusinessInterop");return new MyClass();} }; } namespace BS { using namespace BI; class BusinessService:public BusinessInterop { public: virtual MyClass* func(){return BusinessInterop::func();} }; } namespace XS { using namespace BS; class xxxService:public BusinessService { public: virtual MyClass* func() {return func();} MyClass* funx() { return func(); } }; }
namespace XT { using namespace XS; class xxxServiceTest:public xxxService { public: virtual MyClass* func() {printf("Business"); return new MyClass();} void Call() { funx(); } }; }
I would like to know how to prevent the program print out "Business", I'd like it to call the base class's static function. I have tried to changed MyClass* into void* but it still prints Business :grumpy:.
My challenge is to output "BusinessInterop" without any changes made to the polymorphic structure as designed. Thank you.
Last edited by terminalXXX; June 26th, 2013 at 10:52 AM.
-
June 26th, 2013, 08:19 AM
#2
Re: Preventing execution of a function
Victor Nijegorodov
-
June 26th, 2013, 10:52 AM
#3
Re: Preventing execution of a function
Nice spot! Thanks, I fixed the above code, and now that it becomes umanaged C++
-
June 26th, 2013, 11:01 AM
#4
Re: Preventing execution of a function
- Where is your code printing "Business"?
- Why do you call this func() recursively?
Originally Posted by terminalXXX
Code:
class MyClass{};
...
namespace XS
{
using namespace BS;
class xxxService:public BusinessService
{
public:
virtual MyClass* func() {return func();}
MyClass* funx()
{
return func();
}
};
}
- Please, use Code tags, not the PHP ones.
Victor Nijegorodov
-
June 26th, 2013, 01:39 PM
#5
Re: Preventing execution of a function
Code:
class xxxServiceTest:public xxxService
{
public:
virtual MyClass* func() {printf("Business"); return new MyClass();}
Assuming you correct the mistakes that Victor pointed out, what is your real goal here? What's wrong with just changing that line in red to call the function you want to call?
Code:
class xxxServiceTest:public xxxService
{
public:
virtual MyClass* func() {BusinessInterop::func();}
//...
The BusinessInterop::func is a static function in BusinessInterop, and does nothing except instantiate an empty MyClass and print a message. So why didn't you just do that? There is no polymorphism involved in what you have, since MyClass isn't a base class to anything you posted
My challenge is to output "BusinessInterop" without any changes made to the polymorphic structure as designed. Thank you.
Let's say we figure out on a high-level what you really want to do (it has to be much more than what you described) -- what if there is no feasible way to accomplish what you want to do with the current way you've set this up? Or what if there is a superior way of doing things that you may never had thought of?
Regards,
Paul McKenzie
Last edited by Paul McKenzie; June 26th, 2013 at 01:41 PM.
-
June 27th, 2013, 09:06 AM
#6
Re: Preventing execution of a function
[QUOTE=terminalXXX;2122025]
PHP Code:
class MyClass{};
namespace BI{
class BusinessInterop
{
public:
static MyClass* func(){printf("BusinessInterop");return new MyClass();}
};
}
namespace BS
{
using namespace BI;
class BusinessService:public BusinessInterop
{
public:
virtual MyClass* func(){return BusinessInterop::func();}
};
}
namespace XS
{
using namespace BS;
class xxxService:public BusinessService
{
public:
virtual MyClass* func() {return func();}
MyClass* funx()
{
return func();
}
};
}
namespace XT
{
using namespace XS;
class xxxServiceTest:public xxxService
{
public:
virtual MyClass* func() {printf("Business"); return new MyClass();}
void Call()
{
funx();
}
};
}
Originally Posted by terminalXXX
I would like to know how to prevent the program print out "Business", I'd like it to call the base class's static function. I have tried to changed MyClass* into void* but it still prints Business :grumpy:.
My challenge is to output "BusinessInterop" without any changes made to the polymorphic structure as designed. Thank you.
weird question...
you don't wan to change the polymorphic structure...
but you do want to change the polymorhic behaviour implied by that structure.
the real question is missing because you didn't post the code.
but... assuming you want Call to return something else.
Change xxxServiceTest::Call() to not call funx() but call BI::func() instead. the polymorphic structure will always cause funx() to call the most derived implementation of virtual func().
-
June 27th, 2013, 10:33 AM
#7
Re: Preventing execution of a function
In my project, I must change all user-defined types except share pointers (I don't know how to deal with sharepointers because no default constructor to exchange the defined type with void/void* in the STL) to void* in order to get polymorphic behaviors exhibited otherwise the base class's static methods are always called. Why void* ?
I'll get you a view tomorrow, I also need a satisfactory answer about this odd.
-
June 27th, 2013, 11:17 AM
#8
Re: Preventing execution of a function
Originally Posted by terminalXXX
In my project, I must change all user-defined types except share pointers to void* in order to get polymorphic behaviors exhibited otherwise the base class's static methods are always called.
Why did you name the base class function "func" the same as the derived class function "func"? In the base class it is a static member, while in the derived class, it is a non-static member.
The base class version plays absolutely no role in polymorphism, as it is a static function devoid of a this pointer. All you wound up doing is hide the static version of the function in the derived classes, and instead replaced a function with a function of the same name and made it virtual. Were you expecting the "func" in the base class to be related in some way via polymorphism to the "func" in the derived classes? Well, they're not related.
Code:
class BusinessInterop
{
public:
static MyClass* funcBase() {printf("BusinessInterop");return new MyClass();}
};
Then you call funcBase() instead of func(). Then the confusion is cleared up immediately, instead of having to change things to void* (which I still don't understand why you need to do this).
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|