可以在基类构造函数中调用纯虚函数吗? [英] Can pure virtual function be called in base class constructor?

查看:281
本文介绍了可以在基类构造函数中调用纯虚函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在基函数构造函数中调用纯函数。它产生了运行时错误:
运行时错误:纯虚方法调用。


我的问题是类A有一些派生类。我希望A'的

构造函数将其行为会计更改为派生类。


我试图使A :: fun()不是纯虚拟但是虚拟。它不会产生任何错误。但A :: fun()在A'的构造中被调用,而我
想要调用B :: fun()。我只是不想定义一个默认的虚拟

函数。


我想知道是否有任何解决这个问题的方法。


祝福,



#include< iostream>


A级{

公开:

A(){fun();}

虚拟空虚fun()= 0;

};


B级:公开A {

public:

virtual void fun(){

std :: cout<< __PRETTY_FUNCTION__<< std :: endl;

}

};


int main(int argc,char * argv [])

{

B b;

}

解决方案

< blockquote> Pe*******@gmail.com 写道:

A在基函数构造函数中调用pure函数。它会生成一个运行时错误:纯虚方法叫做。

我的问题是A类有一些派生类。我希望A'的
构造函数将其行为会计更改为派生类。

我试图使A :: fun()不是纯虚拟而是虚拟。它不会产生任何错误。但A :: fun()在A'的构造中被调用,而
我想要调用B :: fun()。我只是不想定义一个默认的
虚函数。


您要完成的工作违背了一个非常基本的原则:

成员函数不应该被调用为其构造的对象

还没有完成。如果您试图从A :: A调用B :: fun,则B

对象尚未完成构建。对于这种特殊的行为而言,Java是非常糟糕的,这是一个常常导致问题的原因。
我想知道是否有任何解决这个问题的工作。




解决方法是单独的(可能是虚拟的)初始化。会员

函数,在您创建对象后,将由您的A或B类的_user_调用,

。如果你想要在没有客户知道的情况下执行该序列(构造,

然后初始化),

你需要一个工厂(或几个)。 />

V


Pe ** *****@gmail.com 写道:



在基函数构造函数中调用纯函数。它会生成一个运行时错误:纯虚方法叫做。

我的问题是A类有一些派生类。我希望A'的
构造函数将其行为会计更改为派生类。

我试图使A :: fun()不是纯虚拟而是虚拟。它不会产生任何错误。但A :: fun()在A'的构造中被调用,而
我想要调用B :: fun()。我只是不想定义一个默认的
虚函数。

我想知道是否有任何解决这个问题的工作。




假设您可能构建任何派生类并且您想要

来调用派生类最多的乐趣(),并且只需要一次,并且您想要从构造函数调用

,下面说明了一个(不完全是

令人满意)的解决方法:


class A
{

public:

A(int){/ * stuff * /}

virtual void fun()= 0 ;

};


B级:公共A

{

public:

B(){fun(); / *其他东西* /}

void fun();

protected:

B(int){/ *其他东西* /}

};


C级:公共B

{

public:

C():B(int){fun(); / *其他东西* /}

void fun();

受保护:

C(int):B (int){/ *其他东西* /}

};


D类:公共C

{

public:

D():C(int){fun(); / *其他东西* /}

void fun();

受保护:

D(int):C(int){/ *其他东西* /}

};

依旧......


DW


David White写道:


A级
{
公开:
A(int){/ * stuff * /}
virtual void fun()= 0;
} ;




[snip]


对不起,匆匆一下。请试试这个:


A级

{

public:

A(){/ * stuff * /}

virtual void fun()= 0;

};


B级:公共A

{

public:

B(){fun(); / *其他东西* /}

虚无趣();

受保护:

B(int){/ *其他东西* /}

};

class C:public B

{

public:

C():B(0){fun(); / *其他东西* /}

void fun();

受保护:

C(int):B(0){/ *其他的东西* /}

};


