帮助一个有成员函数的可怜的FORTRAN程序员? [英] Help a poor FORTRAN programmer with member functions?

查看:41
本文介绍了帮助一个有成员函数的可怜的FORTRAN程序员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



只是一点背景:我是一群FORTRAN程序员中的一员,看着转换为C ++的
。我们正在尝试写一些简单的例子来向我们的经理证明该语言的强大功能,所以他会在转换课程上向我们发送所有




其中一个原因是我们的代码中充斥着以下示例:


SUBROUTINE PRINT_ITEM(ITEM,ITEM_TYPE)

IF(ITEM_TYPE .EQ.SQUARE)那么

CALL PRINT_SQUARE(项目)

ELSEIF(ITEM_TYPE .EQ。CIRCLE)那么

CALL PRINT_CIRCLE( ITEM)

ELSEIF ...

(更多项目类型)

ENDIF

END


(道歉为使用FORTRAN代码玷污该组)。


显然,每当我们添加新的

对象类型或操作。


我想写一个C ++等价物,使用类和成员函数,这样我可以打印(b $ b)或绘制,或询问,或其他......)没有

的对象在运行时知道它的类型。


下面显示了几次尝试中的最新一次 - 编译器在PrintObject函数中抱怨

void *,尽管我认为我已经读到了无效*

可以用来表示指向某个东西的指针,但我不知道是什么。


可以修改此代码以获得我想要的效果吗?如果可能的话,我想避免使用

指向函数的指针。


谢谢!

#include< iostream。 h>


//班级声明

// ------------------


class Square {

public:

void Print();

};


void Square :: Print(){

cout<< 这是一个正方形;

}


class Circle {

public:

void Print();

};


void Circle :: Print(){

cout<< 这是一个圆圈;

}


//打印对象功能

// ------ ---------------


void PrintObject(void * object){

object-> Print() ;

}


//主程序

// ------------


int main(){

Square * square;

Circle * circle;


square = new Square;

circle = new Circle;


//直接致电会员功能


圈 - >打印();

square-> Print();


//通过PrintObject函数调用成员函数

PrintObject(circle);

PrintObject(square);


返回0;


}


Just a bit of background: I''m one of a group of FORTRAN programmers, looking
to switch to C++. We are trying to write a few simple examples to
demonstrate the power of the language to our manager, so he will send us all
on a conversion course.

One of many reasons is that our code is littered with examples of:

SUBROUTINE PRINT_ITEM(ITEM, ITEM_TYPE)
IF (ITEM_TYPE .EQ. SQUARE) THEN
CALL PRINT_SQUARE(ITEM)
ELSEIF (ITEM_TYPE .EQ. CIRCLE) THEN
CALL PRINT_CIRCLE(ITEM)
ELSEIF ...
(lots more item types)
ENDIF
END

(with apologies for sullying the group with FORTRAN code).

Obviously We need to find and modify all these blocks whenever we add a new
object type, or operation.

I want to write a C++ equivalent, using classes and member functions so that
I can print (or draw, or interrogate, or whatever...) an object without
knowing its type at runtime.

The latest of several attempts is shown below - the compiler complains about
the void* in the PrintObject function, though I thought I''d read that void*
could be used to mean "pointer to something, but I don''t know what".

Can this code be modifed to get the effect I want? I''d like to avoid using
pointers to functions if possible.

Thanks!
#include <iostream.h>

// Class declarations
// ------------------

class Square{
public:
void Print();
};

void Square::Print(){
cout << "This is a square";
}

class Circle{
public:
void Print();
};

void Circle::Print(){
cout << "This is a circle";
}

// Print object function
// ---------------------

void PrintObject(void* object){
object->Print();
}

// Main Program
// ------------

int main(){
Square* square;
Circle* circle;

square = new Square;
circle = new Circle;

// Call member functions directly

circle->Print();
square->Print();

// Call member functions through PrintObject function

PrintObject(circle);
PrintObject(square);

return 0;

}

推荐答案



" Anthony Jones" < me@privacy.net>在消息中写道

news:3K ******************** @ nildram.net ...

"Anthony Jones" <me@privacy.net> wrote in message
news:3K********************@nildram.net...
I想要编写一个C ++等价物,使用类和成员函数,所以
我可以打印(或绘制,或询问,或其他......)一个对象,而不是在运行时知道它的类型。


你的意思是不知道它的类型在编译时?它必须在运行时间运行,或者你永远无法用它做任何事情*! :-)

最近几次尝试如下所示 - 编译器抱怨
关于PrintObject函数中的void *,尽管我认为我读过
void *可以用来表示指向某事物的指针,但我不知道是什么。


