关于动态绑定的问题

编程入门 行业动态 更新时间:2024-10-27 20:27:16
本文介绍了关于动态绑定的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好, 我对动态绑定有一些疑问。示例程序 是: #include< iostream> 使用命名空间std; A级{ public: virtual void f(){cout<< " A :: f()的" <<结束;} }; B级:公开A { public: void f(){cout<< " B :: f()的" << endl;} }; int main(){ B b; A * bp =& b; // A * bp2 =新B(* bp); A * bp2 =新A(* bp); bp2-> f(); bp-> f(); 返回0; } 正如我预期的那样bp-> f()叫B'的f()。然而,bp2-> f()叫A'' f()。是因为新A,它只复制了A'的部分 " * bp"对象,还是因为合成的A / b $ b复制构造函数的行为?如果是后者,那么我可以通过为A定义自己的复制构造函数来创建一个B 对象吗? 此外,被注释的语句out产生的编译器 错误。它说没有匹配函数来调用''B :: B(A&)''。不能 编译器发现* bp实际上是B对象吗? 谢谢, Jess

解决方案

// bp指向对象b,即使它是A类的指针。 A * bp =& b; //所以应该调用成员函数B :: f。 bp-> f(); // bp2指向A类的对象。 A * bp2 =新A(* bp); //所以应该调用成员函数A :: f。 bp2-> f(); 关于语句A * bp2 = new A(* bp); 如果A类没有任何复制构造函数,编译器将使用默认的 复制构造函数来构造一个新对象。 A :: A(const A& a), 因为B类派生自A类,所以编译器将使用A'的部分 对象B构造类A的对象。 如果为A类编写重载复制构造函数,su ch as A :: A(const B& b), 编译器将使用你的自定义拷贝构造函数来创建 类A的对象。

>>它表示没有匹配函数来调用''B :: B(A&)''。难道编译器找不到* bp实际上是B对象吗?

是的,在编译时报告错误,动态绑定在运行时 时间。因此编译器无法知道指针是否在编译时指向其实际的 对象。 Jess < wd *** @ hotmail> ??????:11 ******************** @ u30g2000hsc。 googlegr oups ...

您好, 我对动态绑定有一些疑问。示例程序 是: #include< iostream> 使用命名空间std; A级{ public: virtual void f(){cout<< " A :: f()的" <<结束;} }; B级:公开A { public: void f(){cout<< " B :: f()的" << endl;} }; int main(){ B b; A * bp =& b; // A * bp2 =新B(* bp); A * bp2 =新A(* bp); bp2-> f(); bp-> f(); 返回0; } 正如我预期的那样bp-> f()叫B'的f()。然而,bp2-> f()叫A'' f()。是因为新A,它只复制了A'的部分 " * bp"对象,还是因为合成的A / b $ b复制构造函数的行为?如果是后者,那么我可以通过为A定义自己的复制构造函数来创建一个B 对象吗? 此外,被注释的语句out产生的编译器 错误。它说没有匹配函数来调用''B :: B(A&)''。不能 编译器发现* bp实际上是B对象吗? 谢谢, Jess

5月12日晚上8:33,Paolo Maldini < jifang1 ... @ msnwrote:

关于声明A * bp2 = new A(* bp); if A类没有任何复制构造函数,编译器将使用默认的 复制构造函数来构造一个新对象。 A :: A(const A& a) , 因为B类派生自A类,所以编译器将使用对象B的A'部分来构造A类对象。

那么,这是编译器的默认行为吗?

如果为A类编写重载复制构造函数,比如 A :: A(const B& b), 编译器将使用你的自定义拷贝构造函数来创建 的对象A类

有没有办法可以重载复制构造函数,以便创建的 对象实际上是B类? 谢谢, Jess

在2007-05-12 12:13,Jess写道:

你好, 我有一些问题要问做动态绑定。示例程序 是: #include< iostream> 使用命名空间std; A级{ public: virtual void f(){cout<< " A :: f()的" <<结束;} }; B级:公开A { public: void f(){cout<< " B :: f()的" << endl;} }; int main(){ B b; A * bp =& b; // A * bp2 =新B(* bp); A * bp2 =新A(* bp); bp2-> f(); bp-> f(); 返回0; } 正如我预期的那样bp-> f()叫B'的f()。然而,bp2-> f()叫A'' f()。是因为新A,它只复制了A'的部分 " * bp"对象,还是因为合成的A / b $ b复制构造函数的行为?

这是因为新的A()会创建一个A类型的新对象,所以在做的时候我会做什么? bp2-> f ()它将调用bp2指向的对象的f()方法 (恰好是A类型)。当你执行bp-> f()时,指向的 对象的类型是B类,因此调用它的f()。

如果是后者,那么我可以通过为A定义自己的 复制构造函数来创建B对象吗?

否,类型为T的对象中的任何构造函数(无论是否为正常的构造函数或复制构造函数)创建一个 类型为T的对象,所以当你执行'new A()''时,对象的类型总是A (因为那就是你指定)。

