将虚拟类实例化为main [英] Instantiate virtual classes into main

查看:72
本文介绍了将虚拟类实例化为main的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



假设我有一个带有相关.cpp文件的单独头文件。通常情况下,如果我想用普通类做这个,要在main中实例化,可以这样做:



类对象; 





这对虚拟课程不起作用,我理解这一点。尽管阅读,我仍然不是百分之百确定原因。


使用指针,不能像

 Class *对象一样工作; 





但是:

 Class * object(); 

符合。为什么是这样?是否按预期工作?



我尝试过:



上面提到的一切。我是C ++的新手,当涉及到为类使用两个单独的文件时更加环保(任何解释它的教程都会有所帮助)。投掷虚拟,让我更加困惑。

解决方案

你好



对于不仅仅是C ++的新人而且C,最好从一个或多个教程开始。这将有助于您了解最重要的基础知识和一些基本程序,然后您可以自己尝试更高级的东西。



这是一个关于Cplusplus的体面教程。 com,它也是高级语言功能的一个很好的参考站点: C ++语言教程



关于你的问题:

1.有一个或多个虚函数的类无法实例化。这就是所谓的(纯)虚拟或抽象类。通常,如果你想要做的只是描述一个特定的行为,而不是真实类的整个定义,或者你想要描述几个相关类共享的行为,你就会使用这些类。



示例:

  //  文件Shape.h  

// class Shape描述一些几何对象
// 可能会绘制,但如何绘制形状取决于具体类型形状
// 因此我们将此类定义为纯虚拟,并将绘图保留为
// 具体形状类:
形状{
public
virtual void draw()= 0 ; // 纯虚拟或抽象成员函数
};



  //  文件Triangle.h  

#include Shape.h //读取基类的声明Shape

/ / 类三角形
三角形:公开形状{
公开
虚拟 void draw(); // 此函数将绘制一个三角形
};



  //  文件Triangle.cpp  

#include Triangle.h

// Triangle成员函数的实现
void Triangle :: draw(){
// 在这里画一些三角形...
}



(现在对其他类型的形状做同样的事情,例如Circle,Square)

  //   file main.cpp  

#include Shape.h
#include Triangle.h
#include Circle.h
#include Square.h

int main(){
Shape * shapes [ 3 ];
// 构造形状对象
形状[ 0 ] = new Triangle();
形状[ 1 ] = new Square();
形状[ 2 ] = new Circle();

// 现在绘制所有形状:
for int i = 0 ; i< < span class =code-digit> 3 ; ++ i){
shapes [i] .draw();
}

// 再次销毁构造的对象
for int i = 0 ; i< 3 ; ++ i){
delete shapes [i];
}

return 0 ;
}



现在,如果您可以实例化类 Shape 的对象并将其分配给您的 shapes 数组,您认为对 draw()的调用会怎样?你不知道?好吧,编译器也没有。这就是它被禁止的原因。


(其他问题在单独的解决方案中)


2。下面的行只声明一个变量,它可以为类 Class 的对象保存一个地址:

 Class * object; 



这就像一个带有标题罗马的街道标志,躺在地上:它还没有指向任何地方,它与城市,罗马根本不一样。你可以将它种植在地面上,使它指向正确的方向,但它仍然只是一个路标,而不是一个城市。



3.仅限以下行声明一个不带参数的函数,并返回一个指向类型 Class 的对象的指针:

 Class * object(); 



请注意,尾随括号构成一个有效的函数参数列表,因此编译器将 object 解释为函数的名称,该函数具有一个空的参数列表。



这些微妙的差异对于理解非常重要,所以我只能重复我的建议,先通过教程。


关键字virtual表示您无法实例化该类。它是C ++语言中的一个规则。虚拟类通常用作基类来定义在不同类中实现的某些行为。

如果要在不同的文件中使用类,则必须包含标题:

#inlcude   class1.h  //    



