多重继承中的重复调用问题如何有效解决?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1380个文字,预计阅读时间需要6分钟。
本文实例讲述了C++多重继承引发的重复调用问题及其解决方法。分享给广大读者,供大家参考,具体如下:
C++多重继承引发的重复调用问题分析
首先,通过一个简单的C++多重继承功能示例,介绍如何引发重复调用问题。
示例代码:
cppclass Base1 {public: void func() { cout << Base1::func() < class Base2 {public: void func() { cout << Base2::func() < class Derived : public Base1, public Base2 {}; int main() { Derived d; d.func(); // 输出:Base1::func() return 0;} 在这个例子中,`Derived` 类继承自 `Base1` 和 `Base2`,而这两个基类都定义了 `func()` 函数。当调用 `d.func()` 时,程序会输出 `Base1::func()`,这表明 `func()` 函数被重复调用。 解决方法 为了避免重复调用问题,我们可以采用以下几种方法: 1. 虚继承:使用虚继承可以避免重复调用,因为虚继承会创建一个共享的基类实例。 示例代码: cppclass Base1 {public: void func() { cout << Base1::func() < class Base2 : virtual public Base1 {public: void func() { cout << Base2::func() < class Derived : public Base1, public Base2 {}; int main() { Derived d; d.func(); // 输出:Base2::func() return 0;} 2. 重载函数:通过重载函数名称,可以避免重复调用。 示例代码: cppclass Base1 {public: void func() { cout << Base1::func() < class Base2 {public: void func() { cout << Base2::func() < class Derived : public Base1, public Base2 {public: void func() { cout << Derived::func() < int main() { Derived d; d.func(); // 输出:Derived::func() return 0;} 3. 使用友元函数:将函数定义为基类的友元函数,可以避免重复调用。 示例代码: cppclass Base1 {public: void func() { cout << Base1::func() < class Base2 {public: void func() { cout << Base2::func() < class Derived : public Base1, public Base2 {public: void func() { cout << Derived::func() < class FriendFunc {public: static void func(Derived& d) { d.func(); }}; int main() { Derived d; FriendFunc::func(d); // 输出:Derived::func() return 0;} 通过以上方法,我们可以解决C++多重继承引发的重复调用问题。希望本文对您有所帮助。 本文实例讲述了C++多重继承引发的重复调用问题与解决方法。分享给大家供大家参考,具体如下: 前面简单介绍了一个C++多重继承功能示例,这里再来分析一个多重继承引发的重复调用问题,先来看看问题代码:
#include "stdafx.h"
#include<stdlib.h>
#include<iostream>
using namespace std;
class R//祖先类
{
private:
int r;
public:
R(int x = 0):r(x){}
void f()
{
cout << " r = " << r << endl;
}
void print()
{
cout << "print R = " << r << endl;
}
};
//虚继承
class A : virtual public R
{
private:
int a;
public:
A(int x,int y):R(x),a(y){}
//重写父类的f()函数
void f()
{
cout << "a = " << a << endl;
R::f();//r是私有成员变量,不能直接访问,通过作用域进行访问被派生类覆盖的函数f()
}
};
//虚继承
class B : virtual public R
{
private:
int b;
public:
B(int x, int y) :R(x), b(y) {}
//重写父类的f()函数
void f()
{
cout << "b = " << b << endl;
R::f();//r是私有成员变量,不能直接访问,通过作用域进行访问被派生类覆盖的函数f()
}
};
class C :public A, public B
{
private:
int c;
public:
C(int x,int y,int z,int m):R(x),A(x,y),B(x,z),c(m)
{ }
void f()
{
cout << "c = " << c << endl;
A::f();//此时A里面有一个 r 的输出,和输出a
B::f();//B里面也有一个r的输出,和输出b
//从而导致重复调用,两次输出 r
}
};
int main()
{
C cc(1212, 345, 123, 45);
cc.f();
system("pause");
return 0;
}
解决办法:针对重复调用,每个类把属于自己的工作单独封装 修改后的代码如下:
#include "stdafx.h"
#include<stdlib.h>
#include<iostream>
using namespace std;
class R//祖先类
{
private:
int r;
public:
R(int x = 0):r(x){}
void f()
{ cout << " r = " << r << endl; }
virtual void print()
{ cout << "print R = " << r << endl;}
};
//虚继承
class A : virtual public R//virtual写在public的前后均可以
{
private:
int a;
public:
A(int x,int y):R(x),a(y){ }
protected:
void fA()//增加一个保护函数,只打印自己的扩展成员
{
cout << "a = " << a << endl;
}
void f()//重写父类的f()函数
{
//cout << "a = " << a << endl;
fA();
R::f();//r是私有成员变量,不能直接访问,通过作用域进行访问被派生类覆盖的函数f()
}
};
//虚继承
class B : virtual public R
{
private:
int b;
public:
B(int x, int y) :R(x), b(y) {}
protected:
void fB()//增加一个保护函数,只打印自己的扩展成员
{
cout << "b = " << b << endl;
}
void f()//重写父类的f()函数
{
fB();
R::f();//r是私有成员变量,不能直接访问,通过作用域进行访问被派生类覆盖的函数f()
}
};
class C :public A, public B
{
private:
int c;
public:
C(int x,int y,int z,int m):R(x),A(x,y),B(x,z),c(m)
{ }
void f()
{
cout << "c = " << c << endl;
R::f();
//A::f();//此时A里面有一个 r 的输出,和输出a
//B::f();//B里面也有一个r的输出,和输出b
//从而导致重复调用,两次输出 r
fA();//A::fA();
fB();//A::fB();
}
};
int main()
{
C cc(1212, 345, 123, 45);
cc.f();
system("pause");
return 0;
}
希望本文所述对大家C++程序设计有所帮助。
本文共计1380个文字,预计阅读时间需要6分钟。
本文实例讲述了C++多重继承引发的重复调用问题及其解决方法。分享给广大读者,供大家参考,具体如下:
C++多重继承引发的重复调用问题分析
首先,通过一个简单的C++多重继承功能示例,介绍如何引发重复调用问题。
示例代码:
cppclass Base1 {public: void func() { cout << Base1::func() < class Base2 {public: void func() { cout << Base2::func() < class Derived : public Base1, public Base2 {}; int main() { Derived d; d.func(); // 输出:Base1::func() return 0;} 在这个例子中,`Derived` 类继承自 `Base1` 和 `Base2`,而这两个基类都定义了 `func()` 函数。当调用 `d.func()` 时,程序会输出 `Base1::func()`,这表明 `func()` 函数被重复调用。 解决方法 为了避免重复调用问题,我们可以采用以下几种方法: 1. 虚继承:使用虚继承可以避免重复调用,因为虚继承会创建一个共享的基类实例。 示例代码: cppclass Base1 {public: void func() { cout << Base1::func() < class Base2 : virtual public Base1 {public: void func() { cout << Base2::func() < class Derived : public Base1, public Base2 {}; int main() { Derived d; d.func(); // 输出:Base2::func() return 0;} 2. 重载函数:通过重载函数名称,可以避免重复调用。 示例代码: cppclass Base1 {public: void func() { cout << Base1::func() < class Base2 {public: void func() { cout << Base2::func() < class Derived : public Base1, public Base2 {public: void func() { cout << Derived::func() < int main() { Derived d; d.func(); // 输出:Derived::func() return 0;} 3. 使用友元函数:将函数定义为基类的友元函数,可以避免重复调用。 示例代码: cppclass Base1 {public: void func() { cout << Base1::func() < class Base2 {public: void func() { cout << Base2::func() < class Derived : public Base1, public Base2 {public: void func() { cout << Derived::func() < class FriendFunc {public: static void func(Derived& d) { d.func(); }}; int main() { Derived d; FriendFunc::func(d); // 输出:Derived::func() return 0;} 通过以上方法,我们可以解决C++多重继承引发的重复调用问题。希望本文对您有所帮助。 本文实例讲述了C++多重继承引发的重复调用问题与解决方法。分享给大家供大家参考,具体如下: 前面简单介绍了一个C++多重继承功能示例,这里再来分析一个多重继承引发的重复调用问题,先来看看问题代码:
#include "stdafx.h"
#include<stdlib.h>
#include<iostream>
using namespace std;
class R//祖先类
{
private:
int r;
public:
R(int x = 0):r(x){}
void f()
{
cout << " r = " << r << endl;
}
void print()
{
cout << "print R = " << r << endl;
}
};
//虚继承
class A : virtual public R
{
private:
int a;
public:
A(int x,int y):R(x),a(y){}
//重写父类的f()函数
void f()
{
cout << "a = " << a << endl;
R::f();//r是私有成员变量,不能直接访问,通过作用域进行访问被派生类覆盖的函数f()
}
};
//虚继承
class B : virtual public R
{
private:
int b;
public:
B(int x, int y) :R(x), b(y) {}
//重写父类的f()函数
void f()
{
cout << "b = " << b << endl;
R::f();//r是私有成员变量,不能直接访问,通过作用域进行访问被派生类覆盖的函数f()
}
};
class C :public A, public B
{
private:
int c;
public:
C(int x,int y,int z,int m):R(x),A(x,y),B(x,z),c(m)
{ }
void f()
{
cout << "c = " << c << endl;
A::f();//此时A里面有一个 r 的输出,和输出a
B::f();//B里面也有一个r的输出,和输出b
//从而导致重复调用,两次输出 r
}
};
int main()
{
C cc(1212, 345, 123, 45);
cc.f();
system("pause");
return 0;
}
解决办法:针对重复调用,每个类把属于自己的工作单独封装 修改后的代码如下:
#include "stdafx.h"
#include<stdlib.h>
#include<iostream>
using namespace std;
class R//祖先类
{
private:
int r;
public:
R(int x = 0):r(x){}
void f()
{ cout << " r = " << r << endl; }
virtual void print()
{ cout << "print R = " << r << endl;}
};
//虚继承
class A : virtual public R//virtual写在public的前后均可以
{
private:
int a;
public:
A(int x,int y):R(x),a(y){ }
protected:
void fA()//增加一个保护函数,只打印自己的扩展成员
{
cout << "a = " << a << endl;
}
void f()//重写父类的f()函数
{
//cout << "a = " << a << endl;
fA();
R::f();//r是私有成员变量,不能直接访问,通过作用域进行访问被派生类覆盖的函数f()
}
};
//虚继承
class B : virtual public R
{
private:
int b;
public:
B(int x, int y) :R(x), b(y) {}
protected:
void fB()//增加一个保护函数,只打印自己的扩展成员
{
cout << "b = " << b << endl;
}
void f()//重写父类的f()函数
{
fB();
R::f();//r是私有成员变量,不能直接访问,通过作用域进行访问被派生类覆盖的函数f()
}
};
class C :public A, public B
{
private:
int c;
public:
C(int x,int y,int z,int m):R(x),A(x,y),B(x,z),c(m)
{ }
void f()
{
cout << "c = " << c << endl;
R::f();
//A::f();//此时A里面有一个 r 的输出,和输出a
//B::f();//B里面也有一个r的输出,和输出b
//从而导致重复调用,两次输出 r
fA();//A::fA();
fB();//A::fB();
}
};
int main()
{
C cc(1212, 345, 123, 45);
cc.f();
system("pause");
return 0;
}
希望本文所述对大家C++程序设计有所帮助。

