派生类指向基类对象的指针 [英] derived class pointer to base class object
问题描述
大家好,
我只是玩虚拟功能,然后跟着
跟随,
class Base1
{
public:virtual void sample()
{
printf(" base :: sample \ n");
};
};
class Derived1:public Base1
{
public:void sample()
{
printf(" derived :: sample\\\
");
};
};
int main()
{
Derived1 * ptr = reinterpret_cast< Derived1 *(new Base1());
ptr-> sample();
return(0);
}
这会调用基本版本的sample(),但是如果我将样本声明为
a正常(非虚拟)函数,派生类的sample()是
调用。我在想怎么样?谁能帮到这方面呢?
提前致谢!!!
Hi Everyone,
I was just playing around virtual functions and landed up with the
following,
class Base1
{
public: virtual void sample()
{
printf("base::sample\n");
};
};
class Derived1: public Base1
{
public: void sample()
{
printf("derived::sample\n");
};
};
int main()
{
Derived1 *ptr = reinterpret_cast<Derived1 *(new Base1());
ptr->sample();
return(0);
}
this invokes the sample() of base version, but if i declare sample as
a ordinay (non-virtual) function, the sample() of derived class is
invoked. I''m wondering how? Can anyone help in this regard?
Thanks in advance!!!
推荐答案
Rahul写道:
Rahul wrote:
我只是在玩虚拟功能并且跟着
跟随,
类Base1
{
public:virtual void sample()
{
printf(" base :: sample \ n");
};
};
class Derived1:public Base1
{
public:void sample()
{
printf(" derived :: sample\\\
");
};
};
int main()
{
Derived1 * ptr = reinterpret_cast< Derived1 *(new Base1());
ptr-> sample();
return(0);
}
这个邀请es基础版本的sample(),但如果我将样本声明为
一个普通(非虚拟)函数,则派生类的sample()调用
。我在想怎么样?在这方面有人可以提供帮助吗?
I was just playing around virtual functions and landed up with the
following,
class Base1
{
public: virtual void sample()
{
printf("base::sample\n");
};
};
class Derived1: public Base1
{
public: void sample()
{
printf("derived::sample\n");
};
};
int main()
{
Derived1 *ptr = reinterpret_cast<Derived1 *(new Base1());
ptr->sample();
return(0);
}
this invokes the sample() of base version, but if i declare sample as
a ordinay (non-virtual) function, the sample() of derived class is
invoked. I''m wondering how? Can anyone help in this regard?
代码''ptr-> sample()''具有未定义的行为,因为从
a基类转换为派生类是不是什么''reinterpret_cast''用于。
'static_cast''或''dynamic_cast''用于此。
在你的情况下''dynamic_cast''应该返回NULL,因为对象是
不是类型''derived'',这应该表明''static_cast''不是
你是什么的需要。你只在这种情况下使用''static_cast''
如果你*确切地知道*指针是通过将一些派生类对象转换成基数而得到的。
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要问
The code ''ptr->sample()'' has undefined behaviour because casting from
a base class to a derived class is NOT what ''reinterpret_cast'' is for.
Either ''static_cast'' or ''dynamic_cast'' are used for that.
In your case ''dynamic_cast'' should return NULL since the object is
NOT of type ''derived'', which should suggest that ''static_cast'' is not
what you need either. You only use ''static_cast'' in this situation
if you *know exactly* that the pointer was obtained by converting
some derived class object into the base.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
On 2007-12-03 11:27:57 -0500," Victor巴札罗夫" < v。******** @ comAcast.netsaid:
On 2007-12-03 11:27:57 -0500, "Victor Bazarov" <v.********@comAcast.netsaid:
Rahul写道:
Rahul wrote:
>我只是在玩虚拟功能,然后跟着
下载,
类Base1
{
公共:虚拟空白样本()
{
printf(" base :: sample \ n");
};
};
类Derived1:public Base1
{
public:void sample()
{
printf(" derived :: sample \ n");
};
};
int main()
{Derfor1 * ptr = reinterpret_cast< Derived1 *(new Base1());
ptr-> sample();
return( 0);
}
这会调用基本版本的sample(),但是如果我将样本声明为一个普通(非虚拟)函数,那么样本()
调用派生类。我在想怎么样?在这方面有人可以提供帮助吗?
>I was just playing around virtual functions and landed up with the
following,
class Base1
{
public: virtual void sample()
{
printf("base::sample\n");
};
};
class Derived1: public Base1
{
public: void sample()
{
printf("derived::sample\n");
};
};
int main()
{
Derived1 *ptr = reinterpret_cast<Derived1 *(new Base1());
ptr->sample();
return(0);
}
this invokes the sample() of base version, but if i declare sample as
a ordinay (non-virtual) function, the sample() of derived class is
invoked. I''m wondering how? Can anyone help in this regard?
代码''ptr-> sample()''具有未定义的行为,因为从
a基类转换为派生类是不是什么''reinterpret_cast''用于。
'static_cast''或''dynamic_cast''用于此。
在你的情况下''dynamic_cast''应该返回NULL,因为对象是
不是类型''derived'',这应该表明''static_cast''不是
你是什么的需要。你只在这种情况下使用''static_cast''
如果你*确切地知道*指针是通过将一些派生类对象转换成基数而得到的。
The code ''ptr->sample()'' has undefined behaviour because casting from
a base class to a derived class is NOT what ''reinterpret_cast'' is for.
Either ''static_cast'' or ''dynamic_cast'' are used for that.
In your case ''dynamic_cast'' should return NULL since the object is
NOT of type ''derived'', which should suggest that ''static_cast'' is not
what you need either. You only use ''static_cast'' in this situation
if you *know exactly* that the pointer was obtained by converting
some derived class object into the base.
只是为了完成圆圈:用原始代码中的
static_cast替换reinterpret_cast仍会导致未定义的行为。
>
-
Pete
Roundhouse Consulting,Ltd。( www.versatilecoding.com )作者
标准C ++库扩展:教程和参考
( www.petebecker.com/tr1book )
And just to complete the circle: replacing reinterpret_cast with
static_cast in the original code still results in undefined behavior.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
12月3日晚9点27分,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
On Dec 3, 9:27 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Rahul写道:
Rahul wrote:
我是只是玩虚拟功能并下载
跟随,
I was just playing around virtual functions and landed up with the
following,
class Base1
{
public:virtual void sample()
{
printf(" base :: sample\\\
");
};
};
class Base1
{
public: virtual void sample()
{
printf("base::sample\n");
};
};
class Derived1:public Base1
{
public:void sample()
{
printf(" derived :: sample \ n");
};
};
class Derived1: public Base1
{
public: void sample()
{
printf("derived::sample\n");
};
};
int main()
{
Derived1 * ptr = reinterpret_cast< Derived1 *( new Base1());
ptr-> sample();
return(0);
}
int main()
{
Derived1 *ptr = reinterpret_cast<Derived1 *(new Base1());
ptr->sample();
return(0);
}
这会调用基本版本的sample(),但是如果我将样本声明为
a正常(非虚拟)函数,则示例()派生类的类别是
调用。我在想怎么样?在这方面有人可以提供帮助吗?
this invokes the sample() of base version, but if i declare sample as
a ordinay (non-virtual) function, the sample() of derived class is
invoked. I''m wondering how? Can anyone help in this regard?
代码''ptr-> sample()''具有未定义的行为,因为从
a基类转换为派生类是不是什么''reinterpret_cast''用于。
'static_cast''或''dynamic_cast''用于此。
在你的情况下''dynamic_cast''应该返回NULL,因为对象是
不是类型''derived'',这应该表明''static_cast''不是
你是什么的需要。你只在这种情况下使用''static_cast''
如果你*确切地知道*指针是通过将一些派生类对象转换成基数而得到的。
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要问
The code ''ptr->sample()'' has undefined behaviour because casting from
a base class to a derived class is NOT what ''reinterpret_cast'' is for.
Either ''static_cast'' or ''dynamic_cast'' are used for that.
In your case ''dynamic_cast'' should return NULL since the object is
NOT of type ''derived'', which should suggest that ''static_cast'' is not
what you need either. You only use ''static_cast'' in this situation
if you *know exactly* that the pointer was obtained by converting
some derived class object into the base.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
然后,你如何建议从Base类对象输入强制转换为
派生类指针?
Then, how do you suggest to type cast from Base class object to
derived class pointer?
这篇关于派生类指向基类对象的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!