用C ++编写Scalabe软件 [英] Writing Scalabe Software in C++

查看:65
本文介绍了用C ++编写Scalabe软件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


今天早上我知道如何编写可扩展软件。

不幸的是,Delphi 2007不可能做到这一点因为它不支持

操作重载类或记录继承(记录确实有
运算符重载)


这个想法是写一个通用的整数类,派生整数classess

为8位,16位,32位,64位和64位模拟。


然后在运行时计算机程序可以确定执行必要计算所需的派生整数

类。


必要的整数类被实例化并分配给泛型

整数类变量/引用和泛型引用/变量

用于编写执行计算的实际代码。


下面是一个演示程序,它尚未完全编译,但

它正在接近。


// TestWritingScalableSoftware.cpp:定义控制台的入口点

应用程序。

//

#include" stdafx.h"


class TSkybuckGenericInteger

{

};

class TSkybuckInt32:public TSkybuckGenericInteger

{

private:

int mInteger;


public:


//带有初始化参数的构造函数

TSkybuckInt32(int ParaValue);


//添加运算符overloader

TSkybuckInt32& operator +(const TSkybuckInt32& ParaSkybuckInt32);


void显示();

};


类TSkybuckInt64: TSkybuckGenericInteger

{

private:

long long mInteger;


public:

//带有初始化参数的构造函数

TSkybuckInt64(long long ParaValue);


//添加运算符overloader

TSkybuckInt64&安培; operator +(const TSkybuckInt64& ParaSkybuckInt64);


void显示();

};


// < br $>
// TSkybuckInt32

//构造函数

TSkybuckInt32 :: TSkybuckInt32(int ParaValue)

{

mInteger = ParaValue;

}


//添加运算符overloader

TSkybuckInt32& TSkybuckInt32 :: operator +(const TSkybuckInt32&

ParaSkybuckInt32)

{

mInteger = mInteger + ParaSkybuckInt32.mInteger;

返回* this;

}


无效TSkybuckInt32 ::显示()

{

printf("%d \ n",mInteger);

}


//

// TSkybuckInt64

//

//构造函数

TSkybuckInt64 :: TSkybuckInt64(long long ParaValue)

{

mInteger = ParaValue;

}


//添加运算符overloader

TSkybuckInt64& TSkybuckInt64 :: operator +(const TSkybuckInt64&

ParaSkybuckInt64)

{

mInteger = mInteger + ParaSkybuckInt64.mInteger;

返回* this;

}


无效TSkybuckInt64 ::显示()

{

printf("%lu \ n",mInteger);

}


int _tmain(int argc,_TCHAR * argv [])

{

long long FileSize;

long long MaxFileSize32bit;


//必须写这样的代码使用构造函数?不能只是声明

a,b,c?

TSkybuckInt32 A32 = TSkybuckInt32(30);

TSkybuckInt32 B32 = TSkybuckInt32(70) ;

TSkybuckInt32 C32 = TSkybuckInt32(0);

C32 = A32 + B32;

C32.Display();


TSkybuckInt64 A64 = TSkybuckInt64(30);

TSkybuckInt64 B64 = TSkybuckInt64(70);

TSkybuckInt64 C64 = TSkybuckInt64(0);

C64 = A64 + B64;

C64.Display();


FileSize = 1024; // kilobyte

FileSize = FileSize * 1024; //兆字节

FileSize = FileSize * 1024; // gigabyte

FileSize = FileSize * 1024; // terrabyte


MaxFileSize32bit = 1024; // kilobyte

MaxFileSize32bit = MaxFileSize32bit * 1024; //兆字节

MaxFileSize32bit = MaxFileSize32bit * 1024; // gigabyte

MaxFileSize32bit = MaxFileSize32bit * 4; // 4千兆字节

if(FileSize< MaxFileSize32bit)

{

TSkybuckGenericInteger AGeneric = TSkybuckInt32(30);

TSkybuckGenericInteger BGeneric = TSkybuckInt32(70);

TSkybuckGenericInteger CGeneric = TSkybuckInt32(0);

}否则

{

TSkybuckGenericInteger AGeneric = TSkybuckInt64(30);

TSkybuckGenericInteger BGeneric = TSkybuckInt64(70);

TSkybuckGenericInteger CGeneric = TSkybuckInt64(0);

}


CGeneric = AGeneric + BGeneric;

CGeneric.Display();


while(1)

{

}


返回0;

}