D级:公共C

{

public :

D():C(0){fun(); / *其他东西* /}

void fun();

受保护:

D(int):C(0){/ *其他东西* /}

};


和等...


DW


Hi,

A pure function is called in the base function constructor. It generate
a run time error: "pure virtual method called".

My problem is that class A have some derived classes. I want A''s
constructor change its behaviour accounting to the derived class.

I tried to make A::fun() not pure virtual but virtual. It doesn''t
generate any error. But A::fun() is called in A''s construction, while I
want B::fun() be called. I just don''t want to define a default virtual
function.

I''m wondering if there is any work around to solve this problem.

Best wishes,
Peng
#include <iostream>

class A{
public:
A(){ fun();}
virtual void fun() = 0;
};

class B : public A{
public:
virtual void fun(){
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};

int main(int argc, char *argv[])
{
B b;
}

解决方案

Pe*******@gmail.com wrote:

A pure function is called in the base function constructor. It
generate a run time error: "pure virtual method called".

My problem is that class A have some derived classes. I want A''s
constructor change its behaviour accounting to the derived class.

I tried to make A::fun() not pure virtual but virtual. It doesn''t
generate any error. But A::fun() is called in A''s construction, while
I want B::fun() be called. I just don''t want to define a default
virtual function.
What you''re trying to accomplish goes against a very basic principle:
member functions shall not be called for an object whose construction
hasn''t completed. If you''re trying to call B::fun from A::A, the B
object hasn''t finished constructing. Java is notoriously bad about
this particular behaviour and it is a constant cause of trouble.
I''m wondering if there is any work around to solve this problem.



A work-around is a separate (maybe virtual) "initialise" member
function, which will be called by the _user_ of your A or B class,
after creating the object. If you want that sequence (construct,
then initialise) to be executed without client''s knowing about it,
you need a factory (or several).

V


Pe*******@gmail.com wrote:

Hi,

A pure function is called in the base function constructor. It
generate a run time error: "pure virtual method called".

My problem is that class A have some derived classes. I want A''s
constructor change its behaviour accounting to the derived class.

I tried to make A::fun() not pure virtual but virtual. It doesn''t
generate any error. But A::fun() is called in A''s construction, while
I want B::fun() be called. I just don''t want to define a default
virtual function.

I''m wondering if there is any work around to solve this problem.



Assuming that you might be constructing any derived class and that you want
to call the most derived class''s fun(), and only once, and you want to call
it from a constructor, the following illustrates a (not entirely
satisfactory) workaround:

class A
{
public:
A(int) { /* stuff */ }
virtual void fun() = 0;
};

class B : public A
{
public:
B() { fun();/* other stuff */ }
void fun();
protected:
B(int) { /* other stuff */ }
};

class C : public B
{
public:
C() : B(int) { fun();/* other stuff */ }
void fun();
protected:
C(int) : B(int) { /* other stuff */ }
};

class D : public C
{
public:
D() : C(int) { fun();/* other stuff */ }
void fun();
protected:
D(int) : C(int) { /* other stuff */ }
};

And so on...

DW


David White wrote:


class A
{
public:
A(int) { /* stuff */ }
virtual void fun() = 0;
};



[snip]

Sorry, rushed that a bit. Try this instead:

class A
{
public:
A() { /* stuff */ }
virtual void fun() = 0;
};

class B : public A
{
public:
B() { fun();/* other stuff */ }
void fun();
protected:
B(int) { /* other stuff */ }
};

class C : public B
{
public:
C() : B(0) { fun();/* other stuff */ }
void fun();
protected:
C(int) : B(0) { /* other stuff */ }
};

class D : public C
{
public:
D() : C(0) { fun();/* other stuff */ }
void fun();
protected:
D(int) : C(0) { /* other stuff */ }
};

And so on...

DW


这篇关于可以在基类构造函数中调用纯虚函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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