关于动态绑定的问题 [英] questions about dynamic binding
问题描述
您好,
我对动态绑定有一些疑问。示例程序
是:
#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.com>
??????:11 ******************** @ u30g2000hsc。 googlegr oups.com ...
您好,
我对动态绑定有一些疑问。示例程序
是:
#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 ... @ msn.comwrote:
关于声明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
你好,
我有一些问题要问做动态绑定。示例程序
是:
#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 compiler
find 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.com>
??????:11********************@u30g2000hsc.googlegr oups.com...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...@msn.comwrote: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
这篇关于关于动态绑定的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!