Preventing execution of a function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Preventing execution of a function

  1. #1
    Join Date
    Jun 2012
    Posts
    38

    Preventing execution of a function

    PHP Code:
    class MyClass{};
    namespace 
    BI{
        class 
    BusinessInterop
        
    {
        public:
            static 
    MyClassfunc(){printf("BusinessInterop");return new MyClass();}
        };
    }
    namespace 
    BS
    {
        
    using namespace BI;
        class 
    BusinessService:public BusinessInterop
        
    {
        public:
            
    virtual MyClassfunc(){return BusinessInterop::func();}
        };
    }
    namespace 
    XS
    {
        
    using namespace BS;
        class 
    xxxService:public BusinessService
        
    {
        public:    
            
    virtual MyClassfunc() {return func();}
            
    MyClassfunx()
            {
                return 
    func();
            }
        }; 
    }


    namespace 
    XT
    {
        
    using namespace XS;
        class 
    xxxServiceTest:public xxxService
        
    {
        public:
            
    virtual MyClassfunc() {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 11:52 AM.

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,606

    Re: Preventing execution of a function

    Is it managed code?
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2012
    Posts
    38

    Re: Preventing execution of a function

    Nice spot! Thanks, I fixed the above code, and now that it becomes umanaged C++

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,606

    Re: Preventing execution of a function

    1. Where is your code printing "Business"?
    2. Why do you call this func() recursively?
      Quote Originally Posted by terminalXXX View Post
      Code:
      class MyClass{};
      ...
      namespace XS
      {
      	using namespace BS;
      	class xxxService:public BusinessService
      	{
      	public:	
      		virtual MyClass* func() {return func();}
      		MyClass* funx()
      		{
      			return func();
      		}
      	}; 
      }
    3. Please, use Code tags, not the PHP ones.
    Victor Nijegorodov

  5. #5
    Join Date
    Apr 1999
    Posts
    27,446

    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 02:41 PM.

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,100

    Re: Preventing execution of a function

    [QUOTE=terminalXXX;2122025]
    PHP Code:
    class MyClass{};
    namespace 
    BI{
        class 
    BusinessInterop
        
    {
        public:
            static 
    MyClassfunc(){printf("BusinessInterop");return new MyClass();}
        };
    }
    namespace 
    BS
    {
        
    using namespace BI;
        class 
    BusinessService:public BusinessInterop
        
    {
        public:
            
    virtual MyClassfunc(){return BusinessInterop::func();}
        };
    }
    namespace 
    XS
    {
        
    using namespace BS;
        class 
    xxxService:public BusinessService
        
    {
        public:    
            
    virtual MyClassfunc() {return func();}
            
    MyClassfunx()
            {
                return 
    func();
            }
        }; 
    }


    namespace 
    XT
    {
        
    using namespace XS;
        class 
    xxxServiceTest:public xxxService
        
    {
        public:
            
    virtual MyClassfunc() {printf("Business"); return new MyClass();}
            
    void Call()
            {
                
    funx();
            }
        };

    Quote Originally Posted by terminalXXX View Post
    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().

  7. #7
    Join Date
    Jun 2012
    Posts
    38

    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.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Preventing execution of a function

    Quote Originally Posted by terminalXXX View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center