Click here to Skip to main content
16,023,224 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

If possible, can someone help and tell me what happens in internal when we use virtual inhertence to construct a sealed class?
I have the following two code snippet: The mere difference between the two is the virtual keyword. But if we don't use it, then , behaviour is not as expected.Please help me why it is so?

C#
#include<iostream>

using namespace std;

class Base;

class final_lock
{
                friend class Base;
        private:
                final_lock() {}
                final_lock(const final_lock&) {}
};

class Base : public virtual final_lock { ///// Here virtual is necessary... why is it so? How does it make any difference internally?

public:
        Base() { }
        Base(char*) {}
        void fun(){ cout<<"I am in base"<<endl; }

};


class Der : public Base { };

int main()
{
        ///Der dd; // Cannot construct a Der obj... why it it so?
        ///dd.fun();
        Base m;
}


C#
#include<iostream>

using namespace std;

class Base;

class final_lock
{
                friend class Base;
        private:
                final_lock() {}
                final_lock(const final_lock&) {}
};

class Base : public final_lock { //// Here virtual is not present.

public:
        Base() { }
        Base(char*) {}
        void fun(){ cout<<"I am in base"<<endl; }

};


class Der : public Base { };

int main()
{
        Der dd; /// Here we can construct a  Der obj!!!!!
        dd.fun();
        Base m;
}


I know a vptr is being constucted after use use virtual inheritence and the virtual base class is being constructed first. But how does it help in making sealed class? Please tell me internally what make it happen? Please let me know if I am not clear with my problem.
Posted
Comments
Emilio Garavaglia 27-Jan-11 3:15am    
Which compiler and version are you using?
Which compiler switches are used in compiling?
Yuvaraj Gogoi 27-Jan-11 3:44am    
compiler switches are the default ones. I have not changed any of them.
Yuvaraj Gogoi 27-Jan-11 3:42am    
I am using g++ compiler(GNU compiler). Version is 4.4.1 [gcc-4_4-branch revision 150839] (SUSE Linux)

1 solution

See "Inheritance - multiple and virtual inheritance: What special considerations do I need to know about when I inherit from a class that uses virtual inheritance?"[^].

It states:


Initialization list of most-derived-class's ctor directly invokes the virtual base class's ctor.

:)
 
Share this answer
 
v2
Comments
Yuvaraj Gogoi 27-Jan-11 3:33am    
///most-derived-class's ctor directly invokes the virtual base class's ctor.

So if there is no virtual inheritence, then the base class constuctor is called by the immediate next derived constructor. Am I right?
Thanks a lot for the answer!!!

Cheers!!!
Yuvaraj Gogoi
CPallini 27-Jan-11 3:42am    
Yeah, your interpretation is like the mine.
BTW: You are welcome.
Emilio Garavaglia 27-Jan-11 4:09am    
hummm ... The virtual makes the invoking order to change, but if final_lock::final_lock() is private how can it be invoked from whatever derived? There is something more then just the order, here.
CPallini 27-Jan-11 4:38am    
Base class may call final_lock::final_lock on behalf of Der.
Try the following code with and without class C


#include<iostream>

using namespace std;

class B;
class C;

class final_lock
{
friend class B;
private:
final_lock() {}
final_lock(const final_lock&) {}
};

class B : public final_lock
{
public:
B() { }
void fun(){ cout<<"I am in B"<<endl; }
};

class C : public final_lock
{
public:
C() { }
void fun(){ cout<<"I am in C"<<endl; }
};

class Der : public B, public C { };

int main()
{
Der d;
}
Yuvaraj Gogoi 27-Jan-11 4:53am    
Good example!!
This is because class C is not a friend of class final_lock.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900