试图理解从基础到派生的铸造。 [英] Trying to understand casting, from base to derived.

查看:57
本文介绍了试图理解从基础到派生的铸造。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如果我有两节课。


A级

{

public:

A(const char * sz);

~A();

virtual void Foo()= 0 ;

}


B级:公共A

{

公开:

B(const char * sz):A(sz){};

~B(){/ *清理* /};

虚拟void Foo();


int iSomeObject;

char * pSomePointer;

}


如果我这样做


A * test = new A();

//

//然后呢安全吗

//

((B *)测试) - > Foo();


//甚至

((B *)测试) - > iSomeObject = 10

//////////// //////////////////////////////////

我为什么要问?


,因为我的基类从文件中读取信息。

所以我更容易打个电话


A * test = new A(/ *文件名读取* /)


De等待文件中的实际数据,某个基类将使用

...


我可以做


B * test = new B(/ *文件名读取* /)


但正如我所说,我只知道在文件<之后要使用的派生类br />
读。

我想我可以做很长的路,(读取文件并依赖

类型调用相关的派生类),但这似乎不是很有效。


更糟糕的是,我想将所有指针保存在矢量中。


std :: vector< A *>


但是在向量中包含派生类指针是否安全?


非常感谢您的帮助/建议。


Simon

Hi,

If I have two classes.

Class A
{
public:
A( const char *sz);
~A();
virtual void Foo() = 0;
}

Class B : public A
{
public:
B(const char *sz) : A( sz ) {};
~B(){ /* do clean up */ };
virtual void Foo();

int iSomeObject;
char *pSomePointer;
}

if i do

A *test = new A();
//
// is it then safe to do
//

((B *)test)->Foo();

// or even
((B *)test)->iSomeObject = 10

//////////////////////////////////////////////
Why do I ask?

because my base class reads information from a file.
So it is easier for me to have one call

A * test = new A( /* file name to read */ )

Depending on the actual data in the file a certain base class will be
used...

I could do

B * test = new B( /* file name to read */ )

but as I said, I only know what derived class to use after the file has been
read.
I guess I could do it the long way around, (read the file and depending on
the type call the relevant derived class), but that does not seem very
efficient.

To make matters worse I want to save all the pointers in a vector.

std::vector< A* >

but is it safe to include derived classes pointer in a vector?

Many thanks in advance for your help/advices.

Simon

推荐答案

Simon写道:



如果我有两节课。

A班
{
公开:
A(const char * sz);
~A();
虚拟空虚Foo()= 0;
}

B类:公众A
{
公开:
B(const char * sz):A(sz){};
~B(){/ *做清理* /};
虚拟空虚Foo();

int iSomeObject;
char * pSomePointer;
}
如果我这样做

A * test = new A();
//
//然后安全吗
//

((B *)test) - > Foo();


编号测试指向A类物体,而不是B.

//甚至
((B *)测试) - > iSomeObject = 10


编号测试指向A类对象。这样的对象

没有成员iSomeObject。没有内存空间

是为这样的会员保留的,因为你的新请求是一个A对象,而不是B.

//// //////////////////////////////////////////
我为什么要问?

因为我的基类从文件中读取信息。
所以我更容易打一个电话

A * test = new A(/ *要读取的文件名* /)

取决于文件中的实际数据,将使用某个基类...

我可以做

B * test = new B(/ *文件名读取* /)

但正如我所说,我只知道在文件被读。


更改文件格式。

格式插入代码引导读者,使其知道哪个

对象为创建。 - >你知道什么派生类*你读*文件

因此可以创建正确的对象。

我想我可以做很长的路,(阅读文件和取决于类型调用相关的派生类),但这似乎不是很有效。


如上所述:更改文件格式。总是可以使用信息来扩充文件

,以便读者可以弄清楚要做什么。

为了更糟糕,我想保存所有的指针矢量。

std :: vector< A *>

但是在向量中包含派生类指针是否安全?

Hi,

If I have two classes.

Class A
{
public:
A( const char *sz);
~A();
virtual void Foo() = 0;
}

Class B : public A
{
public:
B(const char *sz) : A( sz ) {};
~B(){ /* do clean up */ };
virtual void Foo();

int iSomeObject;
char *pSomePointer;
}

if i do

A *test = new A();
//
// is it then safe to do
//

((B *)test)->Foo();
No. test points to an object of type A, not B.

// or even
((B *)test)->iSomeObject = 10
No. test points to an object of type A. Such an object
doesn''t have a member iSomeObject. No memory space
was reserved for such a member, since your new requested
an A object, not a B.

//////////////////////////////////////////////
Why do I ask?

because my base class reads information from a file.
So it is easier for me to have one call

A * test = new A( /* file name to read */ )

Depending on the actual data in the file a certain base class will be
used...