可能是小编译问题仍然存在:


错误1错误C2243:''type cast'':从''转换为'TSkybuckInt64 * __ w64''

到''const TSkybuckGenericInteger&''存在,但无法访问

y:\ cpp \tests \ test可扩展的软件通用math \version

0.01 \ testwritingscalablesoftware\testwritingingscalab lesoftware\testwritingingscalablesoftware.cpp




错误2错误C2243:''type cast'':从''TSkybuckInt64 * __ w64'转换'

到''const TSkybuckGenericInteger&''存在,但是无法访问
$ b $通过:\ cpp\tests\test编写可扩展的软件通用math \version

0.01 \ testwritingscalablesoftware\testwritingingscalab lesoftware\testwritingingscalablesoftware.cpp

153


错误3错误C2243:''type cast'':从''TSkybuckInt64 * __ w64转换''

到''const TSkybuckGenericInteger&''存在,但是无法访问

y:\ cpp \tests\test编写可扩展的软件通用math\version

0.01 \testwritingscalablesoftware\testwritingingscalab lesoftware \\ teswritingscalablesoftware.cpp

154

错误4错误C2065:''CGeneric'':未声明的标识符y:\ cpp \tests\test < br $>
编写可扩展的软件通用math \version

0.01 \ testwritingscalablesoftware\testwritingingscalab lesoftware\testwritingingscalablesoftware.cpp

157


错误5错误C2065:''AGeneric'':未声明的标识符y:\ cpp \tests\test

编写可扩展的软件通用math\version
0.01 \ testwritingscalablesoftware\testwritingingscalab lesoftware\testwritingingscalablesoftware.cpp

157


错误6错误C2065:''BGeneric'':未声明的标识符y:\ cpp \tests \ test

编写可扩展的软件通用math\version

0.01 \ testwritingscalablesoftware\testwritingingscalab lesoftware\testwritingingscalablesoftware.cpp

157


如何解决剩下的问题?


再见,

Skybuck。

解决方案

对于那些错过了其他线程的人来说,这就是为什么我想要

这样的解释:


对于32位编译器:


int(32位有符号整数)很快,它被翻译成单个32位cpu

指令。


多长(64位有符号整数)很慢,它被翻译成多个32

位cpu指令。


对于64位编译器


long long(64位有符号整数)应该很快,它被翻译成

单64位cpu指令。


我想只编写一次代码!不是三次!我想要最高速度




所以我需要一个通用的整数类,它将使用相似的类,

程序必须在运行时决定什么是必要的,并且还要提供良好的

性能!


我相信/希望在一些小修复之后提供的示例应该能够to

做我想做的事;)!


再见,

Skybuck。


好吧,既然代码还没有编译,我就不能看看asm生成了

但现在我想起来了......


也许C ++确实喜欢使用可编译的

代码的多态或虚函数,这可能会带来比它的价值更多的开销。

仍有待观察。


再见,

Skybuck。


开8月29日上午7:02,Skybuck Flying < s ... @ hotmail.comwrote:


你好,


今天早上我知道怎么写一般的可扩展软件。

不幸的是,Delphi 2007不能这样做,因为它不支持

操作类的重载或记录继承(记录确实有

运算符重载)



这个参数在两个方面是错误的。它假定的东西不是真实的,然后得出不能遵循的结论。


Delphi实现了对象和虚方法。任何具有

这些功能的语言都能够在编译时类型不是
的值上运行。

另一方面这个以及你在下面包含的内容都不会像你开始尝试建议的那样。它们只是

