为什么它不是一个类而是一个指针 [英] Why it's a pointer not just a class

查看:108
本文介绍了为什么它不是一个类而是一个指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




typedef std :: vector< Vehicle * VehicleList;


Vehicle是UDT或类。为什么车辆*不仅仅是车辆在< > ;.

哪个更好,为什么?如果您可以通过列出一些

代码来解释它,我们将非常感谢!


提前致谢,

Michael

解决方案

" Michael" < mi ******* @ gmail.comwrote:





typedef std :: vector< ;车辆*车辆清单;


车辆是UDT或类。为什么车辆*不仅仅是车辆在< > ;.

哪个更好,为什么?如果您可以通过列出一些

代码来解释它,我们将非常感谢!



class Vehicle {};

class Car:public Vehicle {};


vector< ; VehicleobjVec;

objVec.push_back(Car());

//坏,将所有汽车行为切成对象


vector< Vehicle * ptrVec;

ptrVec.push_back(new Car()); //好,所有Car的行为都得到维护


第二个结构的问题是向量不知道

何时删除对象包含的内容。其他一些对象必须跟踪

。这通常是通过某种智能指针完成的。


vector<的shared_ptr<车辆smartVec;




Michael写道:





typedef std :: vector< Vehicle * VehicleList;


Vehicle是UDT或类。为什么车辆*不仅仅是车辆在< > ;.

哪个更好,为什么?如果您可以通过列出一些

代码来解释它,我们将非常感谢!


提前致谢,

Michael



因为车辆是抽象的。你的目标是创建一个包含Vehicle类衍生物的容器

。汽车是一种

车辆。卡车是一种车辆。公共汽车是一种车辆。为什么

必须为每个新类型创建一个新的容器

车辆可以存储任何与车辆相关的类型,包括

混合车辆?


多态,您正在创建一个指向车辆指南的容器

衍生品。然后,指向每个元素的指针允许发生相应的行为:brake(),reverse(),accele()。例如,一辆

汽车不会像卡车那样倒车()卡车需要发出警告

蜂鸣器。指针基本上让vtable做它的魔术。


就代码而言:


#include< iostream>

#include< ostream>

#include< vector>


class Vehicle

{

公开:

车辆(){}

虚拟〜车辆(){} //虚拟

virtual void reverse()= 0; //纯虚拟 - 见下文

};


类汽车:公共汽车

{

public:

Car(){}

~Car(){std :: cout<< "〜车()" <<的std :: ENDL; }

void reverse(){std :: cout<< "倒车" <<的std :: ENDL; }

};


级卡车:公共汽车

{

public:

卡车(){}

~卡车(){std :: cout<< "〜卡车()" <<的std :: ENDL; }

void reverse(){std :: cout<< 蜂鸣器,倒车 <<的std :: ENDL; }

};


int main()

{

std :: vector<车辆*车辆;

// vehicles.push_back(新车辆);错误 - 车辆是抽象的

vehicles.push_back(新车);

vehicles.push_back(新卡车);


车辆[0] - > reverse();

vehicles [1] - > reverse();


for(int i = 0; i< ; vehicles.size(); ++ i)//或size_t

{

删除车辆[i];

}

返回0;

}


/ *

倒车

蜂鸣器,倒车

~汽车()

~卡车()

* /


析破器是虚拟化,他们必须为此工作。反向()在Vehicle中被声明为纯虚拟的原因是强制

客户端程序员按顺序提供reverse()成员函数

使用这个系统。

请注意,我可以编写一个派生自Car的SportsCar类,而

仍然在车辆容器中使用它。一辆SportsCar is_a车是_a

车辆,因此一辆SportsCar is_a车辆。

如果您还没有看到这样一个系统的好处,请考虑

将std :: vector封装在一个容器类中:突然之间,
你有一个可容纳任何车辆的容器 - 包括那些

尚未创建的衍生物(即:SuperSportsCar或

TenTonTruck)。无需以任何方式修改容器。

这是一个非常强大的概念 - 您的代码可以扩展,而不必修改原始类。


"迈克尔" < mi ******* @ gmail.comschrieb im Newsbeitrag

新闻:11 ********************** @ i42g2000cwa.googlegr oups.com ...





typedef std :: vector< Vehicle * VehicleList;


车辆是UDT或类。为什么车辆*不仅仅是车辆在<取代。



看看如何定义Vehicle以及如何使用它。


哪一个更好,为什么?



哪一个更好 - 用锤子或牙刷 - 为什么?


