且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

私有纯虚函数

更新时间:2023-09-23 19:14:04

Martin Vorbrodt写道:
我的一本书中的示例代码引起了我的兴趣:

B类{
public:
B * Clone()const {
B * p = DoClone();
断言(typeid(* p)== typeid(* this));
返回p;
}

受保护:
B(const B&);

私有:
虚拟B * DoClone()const = 0;
};
这将强制所有派生类实现DoClone,否则基类中的Clone将在assert中失败。有人可以解释一下原因吗?


typeid为最派生的类返回''type_info''。如果''DoClone'确实没有'b $ b'创建一个与''这个'相同的类的实例''指向''克隆''

被调用,那么断言失败。

对于D或B类型的派生类D,是* this的typeid吗?


D&

我确定* p是什么类型的覆盖DoClone,但我不确定*这个在基类
类函数中(即使派生类型实际到位)...(我可能会因为整个不使用构造函数中的虚函数而感到困惑,和这个指针建议...)


断言是来自''*''和''* p''的最派生类

是一样的。

另外一件事。纯虚函数是私有的???我勒个去???它真的符合标准吗?


为什么不呢?

你可以在派生类中覆盖它吗(根据标准可移植)并且仍然在派生类中保持私有/>课程?这是否意味着函数的虚拟性无论什么都适用它的访问限定符是???




虚拟性和访问说明符是正交的。


V


" Martin Vorbrodt" < MV ******* @ gmail.com>在消息中写道

新闻:HM ****************** @ news-wrt-01.rdc-nyc.rr.com ...... 我的一本书中的示例代码引起了我的兴趣:

B类{
公开:
B * Clone()const {
B * p = DoClone();
断言(typeid(* p)== typeid(* this));
返回p;
}

受保护:
B(const B&);

私人:
虚拟B * DoClone()const = 0;
};

这将强制所有派生类实现DoClone,否则基类中的Clone将在assert中失败。有人可以解释一下原因吗?


克隆从DoClone接收B指针。所以DoClone应该让

成为自己的克隆。 Clone然后检查以确保创建的克隆是

与它运行的类的实例相同的类型。 I.E.如果DoClone

只返回新的B *,则断言将失败,因为DoClone仅在

derieved类上运行。如果DoClone确实返回新的D *,则断言不应该失败

(假设D是从B派生的)因为typeid'将匹配。

是*的类型对于某些D或B类派生类D?我确定* p是什么类型的覆盖DoClone,但我不确定*这在
基类函数中(即使派生类型实际到位)... 。
(我可能会对整个不使用
构造函数中的虚函数感到困惑,这个指针建议...)


这个指针将是类的实例的类型,无论

是什么。由于B是一个纯粹的虚拟类,它必须是一些被解除的东西。

另外一件事。纯虚函数是私有的???到底是什么???
它真的符合标准吗?你可以在派生类中覆盖它吗(根据标准可移植)并仍然在派生的
类中保持私有?是否意味着函数的虚拟性无论是什么都适用它的访问限定符是???




纯虚拟可以是公共的还是私有的(不确定受保护的)。公共或

私人真的无关紧要纯粹的虚拟我不相信。
相信。

>

Jim Langston写道:

纯虚拟可以是公共的或私有的(不确定受保护)。公共或
私有真的无关紧要纯虚拟我不相信。




私有纯虚拟功能没有多大意义,因为它们不能被覆盖。受保护的纯虚拟机可以。例如:


class Base {

public:

void SomeFunc()

{

DoSomethingVirtually();

}

受保护:

虚拟无效DoSomethingVirtually()= 0;

};


Example code in one of my books intrigues me:

class B {
public:
B* Clone() const {
B* p = DoClone();
assert(typeid(*p) == typeid(*this));
return p;
}

protected:
B(const B&);

private:
virtual B* DoClone() const = 0;
};

This will force all derived classes to implement DoClone, or else Clone in
the base class will fail in the assert. Could someone please explain why?

