Sign in to follow this  

overriding virtual functions dynamically

This topic is 4307 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a way to dynamically over write a virtual function w/ another function (maybe allready created) during run time. I dont know if C++ can do this? Can this be done in another languange if not? (maybe some function pointer or something) The purpose is to avoid unwanted callback requests from client nodes. ie DOS attacks which bog a server node's process.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You can create an class that just has a virtual method in it, with no data. To change the method called, you just replace this object with a subclass with the new method.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You should be able to set up a system like this in in C++ where you have something along the lines of this (off the top of my head)

class Foo
{
protected:
int (Foo::*bar_ptr)(int a); // member function pointer
int bar_impl_A(int a); // function implementation
int bar_impl_B(int a); // function implementation

public:
Foo()
{
setA();
}

void bar(int a)
{
this->*bar_ptr(a);
}

void setA()
{
bar_ptr = &Foo::bar_impl_A;
}

void setB()
{
bar_ptr = &Foo::bar_impl_B;
}
};

--Steven Ashley

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sorry, I had a couple of errors, this should clear it up a bit better :P


// Function pointer based

class Foo
{
protected:
int (Foo::*bar_ptr)(int a);

int bar_impl_A(int a)
{
return a+a;
}

int bar_impl_B(int a)
{
return a*a;
}

public:
Foo()
{
setA();
}

int bar(int a)
{
return (this->*bar_ptr)(a);
}

void setA()
{
bar_ptr = &Foo::bar_impl_A;
}

void setB()
{
bar_ptr = &Foo::bar_impl_B;
}
};




// State based

class Foo
{
protected:
bool state;

int bar_impl_A(int a)
{
return a+a;
}

int bar_impl_B(int a)
{
return a*a;
}

public:
Foo()
{
setA();
}

int bar(int a)
{
if(state)
return bar_impl_A(a);
else
return bar_impl_B(a);
}

void setA()
{
state = true;
}

void setB()
{
state = false;
}
};




// Main

#include <iostream>

int main(int argc, char * argv[])
{
Foo foo;

foo.setA();
std::cout << foo.bar(5) << "\n";

foo.setB();
std::cout << foo.bar(5) << "\n";

return 0;
}


Share this post


Link to post
Share on other sites
Heres the setup :

CLIENT::ACCESS_SERVER()
{
SERVER.ACCESS(*CLIENT);
}

class SERVER, public : TERMINAL, PROCESS, ...

virtual SERVER::ACCESS(*) // Abstract Base Class (TERMINAL)
{
*CLIENT.CALL(SERVER_DEFINED_VARIABLE);
}


I was thinking about using function pointers.... but its to contrived. To static, Im looking for the ultimate dynamic override. But Im not sure if its possible to override the virtual pointer after compiling code. So I will go with the intermediary terminal class, one that can be configured to allow access or deny, or do password checks some sort of open seasame deal.

Then Again I could use a handler, which utilizes function pointer, ... Ill probably use an intermediary class.

Share this post


Link to post
Share on other sites
There is no standard C++ way to override a virtual method. The standard itself doesn't mandate a pointer to virtual function table.

How are function pointers contrived? This is precisely one of those situations why the language has function pointers.

Share this post


Link to post
Share on other sites

This topic is 4307 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this