I could do

B * test = new B( /* file name to read */ )

but as I said, I only know what derived class to use after the file has been
read.
Change your file format.
In the format insert codes which guide the reader, such that it knows which
object to create. -> You know what derived class *during* you read the file
and hence can create the correct object.
I guess I could do it the long way around, (read the file and depending on
the type call the relevant derived class), but that does not seem very
efficient.
As said: Change your file format. It is always possible to augument the file
with information, such that the reader can figure out what to do.

To make matters worse I want to save all the pointers in a vector.

std::vector< A* >

but is it safe to include derived classes pointer in a vector?




那没关系。

每个A指针总是可以指向从A中删除的任何其他类。


-

Karl Heinz Buchegger
kb ****** @ gascad.at


Simon ha scritto:
Simon ha scritto:
A类
{
公开:
A(const char * sz);
~A();
虚拟空Foo()= 0;
}

B组:公众A
公开:
B(const char * sz):A(sz ){};
~B(){/ *做清理* /};
虚拟空虚Foo();

int iSomeObject;
char * pSomePointer ;
}

你不能这样做,虚拟的纯粹的课程不可能是即时的A * test = new A();


这是非法的,你试图在A((B *)测试中)调用B ::方法 - > Foo();
(( B *)测试) - > iSomeObject = 10
Class A
{
public:
A( const char *sz);
~A();
virtual void Foo() = 0;
}

Class B : public A
{
public:
B(const char *sz) : A( sz ) {};
~B(){ /* do clean up */ };
virtual void Foo();

int iSomeObject;
char *pSomePointer;
}
You can''t do that, vitual pure classes can''t be instantied A *test = new A();
It''s illegal, you are trying to call a B::method on A ((B *)test)->Foo();
((B *)test)->iSomeObject = 10




B IS-A A,但不是反之亦然

测试=新B(''stuffstring'');


然后你可以:


test-> Foo(); //法律

test-> iSomeObject = 0; //错误


B级{/*...*/};

D级:公共B {/*.../*};


D是-A B,​​但B不是D.


当你(公开)扩展课程时你可能都会改变行为和

添加功能,基类不需要知道任何关于它的事情

(所以你可以强制B使用仅在D中声明的函数)。


似乎你已经以相反的方式理解了继承。


再见,

Giulio



B IS-A A, but not viceversa

A test = new B(''stuffstring'');

then you can :

test->Foo(); // LEGAL
test->iSomeObject = 0; // ERROR

class B {/*...*/};
class D : public B {/*.../*};

D is-a B, but B isn''t D.

when you (publicly) extend a class you may both change behavior and
adding features, and the base class don''t have to know anything about it
(so you can force B to use functions declared only in D).

It seems your had understood inheritance in the opposite way.

Bye,
Giulio




" Karl Heinz Buchegger" < KB ****** @ gascad.at>在消息中写道

news:43 *************** @ gascad.at ...

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:43***************@gascad.at...
Simon写道:

更改文件格式。
以插入代码的格式引导读者,使其知道要创建的对象。 - >你知道什么派生类*你读*
文件
因此可以创建正确的对象。
Simon wrote:

Change your file format.
In the format insert codes which guide the reader, such that it knows
which
object to create. -> You know what derived class *during* you read the
file
and hence can create the correct object.
我想我可以做很长的路,(读取文件并依赖
类型调用相关的派生类),但这似乎不是很有效。
I guess I could do it the long way around, (read the file and depending
on
the type call the relevant derived class), but that does not seem very
efficient.



As说:改变你的文件格式。总是可以用信息来宣传
文件,以便读者可以弄清楚要做什么。



As said: Change your file format. It is always possible to augument the
file
with information, such that the reader can figure out what to do.


更糟糕的是我想将所有指针保存在向量中。

std :: vector< A *>

但是在向量中包含派生类指针是否安全?

To make matters worse I want to save all the pointers in a vector.

std::vector< A* >

but is it safe to include derived classes pointer in a vector?



那没关系。
每个A指针总是可以指向任何其他来自A的课程。



That''s fine.
Every A pointer can always point to any other class dervied from A.




谢谢你。

最后一个问题。

当需要删除向量中的所有指针时,我会

通常做


A * pSomePointer =(*(m_vectorData) .begin()+ nPos));

删除pSomePointer;


如果使用派生类B创建pSomePointer将是析构函数

B被叫或者是A的析构函数?


Simon



Thanks for that.
One last question.

When it comes time to delete all the pointers in the vector, I would
normally do

A *pSomePointer = (*(m_vectorData.begin()+nPos));
delete pSomePointer;

if pSomePointer was created with the derived class B will the destructor of
B be called or the destructor of A?

Simon


这篇关于试图理解从基础到派生的铸造。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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