473,465 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Create 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 1167
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
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
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...
3
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...
2
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...
7
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...
17
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;"...
7
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...
5
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...
2
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
1
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.