Is typeid of *this for some derived class D of type D or B? I''m sure *p is
of whatever type overrides DoClone, but i''m not sure about *this in the base
class function (even though a derived type is actually in place)... (i may
be getting confused by the whole don''t use virtual functions in
constructors, and this pointer advice...)

Also another thing. pure virtual function is private??? What the hell??? Is
it really standard compliant? Can you override it in derived classes
(portably according to the standard) and still keep it private in derived
classes? Does it mean that virtuality of a function applies no matter whta
it''s access qualifier is???

Thanks

Martin Vorbrodt wrote:
Example code in one of my books intrigues me:

class B {
public:
B* Clone() const {
B* p = DoClone();
assert(typeid(*p) == typeid(*this));
return p;
}

protected:
B(const B&);

private:
virtual B* DoClone() const = 0;
};

This will force all derived classes to implement DoClone, or else Clone in
the base class will fail in the assert. Could someone please explain why?
typeid returns ''type_info'' for the most derived class. If ''DoClone'' does
not create an instance of the same class as ''this'' points to when ''Clone''
is called, then the assertion fails.
Is typeid of *this for some derived class D of type D or B?
D&
I''m sure *p is
of whatever type overrides DoClone, but i''m not sure about *this in the base
class function (even though a derived type is actually in place)... (i may
be getting confused by the whole don''t use virtual functions in
constructors, and this pointer advice...)
The assertion is that the most derived classes from both ''*this'' and ''*p''
are the same.
Also another thing. pure virtual function is private??? What the hell??? Is
it really standard compliant?
Why not?
Can you override it in derived classes
(portably according to the standard) and still keep it private in derived
classes? Does it mean that virtuality of a function applies no matter whta
it''s access qualifier is???



Virtuality and access specifiers are orthogonal.

V


"Martin Vorbrodt" <mv*******@gmail.com> wrote in message
news:HM******************@news-wrt-01.rdc-nyc.rr.com...
Example code in one of my books intrigues me:

class B {
public:
B* Clone() const {
B* p = DoClone();
assert(typeid(*p) == typeid(*this));
return p;
}

protected:
B(const B&);

private:
virtual B* DoClone() const = 0;
};

This will force all derived classes to implement DoClone, or else Clone in
the base class will fail in the assert. Could someone please explain why?
Clone is receiving a B pointer from DoClone. So DoClone is supposed to make
a clone of itself. Clone then checks to make sure that the clone created is
the same type os the instance of the class it''s run on. I.E. If DoClone
simply does return new B* the assert will fail, since DoClone is only run on
derieved classes. If DoClone does return new D* the assert should not fail
(presuming D is the derived from B) since the typeid''s will match.
Is typeid of *this for some derived class D of type D or B? I''m sure *p is
of whatever type overrides DoClone, but i''m not sure about *this in the
base class function (even though a derived type is actually in place)...
(i may be getting confused by the whole don''t use virtual functions in
constructors, and this pointer advice...)
The this pointer will be a type of the instance of the class, whatever that
is. Since B is a pure virtual class, it would have to be some derieved.
Also another thing. pure virtual function is private??? What the hell???
Is it really standard compliant? Can you override it in derived classes
(portably according to the standard) and still keep it private in derived
classes? Does it mean that virtuality of a function applies no matter whta
it''s access qualifier is???



pure virtual can be public or private (not sure about protected). Public or
private really doesn''t matter as far as pure virtual is concerned I don''t
believe.


Jim Langston wrote:

pure virtual can be public or private (not sure about protected). Public or
private really doesn''t matter as far as pure virtual is concerned I don''t
believe.



Private pure virtual functions don''t make much sense, since they can''t
be overridden. Protected pure virtuals do. e.g.:

class Base {
public:
void SomeFunc()
{
DoSomethingVirtually();
}
protected:
virtual void DoSomethingVirtually() = 0;
};