另外,注释掉的语句产生编译器 错误。它说没有匹配函数来调用''B :: B(A&)''。不能 编译器发现* bp实际上是B对象吗?

不,bp是指向A类对象的指针,你可以将 指针强制转换为B类型的指针或者在B中创建一个构造函数,它将 作为参数。 - Erik Wikstr?m

Hello, I have some questions to do with dynamic binding. The example program is: #include<iostream> using namespace std; class A{ public: virtual void f(){cout << "A::f()" << endl;} }; class B:public A{ public: void f(){cout << "B::f()" << endl;} }; int main(){ B b; A* bp = &b; // A* bp2 = new B(*bp); A* bp2 = new A(*bp); bp2->f(); bp->f(); return 0; } As I expected "bp->f()" calls B''s f(). However, "bp2->f()" calls A''s f(). Is it because of the "new A", which only copies the A''s part of "*bp" object, or is it because of the behaviour of the synthesized copy constructor of A? If it is the latter, then can I create a B object by defining my own copy constructor for A? In addition, the statement that is commented out produced compiler error. It says "no matching function for call to ''B::B(A&)''". Can''t compiler find out *bp is in fact a B object? Thanks, Jess

解决方案

// bp points to the object b, even if it is a pointer of class A. A* bp = &b; // so the member function B::f should be invoked. bp->f(); // bp2 points to a object of class A. A* bp2 = new A(*bp); // so the member function A::f should be invoked. bp2->f(); about the statement A* bp2 = new A(*bp); if class A hasn''t any copy constructor, the compiler will use the default copy constructor to construct a new object. A::A( const A& a), because class B derives from class A, so the compiler will use A''s part of the object B to construct the object of class A. if you write a overload copy constructor for class A, such as A::A( const B& b), the compiler will use your custom copy constructor to create the object of class A.

>>It says "no matching function for call to ''B::B(A&)''". Can''t compilerfind out *bp is in fact a B object?

yes, the error is reported at compile time, the dynamic binding is at run time. so the compiler could not know whether a pointer points to its actual object at compile time. "Jess" <wd***@hotmail> ??????:11********************@u30g2000hsc.googlegr oups...

Hello, I have some questions to do with dynamic binding. The example program is: #include<iostream> using namespace std; class A{ public: virtual void f(){cout << "A::f()" << endl;} }; class B:public A{ public: void f(){cout << "B::f()" << endl;} }; int main(){ B b; A* bp = &b; // A* bp2 = new B(*bp); A* bp2 = new A(*bp); bp2->f(); bp->f(); return 0; } As I expected "bp->f()" calls B''s f(). However, "bp2->f()" calls A''s f(). Is it because of the "new A", which only copies the A''s part of "*bp" object, or is it because of the behaviour of the synthesized copy constructor of A? If it is the latter, then can I create a B object by defining my own copy constructor for A? In addition, the statement that is commented out produced compiler error. It says "no matching function for call to ''B::B(A&)''". Can''t compiler find out *bp is in fact a B object? Thanks, Jess

On May 12, 8:33 pm, "Paolo Maldini" <jifang1...@msnwrote:

about the statement A* bp2 = new A(*bp); if class A hasn''t any copy constructor, the compiler will use the default copy constructor to construct a new object. A::A( const A& a), because class B derives from class A, so the compiler will use A''s part of the object B to construct the object of class A.

So, this is the default behaviour of compiler?

if you write a overload copy constructor for class A, such as A::A( const B& b), the compiler will use your custom copy constructor to create the object of class A.

Is there a way that I can overload the copy constructor so that the object created is in fact of class B? Thanks, Jess

On 2007-05-12 12:13, Jess wrote:

Hello, I have some questions to do with dynamic binding. The example program is: #include<iostream> using namespace std; class A{ public: virtual void f(){cout << "A::f()" << endl;} }; class B:public A{ public: void f(){cout << "B::f()" << endl;} }; int main(){ B b; A* bp = &b; // A* bp2 = new B(*bp); A* bp2 = new A(*bp); bp2->f(); bp->f(); return 0; } As I expected "bp->f()" calls B''s f(). However, "bp2->f()" calls A''s f(). Is it because of the "new A", which only copies the A''s part of "*bp" object, or is it because of the behaviour of the synthesized copy constructor of A?

It''s because new A() will create a new object of type A, so when doing bp2->f() it will call the f() method of the object pointed to by bp2 (which happens to be of type A). When you do bp->f() the type of the object pointed to is of type B so its f() is called.

If it is the latter, then can I create a B object by defining my own copy constructor for A?

No, any constructor in an object (regardless if it''s a normal constructor or a copy-constructor) of type T will create an object of type T, so when you do ''new A()'' the type of the object will always be A (since that is what you specified).

In addition, the statement that is commented out produced compiler error. It says "no matching function for call to ''B::B(A&)''". Can''t compiler find out *bp is in fact a B object?

No, bp is a pointer to an object of type A, you can either cast the pointer to a pointer of type B or create a constructor in B which takes an A as argument. -- Erik Wikstr?m

更多推荐

关于动态绑定的问题

本文发布于:2023-10-18 03:28:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502949.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!