没有比另一个更好的。这取决于你想做什么。


亨氏


Hi,

typedef std::vector<Vehicle* VehicleList;

Vehicle is a UDT, or a class. Why is Vehicle* not just Vehicle in < >.
Which one is better and why? If you could explain it by laying out some
codes, it would be highly appreciated!

Thanks in advance,
Michael

解决方案

"Michael" <mi*******@gmail.comwrote:

Hi,

typedef std::vector<Vehicle* VehicleList;

Vehicle is a UDT, or a class. Why is Vehicle* not just Vehicle in < >.
Which one is better and why? If you could explain it by laying out some
codes, it would be highly appreciated!

class Vehicle { };
class Car : public Vehicle { };

vector<VehicleobjVec;
objVec.push_back( Car() );
// bad, slices all Car behavior off of the object

vector<Vehicle*ptrVec;
ptrVec.push_back( new Car() ); // good, all Car behavior is maintained

The problem with the second construct is that the vector doesn''t know
when to delete the objects it contains. Some other object must track
that. This is often done with some sort of smart pointer.

vector< shared_ptr< Vehicle smartVec;



Michael wrote:

Hi,

typedef std::vector<Vehicle* VehicleList;

Vehicle is a UDT, or a class. Why is Vehicle* not just Vehicle in < >.
Which one is better and why? If you could explain it by laying out some
codes, it would be highly appreciated!

Thanks in advance,
Michael

Because a Vehicle is abstract. Your goal here is to create a container
that holds derivatives of the Vehicle class. A Car is a type of
vehicle. A Truck is a type of Vehicle. A Bus is a type of Vehicle. Why
have to create a new container for each new type when a container of
Vehicles can store any type that is related to Vehicles, including a
mix of Vehicles?

Polymorphicly, you are creating a container of pointers to Vehicle
derivatives. That pointer to each element then allows the appropriate
behaviour to happen: brake(), reverse(), accelerate(). For example, a
car will not reverse() like a truck - a truck needs to sound a warning
buzzer first. The pointer essentially lets the vtable do its "magic".

As far as code is concerned:

#include <iostream>
#include <ostream>
#include <vector>

class Vehicle
{
public:
Vehicle() { }
virtual ~Vehicle() { } // virtual
virtual void reverse() = 0; // pure virtual -- see below
};

class Car : public Vehicle
{
public:
Car() { }
~Car() { std::cout << "~Car()" << std::endl; }
void reverse() { std::cout << "reversing" << std::endl; }
};

class Truck : public Vehicle
{
public:
Truck() { }
~Truck() { std::cout << "~Truck()" << std::endl; }
void reverse() { std::cout << "buzzer, reversing" << std::endl; }
};

int main()
{
std::vector< Vehicle* vehicles;
// vehicles.push_back( new Vehicle ); error - Vehicle is abstract
vehicles.push_back( new Car );
vehicles.push_back( new Truck );

vehicles[ 0 ]->reverse();
vehicles[ 1 ]->reverse();

for ( int i = 0; i < vehicles.size(); ++i ) // or size_t
{
delete vehicles[ i ];
}
return 0;
}

/*
reversing
buzzer, reversing
~Car()
~Truck()
*/

The destructors are virtual too, they have to be for this to work. The
reason that reverse() was declared pure virtual in Vehicle is to force
the client programmer to provide a reverse() member function in order
to use this system.
Note that i can write a SportsCar class which derives from Car and
still use it in the vehicles container. A SportsCar is_a Car which is_a
Vehicle, hence a SportsCar is_a Vehicle.
If you don''t yet see the benefits of such a system, consider
encapsulating the std::vector in a container class: all of a sudden,
you''ld have a container that can hold any Vehicle - including those
derivatives that have not been created yet (ie: SuperSportsCar or
TenTonTruck). Without having to modify the container in any way.
Thats a very powerful concept - your code becomes extensible without
having to modify the original classes.


"Michael" <mi*******@gmail.comschrieb im Newsbeitrag
news:11**********************@i42g2000cwa.googlegr oups.com...

Hi,

typedef std::vector<Vehicle* VehicleList;

Vehicle is a UDT, or a class. Why is Vehicle* not just Vehicle in < >.

Look how Vehicle is defined and how it is used.

Which one is better and why?

Which one is better - a hammer or a tooth brush - and why?

None is better than the other. It depends on what you want to do.

Heinz


这篇关于为什么它不是一个类而是一个指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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