473,662 Members | 2,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to call abstract function

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
7 2817
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
> 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
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::fun ction(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
Victor Bazarov wrote:
Try qualifying the name with the class it's in:

object.ABC::fun ction(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.co m> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 16 '06 #5
> JetSnaiLwrote:
[quote:22d7f86ef e="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:22d7f86ef e]

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

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

roberts.n...@gm ail.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...t he 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2296
by: Hubert Baumeister | last post by:
Fifth International Conference on eXtreme Programming and Agile Processes in Software Engineering XP2004 June 6-10, 2004, Garmisch-Partenkirchen, Germany http://www.xp2004.org/
17
6636
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products. So I shall have an Abstract Base called 'Car' implemented by Toyota, Ford, and Buick. Further I'd like to enable to client to say Car *factory;
9
46581
by: Dario | last post by:
This is a technical C++ post regarding the Microsoft runtime error R6025 Pure Virtual Function Call that sometime occurs in programs compiled with Microsoft Visual C++ 6.0. Please consider the following simple illegal C++ program: class Listener { public: virtual void onEvent(int n) = 0;
9
1544
by: Paul Bilnoski | last post by:
Can someone explain why a call to a virtual function within a constructor is illegal? The class can't be instantiated because functions are abstract, so whatever extends it implements them which means they should be implemented when the constructor for the base is called. Is it because the base gets constructed before the rest of the object and the vftable doesn't exist? I think something like this works in Java. class AbstractBase
26
3117
by: Adam Warner | last post by:
Hello all, I'm very new to C but I have a number of years of Common Lisp programming experience. I'm trying to figure out ways of translating higher order concepts such as closures into C. The code will not be idiomatic C. GCC has an extension to ISO C that permits nested functions: <http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html> For implementing closures they have a serious limitation:
0
1412
by: Armin Rigo | last post by:
Hi all, A shameless plug and reminder for EuroPython 2006 (July 3-5): * you can submit talk proposals until May 31st. * there is a refereed papers track; deadline for abstracts: May 5th. See the full call for papers below.
0
1424
by: arigo | last post by:
Hi all, He're a reminder to submit a talk at EuroPython! Like each year, we have both the regular conference (see call at http://indico.cern.ch/conferenceCFA.py?confId=13919) and a somewhat separated Refereed Papers section. Here is the call for the latter. The deadline for both is the 18th of May.
6
2345
by: leoman730 | last post by:
This is one of the interview question this morning, hope someone can help out with this. Thanks. What is the different between System call and library call?
275
12248
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
8435
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8768
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8547
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7368
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4181
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1754
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.