simple override question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: simple override question

  1. #1
    Join Date
    Mar 2009
    Posts
    17

    Question simple override question

    hello,

    OK i need a little help,

    i want to override a funtion from a parent class, but i can seem to get it to work,

    so i have a layout the same as below, i want to override the Z function from class A in class B.
    when i use this layout below it still calls the Z function from A rather than B, i am creating the object from B so from my understanding it should be overwrote...

    can anyone help me out with this?

    thanks
    Philly

    Class A Header:
    Code:
    #pragma once
    
    class A
    {
    private:
        void Y();
    protected:
        virtual void Z();
    };
    class A Source:
    Code:
    #include "stdafx.h"
    #include "A.h"
    
    void A::Y()
    {
        this->Z();
    }
    
    void A::Z()
    {
        // want to override this in class B;
    }
    Class B Header
    Code:
    #pragma once
    #include "A.h"
    
    class B : A
    {
    protected:
        void Z();
    };
    Class B Source:
    Code:
    #include "stdafx.h"
    #include "B.h"
    
    void B::Z()
    {
        // Do Somthing Here!
    }
    Last edited by StaticPhilly; March 12th, 2009 at 07:28 AM.

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

    Re: simple override question

    1. Why don't you declare void Z() in class B as a virtual?
    2. Where and how do you call "the Z function from A rather than B..."?
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2009
    Posts
    17

    Re: simple override question

    its unmanaged c++ (not clr or mfc)

    the reason behide void Z not being virtual in B is because its not the parent function but rather a function to override its parent. i would only have to declare it as virtual in class B if i was going to override class B::Z as well.

    the reason for Z being called from class A is that class A dose all the hard work before calling,
    the whole idea in making a parent class (class A) is so i dont have to duplicate my code everywere, insted just override what i need to.
    just to make this a bit more clear,
    Code:
    class A:
    void Start(), creates a new thread to StartProcessing
    void StartProcessing(), dose some work then passes the data to ProcessCommand
    bool ProcessCommand(string command), this is what i want to override and is virtual
    
    class B:
    bool ProcessCommand(string command), this is the override and is not virtual
    
    My Usage:
    B* test = new B();
    B->Start();
    Last edited by StaticPhilly; March 12th, 2009 at 07:46 AM.

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

    Re: simple override question

    Neither of your posts does contain any example of using these virtual methods. So how do you expect we could help you if we cannot test/reproduce your problem?
    Victor Nijegorodov

  5. #5
    Join Date
    Mar 2009
    Posts
    17

    Re: simple override question

    Class A:
    Code:
    protected: virtual bool ProcessCommand(string, string);
    public: static void StartProcessing(PVOID);
    
    void TCPSession::StartProcessing(void* Link)
    {
    	// Create TCPSession base from link
    	TCPSession* base = (TCPSession*)Link;
    
    	// Start Receaving...
    	base->_TCPReceiveBuffer->Start();
    
    	// Welcome
    	base->_TCPSocket->SendData("+OK Welcome!\r\n");
    
    	// String var
    	string CommandLine;
    	size_t CommandSize;
    	size_t CommandSplitPosision;
    	string Command;
    	string CommandParameter;
    
    	while (true)
    	{
    		if (base->_TCPReceiveBuffer->DataAvilable())
    		{
    			// Read the command
    			CommandLine = base->_TCPReceiveBuffer->ReadLine();
    
    			// Get the command size and split posision 
    			CommandSize = CommandLine.size();
    			CommandSplitPosision = CommandLine.find_first_of(" ");
    
    			// get the command and parameter if passed
    			if (CommandSplitPosision == -1)
    			{
    				Command = CommandLine;
    				CommandParameter = "";
    			} else {
    				Command = CommandLine.substr(0, CommandSplitPosision);
    				CommandParameter = CommandLine.substr(CommandSplitPosision, CommandSize);
    			}
    
    			// Convert the command to upper case
    			Misc::StrToUpper(Command);
    
    			// Process
    			if (base->ProcessCommand(Command, CommandParameter))
    			{
    				break;
    			}
    
    		}
    		Sleep(100);
    	}
    
    	base->_TCPReceiveBuffer->Stop();
    	base->_TCPSocket->Close();
    	delete[] base;
    }
    
    bool TCPSession::ProcessCommand(string command, string parameter)
    {
    	if (command == "QUIT")
    		return true;
    	return false;
    }
    Class B:
    Code:
    protected bool ProcessCommand(string, string);
    
    bool RAPSession::ProcessCommand(string Command, string Parameter)
    {
    	if (Command == "QUIT")
    	{
    		this->Quit(); 
    		return true;
    	}
    
    	return false;
    }
    
    void RAPSession::Quit()
    {
    	this->_TCPSocket->SendData("+OK Good-Bye\r\n");
    }

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,011

    Re: simple override question

    There is no any attempt to access/call RAPSession::ProcessCommand in the code snippet you have posted.
    You only call the TCPSession::ProcessCommand method.
    Victor Nijegorodov

  7. #7
    Join Date
    Mar 2009
    Posts
    17

    Re: simple override question

    yes i was affraid it would be that,

    is there any way round this without overriding my StartProcessing() to tell it to call the new ProcessCommand function

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,011

    Re: simple override question

    Quote Originally Posted by StaticPhilly View Post
    is there any way round this without overriding my StartProcessing() to tell it to call the new ProcessCommand function
    What do mean by "the new ProcessCommand function"? Perhaps, you meant RAPSession::ProcessCommand?
    Yes it would be possible if the object calling it were of the type RAPSession rather than TCPSession
    Victor Nijegorodov

  9. #9
    Join Date
    Mar 2009
    Posts
    17

    Re: simple override question

    thanks for your help VictorN i do apreashate it,

    i am a little confused tho,

    because,
    Im going to have 4 classes that enherit TCPSession class,
    I dont really want to implant a new TCPSession::StartProcessing function into all my classes (thats what calls the TCPSession::ProcessCommand that i was wanting to override)
    and with project going to have 4 classes that inherit TCPSession its not as simple as just changing the call in TCPSession::StartProcessing.

    so im still ratting my brain on how to do this,
    starting to think along the lines of adding a new function to TCPSession to set a private var (TCPSession* _child), get TCPSession::ProcessCommand to use that _child to call the ProcessCommand and then finerly in my classes that enherit TCPSession call that function to set the var to its self. somthing like SetChild((TCPSession*)this)

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,011

    Re: simple override question

    Sorry, but I couldn't understand anything from your explanation.
    So I think I should give up...
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center