473,785 Members | 2,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

virtual funcs basics

WHat is the difference between?
Could someone explain how it's implemented, what goes on behind the
scenes in latter case?
thanks..
a)
class base {
public:

base();
~base();
void virtual func_1();
:
:
void virtual func_n();

};
class derived:public base{
public:
derived();
~derived();
void virtual func_1()
:
:
void virtual func_n();
};

vs.
b)

class base {
public:

base();
~base();
void virtual func_1();
:
:
void virtual func_n();

};
class derived: virtual public base{
public:
derived();
~derived();
void virtual func_1()
:
:
void virtual func_n();
};

Jul 22 '05 #1
3 1189
puzzlecracker wrote:
WHat is the difference between?
None.
Could someone explain how it's implemented, what goes on behind the
scenes in latter case?
Nothing special. Virtual inheritance is only important in the case of
multiple inheritance down the road and it has really nothing to do with
virtual functions (except that the same keyword is used in both).
thanks..
[..]

Jul 22 '05 #2
OK, I actually [implicitly ]meant the case of multiple inheritance,
particularly case of 'dirty diamond'. How does the virtuality keeps
only one copy of the base ? Can you give low and high level overview,
any relevent examples... Thanks a bunch

Victor Bazarov wrote:
puzzlecracker wrote:
WHat is the difference between?
None.
Could someone explain how it's implemented, what goes on behind the
scenes in latter case?


Nothing special. Virtual inheritance is only important in the case

of multiple inheritance down the road and it has really nothing to do with virtual functions (except that the same keyword is used in both).
thanks..
[..]


Jul 22 '05 #3
puzzlecracker wrote:
OK, I actually [implicitly ]meant the case of multiple inheritance,
particularly case of 'dirty diamond'. How does the virtuality keeps
only one copy of the base ? Can you give low and high level overview,
any relevent examples... Thanks a bunch


(a) Please don't top-post.

(b) I am not sure what's low and high level overview, to be honest with
you. If the compiler encounters a virtual base class in a hierarchy of
a particular UDT, it generates a layout where a subobject of that base
class is represented only once. How it does that is not really defined
in the language. If you need to learn how your compiler does that, you
can always cast the most derived class pointer to the virtual base class
and see the difference in the pointer values, which should give you some
idea on where in the derived class object the virtual base resides. IOW,
experiment, and you shall figure out the low level stuff.

(c) If the same class is inherited from both virtually and non-virtually
in the same hierarchy, only the virtual part of it is merged into a single
instance:

struct A {}; struct B : virtual A {}; struct C : A {};
struct D : virtual A {};
struct X : B, C, D {};

Here, every 'X' will have two instances of 'A': C::A and the virtual one,
not one and not three.

Victor
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
7281
by: Geiregat Jonas | last post by:
I've seen some c++ code where before a method there was vitual ex: class foo{ public: virtual bool method(); } what does virtual do ?
23
4497
by: Giancarlo Niccolai | last post by:
Hello all. I have peeked through the FAQ and all relevant links, and also through Stroustrup book, but I have not been able to find an answer, so I have to post here as a last resort. It makes sense that if you have virtual destructors, they are eventually used in the explicit destructor call when using the placement new semantic: class A {
3
1734
by: trialproduct2004 | last post by:
Hi all, Can someone tell me how virtual functions works. Like how memory is getting allocated to virtual function. And how to call base class function through derived class pointer. And why virtual function can be access only through pointer. Thanks in advance.
2
2452
by: david.sanderson | last post by:
Hi, Hopefully this is thecorrect group, its a bit C++ and a bit POSIX... Ive had a look in the archives, and Im a bit confused.... I have some classes in a shared library (a .so on a QNX system) which I want to instantiate into shared memory so that 2 processes can both operate on the same data. The classes have a quite derived ineritance tree (not sure if this is the correct term) where I have several classes derived from a base...
7
3092
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see item 7 for why this is virtual ...
17
3549
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
7
2909
by: Christopher Pisz | last post by:
My problem is my derived class is getting called twice instead of the base and then the derived. I thought this was the purpose for virtuals and dynamic casting :/ I want my base class to have its method called and then the derived class have its method called. What am I not understanding? Int the following code, my Event Tester class is getting called twice for keyboard events when I step through the debugger:...
5
1376
by: dmitrey.kroshko | last post by:
hi all, I need to create a Python list of lambda-funcs that are dependent on the number of the ones, for example F = for i in xrange(N): F.append(lambda x: x + i) however, the example don't work - since i in end is N-1 it yields x+ (N-1) for any func.
2
2861
by: DanielJohnson | last post by:
Hi, I want to study vtable (virtual functions and its behavior) and what it contains. I am a newbie learning C++ and using g++ compiler. Can you throw some pointers as to how to use g++ or gdb to look the contents of vtable ? Every help is greatly appreciated.
0
9643
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
10147
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...
0
8971
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
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
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...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
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.