/*
* GridTest.h
*
* Created on: 2009-2-27
* Author: Administrator
*/
#ifndef GRIDTEST_H_
#define GRIDTEST_H_
class GridTest {
static const int x=3,y=4;
public:
GridTest();
virtual ~GridTest();
typedef int (GridTest:: *memberFuncType)(int x, int y);
void display();
int foreach(memberFuncType fn, int i, int j);
int test1(int x, int y);
int test2(int x, int y);
int test3(int x, int y);
int test4(int x, int y);
};
#endif /* GRIDTEST_H_ */
/*
* GridTest.cpp
*
* Created on: 2009-2-27
* Author: Administrator
*/
#include "GridTest.h"
#include <iostream>
using namespace std;
GridTest::GridTest() {
// TODO Auto-generated constructor stub
}
GridTest::~GridTest() {
// TODO Auto-generated destructor stub
}
int GridTest::foreach(memberFuncType fn, int x, int y){
cout<<"GridTest::foreach(memberFuncType fn, int x, int y)执行了"<<endl;
cout<<"fn(x,y)"<<(this->*fn)(x, y);
return (this->*fn)(x, y);
}
int GridTest::test1(int x, int y){
cout<<"GridTest::test1(int x, int y)执行了!"<<endl;
return 0;
}
int GridTest::test2(int x, int y){
cout<<"GridTest::test2(int x, int y)执行了!"<<endl;
return 0;
}
int GridTest::test3(int x, int y){
cout<<"GridTest::test3(int x, int y)执行了!"<<endl;
return 0;
}
int GridTest::test4(int x, int y){
cout<<"GridTest::test4(int x, int y)执行了!"<<endl;
return 0;
}
void GridTest::display(){
cout << this->foreach( this->test1, 4, 5);
//一直报错:no matching function for call to `GridTest::foreach(<unknown type>, int, int)'
}
int main()
{
GridTest *grid;
grid= new GridTest();
grid->display();
return 0;
}
这段代码在VC6.0上就没有问题,在Eclipse上编译就报错
no matching function for call to `GridTest::foreach(<unknown type>, int, int)'
}
posted @
2009-03-09 17:14 华剑缘 阅读(1579) |
评论 (5) |
编辑 收藏
#include
"
stdio.h
"
#include
"
stdlib.h
"
typedef
struct
{
int
number;
int
dicper;
}
ElemType;
typedef
struct
node
{
ElemType data;
struct
node
*
next;
}
Node,
*
LinkList;
void
Initiatte(LinkList
&
L)
{
if
((L
=
(LinkList)malloc(
sizeof
(Node)))
==
NULL)exit(
1
);
L
->
next
=
L;
}
int
Insert(LinkList
&
L,
int
i,ElemType x)
{
LinkList q,p;
p
=
L
->
next;
int
j
=
1
;
while
(p
!=
L
&&
j
<
i
-
1
)
{
p
=
p
->
next;
j
++
;
}
if
(j
!=
i
-
1
&&
i
!=
1
)
{
printf(
"
插入位置参数错!
"
);
return
0
;
}
q
=
(LinkList)malloc(
sizeof
(Node));
if
(
!
q)exit(
1
);
q
->
data
=
x;
q
->
next
=
p
->
next;
p
->
next
=
q;
return
1
;
}
int
IsNotEmpty(LinkList L)
{
if
(L
->
next
==
L)
return
0
;
else
return
1
;
}
int
Delete(LinkList
&
p)
{
LinkList q;
q
=
p
->
next;
p
->
next
=
p
->
next
->
next;
free(q);
return
1
;
}
void
output(LinkList
&
L,
int
m)
{
LinkList pre,q;
pre
=
L;
q
=
L
->
next;
while
(IsNotEmpty(L)
==
1
)
{
for
(
int
i
=
1
;i
<
m;i
++
)
{
pre;
q
=
q
->
next;
if
(q
==
L)
{
pre
=
q;
q
=
q
->
next;
}
}
printf(
"
%d**
"
,q
->
data.number);
m
=
q
->
data.dicper;
q
=
q
->
next;
if
(q
==
L)q
=
q
->
next;
Delete(pre);
}
}
int
main()
{
int
n;
printf(
"
请输入人数的值:\n
"
);
scanf(
"
%d
"
,
&
n);
ElemType
*
P;
LinkList L;
Initiatte(L);
int
i,m;
P
=
new
ElemType [n];
for
( i
=
0
;i
<
n;i
++
)
{
P[i].number
=
i
+
1
;
printf(
"
第%d个人输入密码
"
,i
+
1
);
printf(
"
\n
"
);
scanf(
"
%d
"
,
&
P[i].dicper);
}
for
(i
=
1
;i
<=
n;i
++
)
Insert(L,i,P[i
-
1
]);
printf(
"
对m的数值进行初始化:
"
);
scanf(
"
%d
"
,
&
m);
printf(
"
结果输出:
"
);
output(L,m);
delete [] P;
printf(
"
\n
"
);
return
0
;
}
12
posted @
2006-12-18 22:53 华剑缘 阅读(241) |
评论 (0) |
编辑 收藏
this指针只能在一个类的成员函数中调用,它表示当前对象的地址。下面是一个例子:
void Date::setMonth( int mn )
{
month = mn; // 这三句是等价的
this->month = mn;
(*this).month = mn;
}
1. this只能在成员函数中使用。
全局函数,静态函数都不能使用this。
实际上,成员函数默认第一个参数为T* const register this。
如:
class A{public: int func(int p){}};
其中,func的原型在编译器看来应该是: int func(A* const register this, int p);
2. 由此可见,this在成员函数的开始前构造的,在成员的结束后清除。
这个生命周期同任一个函数的参数是一样的,没有任何区别。
当调用一个类的成员函数时,编译器将类的指针作为函数的this参数传递进去。如:
A a;
a.func(10);
此处,编译器将会编译成: A::func(&a, 10);
嗯,看起来和静态函数没差别,对吗?不过,区别还是有的。编译器通常会对this指针做一些优化的,因此,this指针的传递效率比较高--如vc通常是通过ecx寄存器来传递this参数。
3. 回答
#1:this指针是什么时候创建的?
this在成员函数的开始执行前构造的,在成员的执行结束后清除。
#2:this指针存放在何处? 堆,栈,全局变量,还是其他?
this指针会因编译器不同,而放置的位置不同。可能是栈,也可能是寄存器,甚至全局变量。
#3:this指针如何传递给类中函数的?绑定?还是在函数参数的首参数就是this指针.那么this指针又是如何找到类实例后函数的?
this是通过函数参数的首参数来传递的。this指针是在调用之前生成的。类实例后的函数,没有这个说法。类在实例化时,只分配类中的变量空间,并没有为函数分配空间。自从类的函数定义完成后,它就在那儿,不会跑的。
#4:this指针如何访问类中变量的/?
如果不是类,而是结构的话,那么,如何通过结构指针来访问结构中的变量呢?如果你明白这一点的话,那就很好理解这个问题了。
在C++中,类和结构是只有一个区别的:类的成员默认是private,而结构是public。
this是类的指针,如果换成结构,那this就是结构的指针了。
#5:我们只有获得一个对象后,才能通过对象使用this指针,如果我们知道一个对象this指针的位置可以直接使用吗?
this指针只有在成员函数中才有定义。因此,你获得一个对象后,也不能通过对象使用this指针。所以,我们也无法知道一个对象的this指针的位置(只有在成员函数里才有this指针的位置)。当然,在成员函数里,你是可以知道this指针的位置的(可以&this获得),也可以直接使用的。
#6:每个类编译后,是否创建一个类中函数表保存函数指针,以便用来调用函数?
普通的类函数(不论是成员函数,还是静态函数),都不会创建一个函数表来保存函数指针的。只有虚函数才会被放到函数表中。
但是,既使是虚函数,如果编译器能明确知道调用的是哪个函数,编译器就不会通过函数表中的指针来间接调用,而是会直接调用该函数。
# 7:这些编译器如何做到的?8:能否模拟实现?
知道原理后,这两个问题就很容易理解了。
其实,模拟实现this的调用,在很多场合下,很多人都做过。
例如,系统回调函数。系统回调函数有很多,如定时,线程啊什么的。
举一个线程的例子:
class A{
int n;
public:
static void run(void* pThis){
A* this_ = (A*)pThis;
this_->process();
}
void process(){}
};
main(){
A a;
_beginthread( A::run, 0, &a );
}
这里就是定义一个静态函数来模拟成员函数。
也有许多C语言写的程序,模拟了类的实现。如freetype库等等。
其实,有用过C语言的人,大多都模拟过。只是当时没有明确的概念罢了。
如:
typedef struct student{
int age;
int no;
int scores;
}Student;
void initStudent(Student* pstudent);
void addScore(Student* pstudent, int score);
...
如果你把 pstudent改成this,那就一样了。
它相当于:
class Student{
public:
int age; int no; int scores;
void initStudent();
void addScore(int score);
}
const常量可以有物理存放的空间,因此是可以取地址的
///this指针是在创建对象前创建.
this指针放在栈上,在编译时刻已经确定.
并且当一个对象创建后,并且运行整个程序运行期间只有一个this指针.
posted @
2006-08-10 14:53 华剑缘 阅读(292) |
评论 (0) |
编辑 收藏
做题的意义
By EmilMatthew 06/04/16
我可以有相当的理由,认为自己是一个喜爱学习的人,但是,我亦不得不承认,我的这份对学习的热爱是有所偏失的,自然也就无法享受到学习的全然乐趣。尤其在做到一些稍难的问题的时候,长长是久攻不下,于是心生疑问,我这等热情的投入学习,怎为何还无法去攻克难题,体会一些学科精妙之处。
实际,这是一个谬误,攻克难题,除了有
PASSION
,有知识上的积累,更为关键的,需要的是一种思维能力。而这种思维能力,对于理工科的学习而言,做题是个必不可少的环节。有
PASSION
,喜欢看书是我的现状,这是好事情,但这绝不是说,有了这些就一定会有好的思维能力,虽然好的
PASSION
可以去激发一定的思维力,但好的思维力却是必须要有训练的这一环节的,否则,就会像一层窗户纸,一捅就破,经不起有难度,有深度问题的考验。
自从进了大学后,实际上我对考试的热情是逐渐下降的,对什么为考试而准备,忙的不可开焦之类的事甚为不齿。的确,到了大学还为考试而学习却实没什么意思,但是由厌恶考试而顺带着产生厌恶做题的情绪就是个很不好的结果了。其实我的学习热情一直是高涨的,尤其是大一感受了程序设计的美妙之后,更是曾经一度为之废寝忘食,再加上我对物理及数学那点或发自内心的或出于敬仰的喜爱,我对去解决一些有实际背景的问题还是充满热情的,每当能发现自己要能发现些或创造什么小成果的时候,更是令人心潮澎湃。但是,我对解题,也就是做传统的书面上的试题却一直提不起精神,尽管多数的题目自己是解得下来的,但是每当面对难题时,我仍旧是力不从心的。而且,我也发现,在自己看书的时候,很多最关键的证明,推导都相当的吃力。比如自动机的一些理论,较难的证明看几眼就开始晕了;再如算法分析理论方面的证明,很多时候也都是望而却步。究其原因,我想,最关键的一点,就是这其中的一个整体思路令人无法把握。而这思路,恰恰是思维过程的一个体现,光有知识的积累是远远不够的,解题是思维能力训练的一个至关重的环节。高中是也许由于过度的应试对解题心生厌倦了,但是到了大学里,如果你想学得深入的话对解题仍旧不能有丝毫的松懈。
读书,更多的时候是侧重吸收与理解,对于理工科类的书籍而言,读书是否读的透还与一个人的思维能力有着相当的关联。在仅知道向量及线性代数的基础上看张量的概念,有的人看的不知所云,有的人一知半解
(
我就属于这种
)
,有的人一看就懂了;再如一些力学中的分析及演算,对于学的不扎实的人来说,可能把各个小步骤分解了看都懂是怎么回事,可以综合到一起,就很难看出个所以然了。究其原因,在读书能力基本一致的前提下,关键就是看你在看到这样的一个概念阐述的时候,你在思维上能对它做出怎样的反应。而这种思维上反应的高下,和读书一样,也是要有一个积累的过程的,而这种过程的积累,在我看来,很大一部分就是通过做题来提高的。
思维的过程有归纳,推理,演绎,发散等许多方式,我们对某个问题的解决有意识和无意识中都用到了这些思维过程。无论是实际的工程问题,抑或是抽象出来的问题,如习题,思维能力起着至关重要的作用。当然,知识的积累在解决问题的过程中亦是举足轻重的。用唯物论的观点来看,二者的关系是相辅相承的。工程的,实际的问题相对要复杂和冗繁,但未必都很艰深,而习题中的抽象问题规模相对小,对问题的剖析能达到相当的深度。所以,任何一个有理性思考的人都可以看到,在学校把解决问题的基础打好了,无论是以后做研究还是去投入到生产实践中去,不说游刃有余,最起码,在经过一定的适应后,是完全可以掌控的。而通过习题在思维层上的训练,更是可以使你在理工科
----
这样一个富于理性化分析,精巧构建,高度技巧化的领域中达到融会贯通,触类旁通的作用。所以,以前我放弃或轻视做题的行为是何等的可笑!
之所以对解题提不起太多兴趣,其中很大的一部分原因是由于多数情况下我们的解题处于一种被动的局面。也就是说,被老师“追着赶”,在这样一种情绪下解题,一者难有好的心情,二者目的以完成任务为主,虽然能学到一些东西,但终究有些情非得已的意味。想有效而又快乐的解题,个人认为最好的法子就是主动出击,自己去找题来做,不必多找,找一两本较好出版社或在某一方面特别有实力的学校出的书是肯定不会错的。然后,就是自己的解题,回顾与体会了。至于解题的多少只能是因人而异了,这和你个人的实际情况有很大关联,但有一点,就是解题一定要投入,一定要重视自己做的这些题的从哪个角度去考查思维力以及对知识的运用、掌握,否则,妄想以多做题来达到思维飞跃的想法是徒劳的。个人认为,这学习的主动性一旦被调动起来之后,你马上会感受到解题也竟然和看书一样,有着许多收获的喜悦与惊奇。而从解题中可以直接得到的最为重要的好处在于:一来你对某门课程本身的体系和知识结构加深了理解和掌握;二来你的思维能力体系又得到了再一次的加强,遇到新问题的时候(哪怕和原来的领域不相关),你的活跃的思路也必定会给你带来解决问题的更多、更好的启示。
所以,作为一个理工科学生的你,无论是以后想做科研,还是想尽快投入生产实践,思维能力是极其重要的,而做题,则是其中的一条必由之路。当你真正投入时,你会发现,做题原来也可以这般的享受。
posted @
2006-05-15 23:01 华剑缘 阅读(229) |
评论 (0) |
编辑 收藏
1.某公司采用公用电话传递10组数据,每组数据必须由四位整数组成,数据在传递过程中是加密的,加密规则如下:每组数据中的每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。编程输出加密前后的数据。
posted @
2006-04-21 23:26 华剑缘 阅读(223) |
评论 (0) |
编辑 收藏
2005图灵奖获得者产生
|
|
3月1日,ACM(美国计算机学会)决定将2005年图灵奖颁发给Peter Naur,以表彰他在设计Algol 60语言上的贡献。由于其定义的清晰性,Algol 60成为了许多现代程序设计语言的原型。在语法描述中广泛使用的BNF范式,其中的“N”便是来自Peter Naur的名字。图灵奖被称为“计算科学界的诺贝尔奖”,它创立于1960年,现在的奖金10万美元,由Intel公司赞助。
详文如下:
SOFTWARE PIONEER PETER NAUR WINS ACM'S TURING AWARD
Dane's Creative Genius Revolutionized Computer Language Design
| New York, March 01, 2006 -- The Association for Computing Machinery (ACM) has named Peter Naur the winner of the 2005 A.M. Turing Award. The award is for Naur's pioneering work on defining the Algol 60 programming language. Algol 60 is the model for many later programming languages, including those that are indispensable software engineering tools today. The Turing Award, considered the "Nobel Prize of Computing" was first awarded in 1966, and is named for British mathematician Alan M. Turing. It carries a $100,000 prize, with financial support provided by Intel Corporation.
Dr. Naur was editor in 1960 of the hugely influential "Report on the Algorithmic Language Algol 60." He is recognized for the report's elegance, uniformity and coherence, and credited as an important contributor to the language's power and simplicity. The report made pioneering use of what later became known as Backus-Naur Form (BNF) to define the syntax of programs. BNF is now the standard way to define a computer language. Naur is also cited for his contribution to compiler design and to the art and practice of computer programming.
"Dr. Naur's ALGOL 60 embodied the notion of elegant simplicity for algorithmic expression," said Justin Rattner, Intel senior fellow and Chief Technology Officer. "Over the years, programming languages have become bloated with features and functions that have made them more difficult to learn and less effective. This award should encourage future language designers who are addressing today's biggest programming challenges, such as general-purpose, multi-threaded computation, to achieve that same level of elegance and simplicity that was the hallmark of ALGOL 60."
Contributions Signal Birth of Computing Science
In 2002, former Turing Award winner Edsger Dijkstra characterized the development of Algol 60 as "an absolute miracle" that signaled the birth of what he called "computing science" because it showed the first ways in which automatic computing could and should become a topic of academic concern. The development of Algol 60 was the result of an exceptionally talented group of people, including several who were later named Turing Award winners.
Dr. Naur's contribution to Algol 60, was seminal. John Backus, another former Turing Award winner, acknowledged Naur as the driving intellectual force behind the definition of Algol 60. He commented that Naur's editing of the Algol report and his comprehensive preparation for the January 1960 meeting in which Algol was presented "was the stuff that really made Algol 60 the language that it is, and it wouldn't have even come about, had he not done that."
Before publication of the Algol 60 Report, computer languages were informally defined by their prose manuals and the compiler code itself. The report, with its use of BNF to define the syntax, and carefully chosen prose to define the semantics, was concise, powerful, and unambiguous.
The 17-page Algol 60 Report presented the complete definition of an elegant, transparent language designed for communication among humans as well as with computers. It was deliberately independent of the properties of any particular computer. The new language was a major challenge to compiler writers. Dr. Naur went on to co-author the GIER Algol Compiler (for the transistorized electronic computer developed in Denmark known as GIER), one of the first compilers to deal fully and correctly with the language's powerful procedure mechanism.
"Dr. Naur's contribution was a watershed in the computing field, and transformed the way we define programming languages," said James Gray of Microsoft Research, and Chair of the 2005 Turing Committee. "Many of the programming constructs we take for granted today were introduced in the Algol Report, which introduced a concise block-structured language that improved the way we express algorithms."
Dr. Naur was instrumental in establishing software engineering as a discipline. He made pioneering contributions to methodologies for writing correct programs through his work on assertions that enable programmers to state their assumptions, and on structured programming. "His work, though formal and precise, displays an exceptional understanding of the limits and uses of formalism and precision," said Gray. Through these activities, and his development of an influential computer science curriculum, Dr. Naur contributed fundamental components of today's computing knowledge and skills.
Early Experience in Practical Calculations and Applications
Dr. Naur began his scientific pursuits as an astronomer, where he was involved in computations of the orbits of comets and minor planets. He obtained a magister of science degree (the equivalent of a master's degree) from Copenhagen University in 1949. He later returned there to earn a doctorate in astronomy in 1957. During the 1950-51 academic year, Dr. Naur studied astronomy at King's College in Cambridge, U.K., and came to the U.S. to further his work in the field. This work involved using early computers (starting with EDSAC, the world's first practical stored program electronic computer) for his astronomical calculations. In 1953, he returned to Denmark and served as a scientific assistant at Copenhagen Observatory.
In 1959, he joined the staff of the compiler design group at Regnecentralen, the first Danish computer company. There he organized the Algol Bulletin and was editor of the 13-person international Algol 60 team's report that defined Algol 60. He became a professor at the Copenhagen University Institute of Datalogy in 1969, retiring in 1998.
Dr. Naur was awarded the G. A. Hagemann Gold Medal from the Danish Technical University in 1963, the Jens Rosenkjaer Prize from the Danish Radio in 1966, and the Computer Pioneer Award from the Institute of Electrical and Electronics Engineers in 1986. ACM will present the Turing Award at the annual ACM Awards Banquet on May 20, 2006, at the Westin St. Francis Hotel in San Francisco, CA.
|
| About the A.M. Turing Award
The A.M. Turing Award was named for Alan M. Turing, the British mathematician who articulated the mathematical foundation and limits of computing, and who was a key contributor to the Allied cryptanalysis of the German Enigma cipher during World War II. Since its inception, the Turing Award has honored the computer scientists and engineers who created the systems and underlying theoretical foundations that have propelled the information technology industry. For additional information, please see the A. M. Turing Awards site.
About ACM
ACM, the Association for Computing Machinery (http://www.acm.org), is an educational and scientific society uniting the world's computing educators, researchers and professionals to inspire dialogue, share resources and address the field's challenges. ACM strengthens the profession's collective voice through strong leadership, promotion of the highest standards, and recognition of technical excellence. ACM supports the professional growth of its members by providing opportunities for life-long learning, career development, and professional networking.
|
|
posted @
2006-04-15 16:52 华剑缘 阅读(849) |
评论 (0) |
编辑 收藏