的方法,可以在逻辑流程中的某个点使用不同的指令

,具体取决于所考虑的变量类型。


Hello,

This morning I had an idea how to write Scalable Software in general.
Unfortunately with Delphi 2007 it can''t be done because it does not support
operating overloading for classes, or record inheritance (records do have
operator overloading)

The idea is to write a generic integer class with derived integer classess
for 8 bit, 16 bit, 32 bit, 64 bit and 64 bit emulated.

Then at runtime the computer program can determine which derived integer
class is needed to perform the necessary calculations.

The necessary integer class is instantiated and assigned to a generic
integer class variable/reference and the generic references/variables are
used to write the actual code that performs the calculations.

Below is a demonstration program, it''s not yet completely compiling, but
it''s getting close.

// TestWritingScalableSoftware.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"

class TSkybuckGenericInteger
{
};
class TSkybuckInt32 : public TSkybuckGenericInteger
{
private:
int mInteger;

public:

// constructor with initializer parameter
TSkybuckInt32( int ParaValue );

// add operator overloader
TSkybuckInt32& operator+( const TSkybuckInt32& ParaSkybuckInt32 );

void Display();
};

class TSkybuckInt64 : TSkybuckGenericInteger
{
private:
long long mInteger;

public:
// constructor with initializer parameter
TSkybuckInt64( long long ParaValue );

// add operator overloader
TSkybuckInt64& operator+( const TSkybuckInt64& ParaSkybuckInt64 );

void Display();
};

//
// TSkybuckInt32

// constructor
TSkybuckInt32::TSkybuckInt32( int ParaValue )
{
mInteger = ParaValue;
}

// add operator overloader
TSkybuckInt32& TSkybuckInt32::operator+ ( const TSkybuckInt32&
ParaSkybuckInt32 )
{
mInteger = mInteger + ParaSkybuckInt32.mInteger;
return *this;
}

void TSkybuckInt32::Display()
{
printf( "%d \n", mInteger );
}

//
// TSkybuckInt64
//
// constructor
TSkybuckInt64::TSkybuckInt64( long long ParaValue )
{
mInteger = ParaValue;
}

// add operator overloader
TSkybuckInt64& TSkybuckInt64::operator+ ( const TSkybuckInt64&
ParaSkybuckInt64 )
{
mInteger = mInteger + ParaSkybuckInt64.mInteger;
return *this;
}

void TSkybuckInt64::Display()
{
printf( "%lu \n", mInteger );
}

int _tmain(int argc, _TCHAR* argv[])
{
long long FileSize;
long long MaxFileSize32bit;

// must write code like this to use constructor ? can''t just declare
a,b,c ?
TSkybuckInt32 A32 = TSkybuckInt32( 30 );
TSkybuckInt32 B32 = TSkybuckInt32( 70 );
TSkybuckInt32 C32 = TSkybuckInt32( 0 );
C32 = A32 + B32;
C32.Display();

TSkybuckInt64 A64 = TSkybuckInt64( 30 );
TSkybuckInt64 B64 = TSkybuckInt64( 70 );
TSkybuckInt64 C64 = TSkybuckInt64( 0 );
C64 = A64 + B64;
C64.Display();

FileSize = 1024; // kilobyte
FileSize = FileSize * 1024; // megabyte
FileSize = FileSize * 1024; // gigabyte
FileSize = FileSize * 1024; // terrabyte

MaxFileSize32bit = 1024; // kilobyte
MaxFileSize32bit = MaxFileSize32bit * 1024; // megabyte
MaxFileSize32bit = MaxFileSize32bit * 1024; // gigabyte
MaxFileSize32bit = MaxFileSize32bit * 4; // 4 gigabyte
if (FileSize < MaxFileSize32bit)
{
TSkybuckGenericInteger AGeneric = TSkybuckInt32( 30 );
TSkybuckGenericInteger BGeneric = TSkybuckInt32( 70 );
TSkybuckGenericInteger CGeneric = TSkybuckInt32( 0 );
} else
{
TSkybuckGenericInteger AGeneric = TSkybuckInt64( 30 );
TSkybuckGenericInteger BGeneric = TSkybuckInt64( 70 );
TSkybuckGenericInteger CGeneric = TSkybuckInt64( 0 );
}

CGeneric = AGeneric + BGeneric;
CGeneric.Display();

while (1)
{
}

return 0;
}