对于使用指针,您还必须创建一个objekt并在最后删除它。

 Class * object =  new  Class();  //  创建实例 
object-> DoSomething(); // 调用函数
delete object; // 将其销毁



一个不错的tutorial

Hello,

Assume I have a separate header file with an associated .cpp file. Normally, if i wanted to do this with a regular class, to instantiate it in main, one would do:

Class object;



This does not work with virtual classes, I understand this much. Despite reading, I am still not 100 percent sure why.

Using a pointer, does not work either as doing

Class *object;



However:

Class *object();

complies. Why is this? Is it working as intended?

What I have tried:

Everything mentioned above. I am new to C++, and even greener when it comes to using two separate files for classes (any tutorial that explains it well would be helpful). Throwing in virtual, confuses me even more.

解决方案

Hello

For someone new to not only C++ but also C, it would be best to start with one or more tutorials. That would help you understand the most important basics and some basic program that you can then extend to try out more advanced stuff by yourself.

Here's a decent tutorial on Cplusplus.com, which is also a good reference site for advanced language features: C++ language tutorial

As to your questions:
1. a class that has one or more pure virtual functions cannot be instantiated. This is what is called a (pure) virtual, or 'abstract' class. Typically you use such classes if all you want to do is describing a specific behaviour, rather than the entire definition of the real class, or if you want to describe behaviour that is shared by several related classes.

Example:

// file Shape.h

// class Shape describes some geometric object
// it may be drawn, but how to draw a shape depends on the specific type of shape
// therefore we define this class as pure virtual, and leave the drawing to the
// concrete shape classes:
class Shape {
public:
   virtual void draw() = 0; // pure virtual or 'abstract' member function
};


// file Triangle.h

#include "Shape.h" // read declaration of the base class Shape

// class triangle
class Triangle : public Shape {
public:
   virtual void draw(); // this function will draw a triangle
};


// file Triangle.cpp

#include "Triangle.h"

// implementation of the Triangle member functions
void Triangle::draw() {
   // draw some triangle here ...
}


(and now do the same for other types of shapes, e. g. Circle, Square)

// file main.cpp

#include "Shape.h"
#include "Triangle.h"
#include "Circle.h"
#include "Square.h"

int main() {
   Shape* shapes[3];
   // construct the shape objects
   shapes[0] = new Triangle();
   shapes[1] = new Square();
   shapes[2] = new Circle();

   // now draw all shapes:
   for (int i = 0; i < 3; ++i) {
      shapes[i].draw();
   }

   // destroy the constructed objects again
   for (int i = 0; i < 3; ++i) {
      delete shapes[i];
   }

   return 0;
}


Now, if you could instantiate an object of class Shape and assign it to your shapes array, what do you think the call to draw() would do? You don't know? Well, the compiler doesn't either. That's why it is forbidden.

(rest of the questions in separate solution(s))


2. The follwing line only declares a variable that can hold an address to an object of class Class:

Class *object;


this is like a Street sign with the caption "Rome", lying on the ground: it is not yet pointing anywhere, and it is not the same as the city, Rome, at all. You can plant it in the ground and make it point in the right direction, but then it's still only a signpost, not a city.

3. The following line only declares a function that takes no arguments and returns a pointer to an object of type Class:

Class *object();


Note that the trailing brackets constitute a valid function argument list, and therefore the compiler interprets object as the name of a function, which has an empty argument list.

These kinds of subtle differences are really important to understand, so I can only repeat my suggestion to first work your way through a tutorial.


The keyword virtual means that you cant instantiate the class. It is a rule in the C++ language. Virtual classes are often used as base class to define some behavior which is implemented in different class.
If you want to use a class in a different file you must include the header:

#inlcude "class1.h"//


For using a pointer you also must create an objekt and delete it at the end.

Class *object = new Class();//create instance
object->DoSomething();//call a function
delete object;// destroy it


A nice tutorial.


这篇关于将虚拟类实例化为main的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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