“公共虚拟”应该永远变成“私人虚拟”吗? &安培;使用私有继承 [英] Should 'public virtual' always become 'private virtual'? & using private inheritance

查看:63
本文介绍了“公共虚拟”应该永远变成“私人虚拟”吗? &安培;使用私有继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

班级基础

{

//其他会员

公开:

virtual~base()

{

}

virtual void virtualMethod1()= 0;

virtual void virtualMethod2()= 0;

虚拟void virtualMethod3()= 0;

};

我从base派生一个类。我想将所有虚拟功能

设为私有。

等级派生:公共基础

{

/ /其他会员

公开:

虚拟〜派生()

{

}


void myPublicInterface1();

void myPublicInterface1();


private:

virtual void virtualMethod1( )= 0;

virtual void virtualMethod2()= 0;

virtual void virtualMethod3()= 0;

};

我做了上面考虑的事实

- 无论如何,虚函数总是会通过

来调用基类指针

- 公共虚拟接口的数量应该减少多少

可能


以下是我的问题:

1)

我可以认为这是一个好的设计吗?如果是/否,为什么?


2)

怎么样

派生类:私人基地

$

}

而不是上面的设计,无论如何,所有继承的虚拟

函数都需要制作私有吗?


这是正确的设计吗?如果是/否,为什么?

3)

上述设计中存在哪些缺陷?您对

的改进有何建议?

class base
{
// other members
public:
virtual ~base()
{
}
virtual void virtualMethod1()=0 ;
virtual void virtualMethod2()=0 ;
virtual void virtualMethod3()=0 ;
} ;
I derive a class from base. I want to make all the virtual functions
as private.
class derived : public base
{
// other members
public:
virtual ~derived()
{
}

void myPublicInterface1() ;
void myPublicInterface1() ;

private:
virtual void virtualMethod1()=0 ;
virtual void virtualMethod2()=0 ;
virtual void virtualMethod3()=0 ;
} ;
I did the above considering the facts that
- in any case, the virtual functions will always be called through
the base class pointer only
- No. of public virtual interfaces should be reduced as much as
possible

Here are my questions:
1)
Can I consider this as a good design? If yes/no, why?

2)
How about
class derived : private base
{

}
instead of the above design, as anyway, all the inherited virtual
functions need to be made as private ?

Is this a correct design? If yes/no, why?
3)
What are the flaws in the above design and what do you suggest for
improvements?

推荐答案



" qazmlp" < QA ******** @ rediffmail.com> skrev i en meddelelse

新闻:db ************************** @ posting.google.c om .. 。

"qazmlp" <qa********@rediffmail.com> skrev i en meddelelse
news:db**************************@posting.google.c om...
班级基地
{
//其他成员
公开:
虚拟〜基地()

}
virtual void virtualMethod1()= 0;
virtual void virtualMethod2()= 0;
virtual void virtualMethod3()= 0;
};

我从基础派生一个类。我想把所有虚拟功能都设为私有。
类派生:公共基地
//其他成员
公开:
虚拟〜派生()


void myPublicInterface1();
void myPublicInterface1();

私有:
虚拟空白virtualMethod1()= 0;
virtual void virtualMethod2()= 0;
virtual void virtualMethod3()= 0;
};


这绝对没有价值。无论如何,这些功能都是可以访问的,你知道。


我做了以上考虑的事实
- 无论如何,虚函数将会总是只通过基类指针调用
- 公共虚拟接口的数量应该减少多少


以下是我的问题:
1)
我可以认为这是一个好的设计吗?如果是/否,为什么?


否 - 因为你没有减少公共虚拟功能的数量。
2)
如何派生类:私人基地功能都需要被设为私有?

是这是一个正确的设计?如果是/否,为什么?


这不是更好的基础* b =新派生的(...)将不再编译。


3)
上述设计有哪些缺陷,你对
改进有什么建议?
class base
{
// other members
public:
virtual ~base()
{
}
virtual void virtualMethod1()=0 ;
virtual void virtualMethod2()=0 ;
virtual void virtualMethod3()=0 ;
} ;
I derive a class from base. I want to make all the virtual functions
as private.
class derived : public base
{
// other members
public:
virtual ~derived()
{
}

void myPublicInterface1() ;
void myPublicInterface1() ;

private:
virtual void virtualMethod1()=0 ;
virtual void virtualMethod2()=0 ;
virtual void virtualMethod3()=0 ;
} ;
This has absolutely no value. Those functions are accesible anyway, you
know.


