By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,081 Members | 1,317 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,081 IT Pros & Developers. It's quick & easy.

How to call abstract function

P: n/a
There was a way to trinck program to call purly abstract non-existing
method (causing undefined behaviour), I would like to try that for
curiosity (how different compilers/platforms react), how to do it?
Feb 16 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
if Class A is an ABC you can invoke A->f() such that f() is a pure
virtual from A's destructor

Feb 16 '06 #2

P: n/a
> Rafał Maj Raf256wrote:
There was a way to trinck program to call purly abstract
non-existing
method (causing undefined behaviour), I would like to try that for
curiosity (how different compilers/platforms react), how to do it?

namespace
{
class Base
{
public:
virtual void doSomething() = 0;

void test()
{
doSomething();
}
};

class Sily : public Base
{
public:
Sily() { test(); }
virtual ~Sily() { }
};

class Derived : public Sily
{
public:
Derived() { doSomething(); }
virtual ~Derived() { }

virtual void doSomething()
{
}
};
}

int main()
{
Derived();
return 0;
}
Feb 16 '06 #3

P: n/a
Rafał Maj Raf256 wrote:
There was a way to trinck program to call purly abstract non-existing
method (causing undefined behaviour), I would like to try that for
curiosity (how different compilers/platforms react), how to do it?


Try qualifying the name with the class it's in:

object.ABC::function(params);

I don't understand the purpose, though. What knowledge are you going
to derive from that "exercise"?

V
--
Please remove capital As from my address when replying by mail
Feb 16 '06 #4

P: n/a
Victor Bazarov wrote:
Try qualifying the name with the class it's in:

object.ABC::function(params);

I don't understand the purpose, though. What knowledge are you going
to derive from that "exercise"?


I tried to just this and it turns out that at least gcc catches this
attempt at link time: it issues an unresolved symbol error for
'ABC::function()'. What works actually involves three classes:

struct base
{
virtual ~base() {}
virtual void f() = 0;
void call() { this->f(); }
};

struct aux: base { aux() { this->call(); } };
struct derived: aux { void f() {} };

int main() { derived obj; }
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 16 '06 #5

P: n/a
> JetSnaiLwrote:
[quote:22d7f86efe="Rafał Maj Raf256"]There was a way to trinck
program to call purly abstract non-existing
method (causing undefined behaviour), I would like to try that for
curiosity (how different compilers/platforms react), how to do it?

namespace
{
class Base
{
public:
virtual void doSomething() = 0;

void test()
{
doSomething();
}
};

class Sily : public Base
{
public:
Sily() { test(); }
virtual ~Sily() { }
};

class Derived : public Sily
{
public:
Derived() { doSomething(); }
virtual ~Derived() { }

virtual void doSomething()
{
}
};
}

int main()
{
Derived();
return 0;
}
[/quote:22d7f86efe]

Hey, guys. The code I provide works with MS Visual Studio 6, 7.0 and
7.1 compilers. I think it will work with gcc and others too.

Feb 16 '06 #6

P: n/a

Dietmar Kuehl wrote:
What works actually involves three classes:

struct base
{
virtual ~base() {}
virtual void f() = 0;
void call() { this->f(); }
};

struct aux: base { aux() { this->call(); } };
struct derived: aux { void f() {} };

int main() { derived obj; }


I believe your goal is to call a purely abstract function (in other
words f() in 'base')?

That isn't successfully calling a non-existant function. "derived" has
an f. Therefor the call to f() within derived's immediate parent is
calling an existing function...it is calling derived's version of f().
To prove this place 'cerr << "THIS IS DERIVED'S F().";' in derived's
version of f().

So long as you are not in a constructor virtual calls go to the most
derived definition. In aux 'this' is a pointer to an aux but the very
definition of a virtual function means that a call to f() through that
pointer still resolves to the definition in 'derived'. Inside of
constructors this is undefined I believe and, though the virtual
mechanism might still work, your derived class is not fully constructed
(construction is top->down basically) so the very likely result is an
explosion.

Feb 16 '06 #7

P: n/a

roberts.n...@gmail.com wrote:
Dietmar Kuehl wrote:
What works actually involves three classes:

struct base
{
virtual ~base() {}
virtual void f() = 0;
void call() { this->f(); }
};

struct aux: base { aux() { this->call(); } };
struct derived: aux { void f() {} };

int main() { derived obj; }
I believe your goal is to call a purely abstract function (in other
words f() in 'base')?

That isn't successfully calling a non-existant function. "derived" has
an f. Therefor the call to f() within derived's immediate parent is
calling an existing function...it is calling derived's version of f().
To prove this place 'cerr << "THIS IS DERIVED'S F().";' in derived's
version of f().


Nevermind that, you are in a constructor...the result is undefined I
think though doesn't necessarily call the abstract function. That's
why I make class names capitalized.
So long as you are not in a constructor virtual calls go to the most
derived definition. In aux 'this' is a pointer to an aux but the very
definition of a virtual function means that a call to f() through that
pointer still resolves to the definition in 'derived'. Inside of
constructors this is undefined I believe and, though the virtual
mechanism might still work, your derived class is not fully constructed
(construction is top->down basically) so the very likely result is an
explosion.


Feb 16 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.