メンバへのポインタ
メンバへのポインタ
C++
Student.h
#include <stdio.h>
#include "Student.h"int main(int argc, char* argv[])
{
Student Shibata("柴田望洋", 180, 80);
Student Masuyan("増田真二", 170, 60);Shibata.Print();
Masuyan.Print();
puts("");printf("身長差:%d\n", Shibata.Comph(Masuyan.Height()));
printf("体重差:%d\n", Shibata.Compw(Masuyan.Weight()));
puts("");Student::compare ptr = &Student::Comph;
printf("身長差:%d\n", (Shibata.*ptr)(Masuyan.Height()));ptr = &Student::Compw;
printf("体重差:%d\n", (Shibata.*ptr)(Masuyan.Weight()));
puts("");Student* P = &Masuyan;
printf("体重差:%d\n", (P->*ptr)(Shibata.Weight()));return 0;
}
Student.cpp
class Student
{
char name[20];
int height;
int weight;
public:
Student(const char*, int, int);char* Name(void) {return name;}
int Height(void) {return height;}
int Weight(void) {return weight;}int Comph(int h) {return height - h;}
int Compw(int w) {return weight - w;}void Print();
typedef
int (Student::*compare)(int);
};
#include <stdio.h>
#include <string.h>
#include "Student.h"Student::Student(const char* n, int h, int w)
{
strcpy(name, n);
height = h;
weight = w;
}void Student::Print()
{
printf("%s\n", name);
printf(" 身長 = %d\n", height);
printf(" 体重 = %d\n", weight);
}
実行結果
R:\>lesson006\project1.exe
柴田望洋
身長 = 180
体重 = 80
増田真二
身長 = 170
体重 = 60身長差:10
体重差:20身長差:10
体重差:20体重差:-20
Delphi
Student.pas
program Project1;{$APPTYPE CONSOLE}
uses
SysUtils, Student;procedure main();
var
Shibata: TStudent;
Masuyan: TStudent;
ptr: TCompare;
PMasuyan: ^TStudent;
begin
Shibata := TStudent.Create('柴田望洋', 180, 80);
Masuyan := TStudent.Create('増田真二', 170, 60);Shibata.Print;
Masuyan.Print;
Writeln('');Writeln(Format('身長差:%d', [Shibata.Comph(Masuyan.Height)]));
Writeln(Format('体重差:%d', [Shibata.Compw(Masuyan.Weight)]));
Writeln('');ptr := Shibata.Comph;
Writeln(Format('身長差:%d', [ptr(Masuyan.Height)]));ptr := Shibata.Compw;
Writeln(Format('体重差:%d', [ptr(Masuyan.Weight)]));
Writeln('');PMasuyan := @Masuyan;
ptr := PMasuyan^.Compw;
Writeln(Format('体重差:%d', [ptr(Shibata.Weight)]));Shibata.Free;
Masuyan.Free;
end;begin
main;
end.
unit Student;interface
uses
SysUtils;type
TStudent = class
private
m_name: String;
m_height: Integer;
m_weight: Integer;
public
constructor Create(n:String; h:Integer; w:Integer);function name():String;
function Height():Integer;
function Weight():Integer;function Comph(h:Integer):Integer;
function Compw(w:Integer):Integer;procedure Print();
end;TCompare = function (x:Integer):Integer of object;
implementation
constructor TStudent.Create(n:String; h:Integer; w:Integer);
begin
m_name := n;
m_height := h;
m_weight := w;
end;function TStudent.name():String;
begin
result := m_name;
end;function TStudent.Height():Integer;
begin
result := m_height;
end;function TStudent.Weight():Integer;
begin
result := m_weight;
end;function TStudent.Comph(h:Integer):Integer;
begin
result := m_height - h;
end;function TStudent.Compw(w:Integer):Integer;
begin
result := m_weight - w;
end;procedure TStudent.Print();
begin
Writeln(Format('%s', [m_name]));
Writeln(Format(' 身長= %d', [m_height]));
Writeln(Format(' 体重= %d', [m_weight]));
end;
end.
実行結果
S:\>lesson006\project1.exe
柴田望洋
身長= 180
体重= 80
増田真二
身長= 170
体重= 60身長差:10
体重差:20身長差:10
体重差:20体重差:-20