I did the above considering the facts that
- in any case, the virtual functions will always be called through
the base class pointer only
- No. of public virtual interfaces should be reduced as much as
possible

Here are my questions:
1)
Can I consider this as a good design? If yes/no, why?
No - because you do not reduce the number of public, virtual functions.
2)
How about
class derived : private base
{

}
instead of the above design, as anyway, all the inherited virtual
functions need to be made as private ?

Is this a correct design? If yes/no, why?
This is not any better as base *b = new derived(...) will no longer compile.


3)
What are the flaws in the above design and what do you suggest for
improvements?




使用这个方案代替:

类基地

{

//其他成员

虚拟空虚virtualMethod1()= 0;

虚拟空virtualMethod2()= 0;

virtual void virtualMethod3()= 0;

public:

virtual~base()

{

}

void method1(){virtualMethod1();}

void method2(){virtualMethod2();}

void method3(){virtualMethod3();}


};



Use this scheme instead:
class base
{
// other members
virtual void virtualMethod1()=0 ;
virtual void virtualMethod2()=0 ;
virtual void virtualMethod3()=0 ;
public:
virtual ~base()
{
}
void method1() { virtualMethod1();}
void method2() { virtualMethod2();}
void method3() { virtualMethod3();}

};




" qazmlp" < QA ******** @ rediffmail.com>在留言中写道

news:db ************************** @ posting.google.c om ...

"qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c om...
班级基地
{
//其他成员
公众:
虚拟〜基地()
{
}
virtual void virtualMethod1()= 0;
virtual void virtualMethod2()= 0;
virtual void virtualMethod3()= 0;
};

我派生基地的一个班级。我想把所有虚拟功能都设为私有。
类派生:公共基地
//其他成员
公开:
虚拟〜派生()


void myPublicInterface1();
void myPublicInterface1();

私有:
虚拟空白virtualMethod1()= 0;
virtual void virtualMethod2()= 0;
virtual void virtualMethod3()= 0;
};

我做了以上考虑
- 无论如何,虚函数总是只通过基类指针调用
- 公共虚拟接口的数量应尽可能减少


以下是我的问题:
1)
我可以认为这是一个好的设计吗?如果是/否,为什么?


因为公共继承应该意味着IS-A意味着

你必须至少提供尽可能多的基础课。

2)

类来源:私人基地
{

}
而不是以上设计,无论如何,所有继承的虚拟功能都需要设为私有?

这是一个正确的设计吗?如果是/否,为什么?


取决于 - 他们不可能都是正确的,因为要么是衍生的IS-A基础还是没有



取决于派生的和实际的基数。

3)
上述设计有哪些缺陷,你对
的改进有什么建议?
class base
{
// other members
public:
virtual ~base()
{
}
virtual void virtualMethod1()=0 ;
virtual void virtualMethod2()=0 ;
virtual void virtualMethod3()=0 ;
} ;
I derive a class from base. I want to make all the virtual functions
as private.
class derived : public base
{
// other members
public:
virtual ~derived()
{
}

void myPublicInterface1() ;
void myPublicInterface1() ;

private:
virtual void virtualMethod1()=0 ;
virtual void virtualMethod2()=0 ;
virtual void virtualMethod3()=0 ;
} ;
I did the above considering the facts that
- in any case, the virtual functions will always be called through
the base class pointer only
- No. of public virtual interfaces should be reduced as much as
possible

Here are my questions:
1)
Can I consider this as a good design? If yes/no, why?

Because public inheritance should mean IS-A which implies that
you must provide at least as much if not more than the base class.
2)
How about
class derived : private base
{

}
instead of the above design, as anyway, all the inherited virtual
functions need to be made as private ?

Is this a correct design? If yes/no, why?

Depends - they can''t both be right because either a derived IS-A base or not
and
that depends on what derived and base actually are.

3)
What are the flaws in the above design and what do you suggest for
improvements?




它根本不是一个设计,因为它没有定义实际上是什么基础和衍生的实际价值。