Probably minor compile issue''s remain:

Error 1 error C2243: ''type cast'' : conversion from ''TSkybuckInt64 *__w64 ''
to ''const TSkybuckGenericInteger &'' exists, but is inaccessible
y:\cpp\tests\test writing scalable software generic math\version
0.01\testwritingscalablesoftware\testwritingscalab lesoftware\testwritingscalablesoftware.cpp
152

Error 2 error C2243: ''type cast'' : conversion from ''TSkybuckInt64 *__w64 ''
to ''const TSkybuckGenericInteger &'' exists, but is inaccessible
y:\cpp\tests\test writing scalable software generic math\version
0.01\testwritingscalablesoftware\testwritingscalab lesoftware\testwritingscalablesoftware.cpp
153

Error 3 error C2243: ''type cast'' : conversion from ''TSkybuckInt64 *__w64 ''
to ''const TSkybuckGenericInteger &'' exists, but is inaccessible
y:\cpp\tests\test writing scalable software generic math\version
0.01\testwritingscalablesoftware\testwritingscalab lesoftware\testwritingscalablesoftware.cpp
154

Error 4 error C2065: ''CGeneric'' : undeclared identifier y:\cpp\tests\test
writing scalable software generic math\version
0.01\testwritingscalablesoftware\testwritingscalab lesoftware\testwritingscalablesoftware.cpp
157

Error 5 error C2065: ''AGeneric'' : undeclared identifier y:\cpp\tests\test
writing scalable software generic math\version
0.01\testwritingscalablesoftware\testwritingscalab lesoftware\testwritingscalablesoftware.cpp
157

Error 6 error C2065: ''BGeneric'' : undeclared identifier y:\cpp\tests\test
writing scalable software generic math\version
0.01\testwritingscalablesoftware\testwritingscalab lesoftware\testwritingscalablesoftware.cpp
157

How to solve the remaining issue''s ?

Bye,
Skybuck.

解决方案

For those that missed the other threads here is the explanation why I want
something like this:

For 32 bit compilers:

int (32 bit signed integer) is fast, it''s translated to single 32 bit cpu
instructions.

long long (64 bit signed integer) is slow, it''s translated to multiple 32
bit cpu instructions.

For 64 bit compilers

long long (64 bit signed integer) should be fast, it''s translated to a
single 64 bit cpu instruction.

I want to write code just once ! not three times ! and I want maximum speed
!

So I need a generic integer class which will use the appriorate class, the
program must decide what''s necessary at runtime, and still give good
performance !

I believe/hope the provided example after some minor fixes should be able to
do what I want ;) !

Bye,
Skybuck.


Well, since the code doesn''t compile yet I can''t look at the asm generated
but now that I think about it...

Maybe C++ does like polymorhpic or virtual function stuff for compileable
code and that might introduce more overhead than it''s worth.

Remains to be seen.

Bye,
Skybuck.


On Aug 29, 7:02 am, "Skybuck Flying" <s...@hotmail.comwrote:

Hello,

This morning I had an idea how to write Scalable Software in general.
Unfortunately with Delphi 2007 it can''t be done because it does not support
operating overloading for classes, or record inheritance (records do have
operator overloading)

This argument is wrong in two ways. It assumes things that are not
true and then draws conclusions that don''t follow.

Delphi implements objects, and virtual methods. Any language that has
these features is able to operate on values where the type is not
known at compile time.
On the other hand neither this nor what you included below will do
what you started off trying to suggest. They are just methods by
which different instructions can be used at a point in the logic flow
depending on the sort of variable under consideration.


这篇关于用C ++编写Scalabe软件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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