是的,您可以使用void *来存储任何其他指针,但是你永远不能*使用*

指针代替任何东西(取消引用它),除非你先把它转回

无论它应该是什么。

//打印对象函数
// ---------------------

void PrintObject(void * object) {
对象 - >打印();
}
I want to write a C++ equivalent, using classes and member functions so that I can print (or draw, or interrogate, or whatever...) an object without
knowing its type at runtime.
You mean without knowing its type "at compile time"? It has to be known at
run-time, or you''d never be able to do *anything* with it! :-)

The latest of several attempts is shown below - the compiler complains about the void* in the PrintObject function, though I thought I''d read that void* could be used to mean "pointer to something, but I don''t know what".

Yes, you can use a void* to store any other pointer, but you can never *use*
that pointer for anything (dereference it) unless you first cast it back to
whatever it''s supposed to be.

// Print object function
// ---------------------

void PrintObject(void* object){
object->Print();
}




你要做什么而不是使用void *就是要有一个基础来自

的课程,您的所有特定形状都会继承。然后你在基类中创建一个虚拟打印

函数,并让每个派生类都重写

函数来执行自己的打印版本。


在尝试进一步研究之前,你真的需要阅读基类,继承和

虚函数。


-Howard




What you want to do instead of using a void* is to have a base class from
which all your specific shapes inherit. Then you make a virtual Print
function in the base class, and have each derived class overried that
function to do its own version of printing.

You really need to do some reading up on base classes, inheritance, and
virtual functions before attempting to go further on this.

-Howard





" Anthony Jones" < me@privacy.net>在消息中写道

news:3K ******************** @ nildram.net ...

"Anthony Jones" <me@privacy.net> wrote in message
news:3K********************@nildram.net...
I想要编写一个C ++等价物,使用类和成员函数,所以
我可以打印(或绘制,或询问,或其他......)一个对象,而不是在运行时知道它的类型。


你的意思是不知道它的类型在编译时?它必须在运行时间运行,或者你永远无法用它做任何事情*! :-)

最近几次尝试如下所示 - 编译器抱怨
关于PrintObject函数中的void *,尽管我认为我读过
void *可以用来表示指向某事物的指针,但我不知道是什么。


是的,您可以使用void *来存储任何其他指针,但是你永远不能*使用*

指针代替任何东西(取消引用它),除非你先把它转回

无论它应该是什么。

//打印对象函数
// ---------------------

void PrintObject(void * object) {
对象 - >打印();
}
I want to write a C++ equivalent, using classes and member functions so that I can print (or draw, or interrogate, or whatever...) an object without
knowing its type at runtime.
You mean without knowing its type "at compile time"? It has to be known at
run-time, or you''d never be able to do *anything* with it! :-)

The latest of several attempts is shown below - the compiler complains about the void* in the PrintObject function, though I thought I''d read that void* could be used to mean "pointer to something, but I don''t know what".

Yes, you can use a void* to store any other pointer, but you can never *use*
that pointer for anything (dereference it) unless you first cast it back to
whatever it''s supposed to be.

// Print object function
// ---------------------

void PrintObject(void* object){
object->Print();
}




你要做什么而不是使用void *就是要有一个基础来自

的课程,您的所有特定形状都会继承。然后你在基类中创建一个虚拟打印

函数,并让每个派生类都重写

函数来执行自己的打印版本。


在尝试进一步研究之前,你真的需要阅读基类,继承和

虚函数。


-Howard




What you want to do instead of using a void* is to have a base class from
which all your specific shapes inherit. Then you make a virtual Print
function in the base class, and have each derived class overried that
function to do its own version of printing.

You really need to do some reading up on base classes, inheritance, and
virtual functions before attempting to go further on this.

-Howard



你想做什么而不是使用void *是有一个基础来自
的所有特定形状继承的课程。然后你在基类中创建一个虚拟打印功能,并让每个派生类都覆盖了
函数来做自己的打印版本。

你真的需要做一些在尝试进一步研究之前,先阅读基类,继承和虚函数。
What you want to do instead of using a void* is to have a base class from
which all your specific shapes inherit. Then you make a virtual Print
function in the base class, and have each derived class overried that
function to do its own version of printing.

You really need to do some reading up on base classes, inheritance, and
virtual functions before attempting to go further on this.




我还建议您查看一些示例

可在互联网上找到。

google forc ++ polymorphism example" ......你可能会发现

一些带圆圈和矩形的例子...


参见例如 http://onestepback.org/articles/poly/


这篇关于帮助一个有成员函数的可怜的FORTRAN程序员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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