直到你决定不能做出明智的决定如何实施它们。


你必须要告诉我们它们应该是什么或者重写你的

问题

纯粹的语言性质而不是设计。



It''s not a design at all because it doesn''t define what base and derived
actually are.
Until you decide that you cannot make sensible decisions about how to
implement them.

You must either tell us what they are supposed to be or rewrite your
question as
one of a purely language nature rather than design.


好奇。 .....降低公共虚拟方法的数量有什么好处?谢谢

" Peter Koch Larsen" < PK ***** @ mailme.dk>在消息中写道

新闻:HG ********************* @ news000.worldonline.d k ...
Just curious......what is the advantage of lowering the number of public
virtual methods? Thanks
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:HG*********************@news000.worldonline.d k...

" qazmlp" < QA ******** @ rediffmail.com> skrev i en meddelelse
新闻:db ************************** @ posting.google.c om ...

"qazmlp" <qa********@rediffmail.com> skrev i en meddelelse
news:db**************************@posting.google.c om...
班级基地
{
//其他成员
公开:
虚拟〜基地()

}
虚拟void virtualMethod1()= 0;
virtual void virtualMethod2()= 0;
virtual void virtualMethod3()= 0;
};

我从中派生出一个类基础。我想把所有虚拟功能都设为私有。
类派生:公共基地
//其他成员
公开:
虚拟〜派生()


void myPublicInterface1();
void myPublicInterface1();

私有:
虚拟空白virtualMethod1()= 0;
virtual void virtualMethod2()= 0;
virtual void virtualMethod3()= 0;
};
这绝对没有价值。无论如何,这些功能都是可以访问的,你知道。
class base
{
// other members
public:
virtual ~base()
{
}
virtual void virtualMethod1()=0 ;
virtual void virtualMethod2()=0 ;
virtual void virtualMethod3()=0 ;
} ;
I derive a class from base. I want to make all the virtual functions
as private.
class derived : public base
{
// other members
public:
virtual ~derived()
{
}

void myPublicInterface1() ;
void myPublicInterface1() ;

private:
virtual void virtualMethod1()=0 ;
virtual void virtualMethod2()=0 ;
virtual void virtualMethod3()=0 ;
} ;
This has absolutely no value. Those functions are accesible anyway, you
know.


我做了以上考虑事实
- 无论如何,虚函数总是只通过基类指针调用
- 公共虚拟接口的数量应该减少多少


这是我的问题:
1)
我可以认为这是一个好的设计吗?如果是/否,为什么?


I did the above considering the facts that
- in any case, the virtual functions will always be called through
the base class pointer only
- No. of public virtual interfaces should be reduced as much as
possible

Here are my questions:
1)
Can I consider this as a good design? If yes/no, why?



否 - 因为你没有减少公共虚拟功能的数量。



No - because you do not reduce the number of public, virtual functions.


2)
如何派生类:私人基地

而不是上面的设计,无论如何,所有继承的虚拟
功能都需要是私人的?

这是一个正确的设计吗?如果是/否,为什么?

2)
How about
class derived : private base
{

}
instead of the above design, as anyway, all the inherited virtual
functions need to be made as private ?

Is this a correct design? If yes/no, why?



这不是更好的基础* b =新派生(...)将不再



This is not any better as base *b = new derived(...) will no longer



compile。


compile.



3)
上述设计有哪些缺陷,你对
改进有什么建议?


3)
What are the flaws in the above design and what do you suggest for
improvements?



使用这个方案:
类基础
//
其他成员
virtual void virtualMethod1()= 0;
virtual void virtualMethod2 ()= 0;
virtual void virtualMethod3()= 0;
public:
virtual~base()


void method1() {virtualMethod1();}
void method2(){virtualMethod2();}
void method3(){virtualMethod3();}

};



Use this scheme instead:
class base
{
// other members
virtual void virtualMethod1()=0 ;
virtual void virtualMethod2()=0 ;
virtual void virtualMethod3()=0 ;
public:
virtual ~base()
{
}
void method1() { virtualMethod1();}
void method2() { virtualMethod2();}
void method3() { virtualMethod3();}

};



这篇关于“公共虚拟”应该永远变成“私人虚拟”吗? &安培;使用私有继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