inheritance - How to solve diamond issue in C++? -
i have following test program
#include<iostream> using namespace std; class faculty { // data members of faculty public: faculty(int x) { cout<<"faculty::faculty(int ) called"<< endl; } void test() { cout<<"faculty::test called" << endl; } }; class student { // data members of student public: student(int x) { cout<<"student::student(int ) called"<< endl; } void test() { cout<<"student::test called" << endl; } }; class ta : virtual public faculty, virtual public student { public: ta(int x):student(x), faculty(x) { cout<<"ta::ta(int ) called"<< endl; } }; int main() { ta ta1(30); ta1.test(); }
an errors getting during compilation
8be257447d8c26ef785b1a60f2884a.cpp: in function 'int main()': 748be257447d8c26ef785b1a60f2884a.cpp:36:6: error: request member 'test' ambiguous ta1.test(); ^ 748be257447d8c26ef785b1a60f2884a.cpp:22:7: note: candidates are: void student::test() void test() { ^ 748be257447d8c26ef785b1a60f2884a.cpp:11:7: note: void faculty::test() void test() { ^
even i'm using virtual inheritance here. solution this?
there no need of virtual
keyword here, classes student
, faculty
not related via inheritance common class.
if want specific method used in ta
can put using student::test;
or using faculty::test;
inside ta
class declaration.
i hope example appeared purely educational purposes, because if it's used/planned used in real application - it's sign of something's going wrong design :)
Comments
Post a Comment