Stroustrup先生将如何实施他的解决方案12.7 [2] [英] How would Mr. Stroustrup implement his solution to 12.7[2]

查看:36
本文介绍了Stroustrup先生将如何实施他的解决方案12.7 [2]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在学习标准C ++作为业余爱好。 C ++编程语言:

特别版一直是我信息的主要来源。我阅读了这本书的全部内容,并得出结论说我没有尝试并完成练习,因此我已经完成了自己的伤害。

我打算纠正这个问题。


我目前的例行程序是阅读章节,然后尝试每一个练习

问题,我发现这个过程导致更好的理解

的语言和实现。


然而,偶尔我无法完成一些练习,或者不是

对我的解决方法感到满意。一个这样的练习是12.7 [2]。


从我在本章和他的几篇论文中读到的,Stroustrup先生

表明大多数Shape,Dot,

Line,Rectangle层次结构的最佳实现时间将是Shape的纯粹抽象基础

类,以及Dot,Line,Rectangle来自Shape的inheirit并实现

虚函数。在他的代码示例中,他经常定义虚拟空白

draw()= 0作为Shape类的成员。在实现了
12.7 [2]的解决方案后,我遇到了如何为必须存在的Window类实现绘图形状的难度。我的实现是使用std :: cout进行简单的控制台模式字符输出
$ b $。


因为我是OO编程的新手,我觉得我缺乏一些理解

他打算让我的解决方案类似于什么。我已经读过很好的

来保持单独的想法,分开。并且,使用这个想法,我遇到了如何使用Dots,Lines,Rectangles,绘制自己的难度,因为

他们需要访问渲染到屏幕,但渲染

必须控制,因为我使用的是控制台模式字符输出。另外,从读取12.7 [5]看
,似乎形状的渲染需要由中间对象控制,以便可以执行裁剪。 />
我对如何以面向对象的正确方式完成本练习感到茫然。我希望了解如何以最好的方式实现C ++,这就是为什么我对目前的

理解不满意。如果您有时间和经验,请分享给您

这方面的知识。


以下是我用来完成12.7的基本设计[2] ],但我觉得

是一个非常糟糕的设计,不应该被接受作为解决方案。我希望有经验的程序员可以更好地理解我的问题,并且,或许,b $ b或者提供更好的解决方案。只显示Point,Shape,Dot和

类窗口,因为它们的交互与Line的交互相同,并且

矩形。


(这是我的第一篇文章,我不知道如何在我的解决方案中包含一个压缩文件

,所以请接受这个。)


形状保持纯粹抽象,但我添加了虚拟空白绘制(Window *

const)const = 0;。我添加了这个,因为在实现时我遇到了

问题,即形状如何能够访问一个退回到

屏幕的方法。会发生什么是一个Window对象w,它的draw(Shape *)

成员被调用。 Window :: draw(Shape *)然后传递它的this。指向

" Shape :: draw(Window *)" ;.然后,Shape :: draw成员使用Window指针

来访问Window :: put_pixel(Point,char)。 Window :: put_pixel(Point,char)

然后添加一对< Point,char>到一个向量对象,在调用Window :: show()时使用

。该向量用作一种缓冲区,用于绘制

形状,以便可以正确绘制多个形状,因为我使用控制台模式和cout来获取
渲染角色。


要绘制一些东西,将使用以下内容:


窗口w(10,10);

w.draw(Dot(Point(5,5));

w.show();


// ***** ********************** *

// Point.h

//这是一个结构因为Stroustrup先生建议使用隐藏成员的类

//和访问成员应该只有在执行不变量时才使用
//和Point

//没有不变量。

// **************************

#ifndef POINT_H

#define POINT_H


struct Point {

Point():x( 0),y(0){}

Point(int i,int j):x(i),y(j){}


int x,y;

};


#endif


// ******** ******** ***********

// Shape.h

// Stroustrup先生通常声明

//虚拟void draw()= 0;

//我的设计必须传递一个Window对象。

// ************** *************

#ifndef SHAPE_H

#define SHAPE_H


#include" ; point.h"


类窗口;


struct Shape {

虚拟点n()const = 0;

虚拟点e()const = 0;

虚拟点s()const = 0;

虚拟点w()const = 0;

虚拟点ne()const = 0;

虚拟点nw()const = 0;

虚拟点se() const = 0;

虚拟点sw()const = 0;

虚拟~Shape(){}


虚拟空draw(Window * const)const = 0;

};


#endif


// ** ****************************

// Dot.h

/ / ****************************** *

#ifndef DOT_H

#限定DOT_H


#include" point.h"

#include" shape.h"


class窗口;


class Dot:public Shape {

public:

Dot();

Dot(const Point&);


Point n()const;

Point e()const;

Point s( )const;

Point w()const;

Point ne()const;

Point nw()const;

Point se()const;

Point sw()const;


void draw(Window * const)const;


私人:

点p_;

};


//内联定义

inline Point Dot :: n()const {return p _; }


内联点Dot :: e()const {return p_; }


内联点Dot :: s()const {return p_; }


内联点Dot :: w()const {return p_; }


内联点Dot :: ne()const {return p_; }


内联点Dot :: nw()const {return p_; } $ / $

内联点Dot :: se()const {return p_; }


内联点Dot :: sw()const {return p_; }


#endif


// ******************** br $>
// Dot.cpp

// ********************

#include" dot.h"

#include" window.h"


Dot :: Dot(){}


Dot :: Dot(const Point& p)

:p_(p)

{

}

void Dot :: draw(Window * const w)const

{

w-> put_pixel(p _,''*' ');

}


// ******************** ********

// Window.h

// ******************* *********

#ifndef WINDOW_H

#define WINDOW_H


#include" point.h" ;

#include" utility"

#include" vector"


class Shape;


类窗口{

public:

Window(const int,const int);


void draw (const Shape&);

Point current()const;

void current(const Po int&);

void put_pixel(const Point&,const char&);

void show()const;

void clear() ;


私人:

int w_,h_;

点c_;

std :: vector< std :: pair< Point,char> >积分;

};


#endif


// ****** *******************

// Window.cpp

// ******** *****************

#include" window.h"

#include" shape.h"

#include< iostream>


Window :: Window(const int x,const int y)

:w_(x ),h_(y){}

void Window :: draw(const Shape& s)

{

s。 draw(this);

c_ = s.se();

}


Point Window :: current()const

{

返回c_;

}


void Window :: current(const Point& c)

{

c_ = c;

}


void Window :: put_pixel( const Point& p,const char& c)

{

points.push_back(std :: make_pair(p,c));

}


//这会在向量中搜索窗口的每个字符位置。

//如果找到一个字符,它就会被写入进入屏幕。

//如果找不到字符,屏幕上会写一个空格。

void Window :: show()const

{

for(int j = 0; j< = h_; j ++){

for(int i = 0; i< = w_; i ++){

typedef std :: vector< std :: pair< Point,char> > :: const_iterator CI;

CI k;

for(k = points.begin(); k!= points.end(); ++ k){

if(k-> first.x == i&& k-> first.y == j){

std :: cout< < k->秒;

休息;

}

}

if(k == points.end ())std :: cout<< '''';

}

std :: cout<< ''\ n'';

}

}


void Window :: clear()

{

points.clear();

}

Hi,

I am learning standard C++ as a hobby. The C++ Programming Language :
Special Edition has been the principal source for my information. I read the
entirety of the book and concluded that I had done myself a disservice by
having not attempted and completed the exercises. I intend to rectify that.

My current routine is to read a chapter and then attempt every exercise
problem, and I find that this process is leading to a greater understanding
of the language and implementation.

However, occasionly I am unable to complete some of the exercises, or am not
satisfied with my method of solution. One such exercise is 12.7[2].

From what I read in the chapter and a few papers of his, Mr. Stroustrup
suggests that most of the time the best implemention for the Shape, Dot,
Line, Rectangle hierarchy would be for Shape to be a purely abstract base
class, and for Dot, Line, Rectangle to inheirit from Shape and implement the
virtual functions. In his code examples he routinely defines "virtual void
draw() = 0" as a member of the Shape class. Upon implementing a solution to
12.7[2] I experienced a difficulty surrounding how to implement the drawing
of Shapes for the Window class that must exist. My implementation was for
simple console mode character output using std::cout.

Since I am new to OO programming, I feel that I lack some understanding of
what he intended my solution to be similar to. I have read that it is good
to keep separate ideas, separate. And, using this idea, I run into
difficulty on how to have Dots, Lines, Rectangles, draw themselves, since
they need access to a method of rendering to the screen, but the rendering
must be controlled because I am using console mode character output. Also,
from reading 12.7[5], it seems that the rendering of the shapes needs to
controlled by an intermediary object so that the clipping can be performed.
I am at a loss as to how to complete this exercise in a properly
object-oriented fashion. I wish to understand how to implement C++ in the
best possible way, and that is why I am not satisfied with my current
understanding. If you have the time, and the experience, please share you
knowledge in this matter.

The following is my basic design used to complete 12.7[2], but what I feel
is a very poor design and should not be accepted as a solution. I offer it
in hopes that experienced programmers may better understand my problem, and,
perhaps, offer a better solution. Only the classes Point, Shape, Dot, and
Window will be shown, as their interaction is the same as that for Line, and
Rectangle.

(This is my first post, and I''m not sure how to include a compressed file of
my solution, so please accept this.)

Shape was kept purely abstract, but I added "virtual void draw(Window*
const) const = 0;". I added this because upon implementation I ran into the
problem of how the shapes would be able to access a method of rending to the
screen. What happens is that a Window object, w, has its "draw(Shape*)"
member called. Window::draw(Shape*) then passes its "this" pointer to
"Shape::draw(Window*)". The Shape::draw member then uses the Window pointer
to access Window::put_pixel(Point, char). Window::put_pixel(Point, char)
then adds a pair<Point, char> to a vector object, which is used when
Window::show() is called. The vector is used as a sort of buffer for the
shapes to be drawn to so that multiple shapes can be drawn correctly since I
am using a console mode and cout to render characters.

To draw something, the following would be used:

Window w(10,10);
w.draw(Dot(Point(5,5));
w.show();

//***************************
// Point.h
// This is a structure because Mr. Stroustrup suggests that classes
// with hidden members, and access members should only be
// used when there is an invariant to enforce, and Point
// does not have an invariant.
// **************************
#ifndef POINT_H
#define POINT_H

struct Point {
Point() :x(0), y(0) { }
Point(int i, int j) :x(i), y(j) { }

int x, y;
};

#endif

// ***************************
// Shape.h
// Mr. Stroustrup usually declares
// virtual void draw() = 0;
// My design has to pass a Window object.
// ***************************
#ifndef SHAPE_H
#define SHAPE_H

#include "point.h"

class Window;

struct Shape {
virtual Point n() const = 0;
virtual Point e() const = 0;
virtual Point s() const = 0;
virtual Point w() const = 0;
virtual Point ne() const = 0;
virtual Point nw() const = 0;
virtual Point se() const = 0;
virtual Point sw() const = 0;
virtual ~Shape() {}

virtual void draw(Window* const) const = 0;
};

#endif

// ******************************
// Dot.h
// ******************************
#ifndef DOT_H
#define DOT_H

#include "point.h"
#include "shape.h"

class Window;

class Dot : public Shape {
public:
Dot();
Dot(const Point&);

Point n() const;
Point e() const;
Point s() const;
Point w() const;
Point ne() const;
Point nw() const;
Point se() const;
Point sw() const;

void draw(Window* const) const;

private:
Point p_;
};

// Inline definitions
inline Point Dot::n() const { return p_; }

inline Point Dot::e() const { return p_; }

inline Point Dot::s() const { return p_; }

inline Point Dot::w() const { return p_; }

inline Point Dot::ne() const { return p_; }

inline Point Dot::nw() const { return p_; }

inline Point Dot::se() const { return p_; }

inline Point Dot::sw() const { return p_; }

#endif

// ********************
// Dot.cpp
// ********************
#include "dot.h"
#include "window.h"

Dot::Dot() {}

Dot::Dot(const Point& p)
: p_(p)
{
}

void Dot::draw(Window* const w) const
{
w->put_pixel(p_, ''*'');
}


// ****************************
// Window.h
// ****************************
#ifndef WINDOW_H
#define WINDOW_H

#include "point.h"
#include "utility"
#include "vector"

class Shape;

class Window {
public:
Window(const int, const int);

void draw(const Shape&);
Point current() const;
void current(const Point&);
void put_pixel(const Point&, const char&);
void show() const;
void clear();

private:
int w_, h_;
Point c_;
std::vector<std::pair<Point, char> > points;
};

#endif


// *************************
// Window.cpp
// *************************
#include "window.h"
#include "shape.h"
#include <iostream>

Window::Window(const int x, const int y)
: w_(x), h_(y) { }

void Window::draw(const Shape& s)
{
s.draw(this);
c_ = s.se();
}

Point Window::current() const
{
return c_;
}

void Window::current(const Point& c)
{
c_ = c;
}

void Window::put_pixel(const Point& p, const char& c)
{
points.push_back(std::make_pair(p, c));
}

// This searches the vector for each character position of the window.
// If a character is found, it is written to the screen.
// If a character is not found, a space is written to the screen.
void Window::show() const
{
for (int j=0; j <= h_; j++) {
for (int i=0; i <= w_; i++) {
typedef std::vector<std::pair<Point,char> >::const_iterator CI;
CI k;
for (k=points.begin(); k != points.end(); ++k) {
if (k->first.x == i && k->first.y == j) {
std::cout << k->second;
break;
}
}
if (k == points.end()) std::cout << '' '';
}
std::cout << ''\n'';
}
}

void Window::clear()
{
points.clear();
}

推荐答案

Oplec< ;一个******* @ anonymous.com>在消息中写道

新闻:FM ******************* @ twister01.bloor.is.net。 cable.rogers.com ...
Oplec <an*******@anonymous.com> wrote in message
news:FM*******************@twister01.bloor.is.net. cable.rogers.com...


我正在学习标准C ++作为业余爱好。 C ++编程语言:
特别版一直是我信息的主要来源。我读了整本书的
,并得出结论说我没有尝试并完成练习,这对我自己造成了伤害。我打算纠正
那个。
我当前的例行程序是读一章然后尝试每一个练习
问题,我发现这个过程导致了更多
的理解语言和实现。

然而,偶尔我无法完成一些练习,或者我的b $ b不满意我的解决方法。一个这样的练习是12.7 [2]。

从我在本章和他的一些论文中看到的,Stroustrup先生
建议大多数时候是形状的最佳实现,Dot,
Line,Rectangle层次结构将Shape用作纯粹的抽象基类,并且对于Dot,Line,Rectangle来自Shape的inheirit并实现
的虚函数。在他的代码示例中,他通常定义虚拟空白
draw()= 0。作为Shape类的成员。在实现解决方案
到12.7 [2]时,我遇到了如何为必须存在的Window类实现Shapes的
绘图的困难。我的实现是使用std :: cout进行简单的控制台模式字符输出。

由于我是OO编程的新手,我觉得我对
他的意图缺乏了解我的解决方案类似于。我已经读到,保持单独的想法是分开的很好。并且,使用这个想法,我遇到了如何使用点,线,矩形,绘制自己的难度,因为他们需要访问渲染到屏幕的方法,但渲染
必须控制,因为我使用控制台模式字符输出。另外,从阅读12.7 [5]开始,似乎形状的渲染需要由中间对象控制,以便裁剪可以执行
。我不知道如何以适当的面向对象的方式完成这项练习。我希望了解如何以最佳方式实现C ++,这就是为什么我对目前的理解不满意。如果你有时间和经验,请分享你这方面的知识。

以下是我用来完成12.7 [2]的基本设计,但我感觉如何/>是一个非常差的设计,不应该被接受为解决方案。我提供它
希望有经验的程序员可以更好地理解我的问题,
,或许,提供更好的解决方案。只会显示Point,Shape,Dot和
类窗口,因为它们的交互与Line,
和Rectangle的交互相同。

(这是我的第一个发帖,我不知道如何在我的解决方案中包含一个压缩文件
,所以请接受这个。)

形状保持纯粹抽象,但我添加了虚拟虚空 draw(Window *
const)const = 0;"。我添加了这个,因为在实现时我遇到了
形状如何能够访问屏幕上的
的方法的问题。


这是合理的。在真实的图形系统中,你希望能够在不同的设备上渲染一张

的图纸 - 任何窗口,打印机,绘图仪等等,但是你不需要b / b
我希望将形状对象本身绑定到一个设备上。

传入设备进行绘制是一个好主意。

会发生什么是窗口对象w具有其draw(Shape *)成员调用。 Window :: draw(Shape *)然后传递它的this。指向
Shape :: draw(Window *)的指针。


我不认为这是个好主意。由于Shape对象正在执行

绘图,因此Window类不需要了解Shapes。只需

将Window对象的引用传递给Shape'的draw(),并且特定形状中的

重写函数将在Window上绘制。

然后Shape :: draw成员使用Window指针
来访问Window :: put_pixel(Point,char)。 Window :: put_pixel(Point,char)
然后添加一对< Point,char>到一个向量对象,在调用Window :: show()时使用。向量用作一种缓冲区,用于绘制形状,以便可以正确绘制多个形状,因为
我使用控制台模式和cout来渲染字符。


这对我来说听起来都不错。

为了画一些东西,会使用以下内容:

Window w(10, 10);
w.draw(Dot(Point(5,5));
w.show();


我会:


窗口w(10,10);

点点(点(5,5));

dot.draw(w) ;


[大多数代码剪断]


代码注释:void draw(Window * const)const;
窗口(const int,const int);
Hi,

I am learning standard C++ as a hobby. The C++ Programming Language :
Special Edition has been the principal source for my information. I read the entirety of the book and concluded that I had done myself a disservice by
having not attempted and completed the exercises. I intend to rectify that.
My current routine is to read a chapter and then attempt every exercise
problem, and I find that this process is leading to a greater understanding of the language and implementation.

However, occasionly I am unable to complete some of the exercises, or am not satisfied with my method of solution. One such exercise is 12.7[2].

From what I read in the chapter and a few papers of his, Mr. Stroustrup
suggests that most of the time the best implemention for the Shape, Dot,
Line, Rectangle hierarchy would be for Shape to be a purely abstract base
class, and for Dot, Line, Rectangle to inheirit from Shape and implement the virtual functions. In his code examples he routinely defines "virtual void
draw() = 0" as a member of the Shape class. Upon implementing a solution to 12.7[2] I experienced a difficulty surrounding how to implement the drawing of Shapes for the Window class that must exist. My implementation was for
simple console mode character output using std::cout.

Since I am new to OO programming, I feel that I lack some understanding of
what he intended my solution to be similar to. I have read that it is good
to keep separate ideas, separate. And, using this idea, I run into
difficulty on how to have Dots, Lines, Rectangles, draw themselves, since
they need access to a method of rendering to the screen, but the rendering
must be controlled because I am using console mode character output. Also,
from reading 12.7[5], it seems that the rendering of the shapes needs to
controlled by an intermediary object so that the clipping can be performed. I am at a loss as to how to complete this exercise in a properly
object-oriented fashion. I wish to understand how to implement C++ in the
best possible way, and that is why I am not satisfied with my current
understanding. If you have the time, and the experience, please share you
knowledge in this matter.

The following is my basic design used to complete 12.7[2], but what I feel
is a very poor design and should not be accepted as a solution. I offer it
in hopes that experienced programmers may better understand my problem, and, perhaps, offer a better solution. Only the classes Point, Shape, Dot, and
Window will be shown, as their interaction is the same as that for Line, and Rectangle.

(This is my first post, and I''m not sure how to include a compressed file of my solution, so please accept this.)

Shape was kept purely abstract, but I added "virtual void draw(Window*
const) const = 0;". I added this because upon implementation I ran into the problem of how the shapes would be able to access a method of rending to the screen.
That''s reasonable. In a real graphics system you want to be able to render a
drawing on different devices - any window, a printer, a plotter etc., but
you don''t want the shape object itself to be tied down to a single device.
Passing in the device to draw on is therefore a good idea.
What happens is that a Window object, w, has its "draw(Shape*)"
member called. Window::draw(Shape*) then passes its "this" pointer to
"Shape::draw(Window*)".
I don''t think this is such a good idea. Since the Shape object is doing the
drawing, there''s no need for the Window class to know about Shapes. Just
pass a reference to the Window object to the Shape''s draw(), and the
overridden function in the specific shape will draw itself on the Window.
The Shape::draw member then uses the Window pointer
to access Window::put_pixel(Point, char). Window::put_pixel(Point, char)
then adds a pair<Point, char> to a vector object, which is used when
Window::show() is called. The vector is used as a sort of buffer for the
shapes to be drawn to so that multiple shapes can be drawn correctly since I am using a console mode and cout to render characters.
That all sounds good to me.
To draw something, the following would be used:

Window w(10,10);
w.draw(Dot(Point(5,5));
w.show();
I would have:

Window w(10,10);
Dot dot(Point(5,5));
dot.draw(w);

[most code snipped]

One comment on the code: void draw(Window* const) const;
Window(const int, const int);




你可以根据需要设置你的函数参数const,但这并不常见

参数是按值传递的,所以函数有自己的

副本。它不能帮助一个函数的调用者知道

函数是不是不会修改自己传递给它的副本。

来电者不在乎。


如果你没有'以前做过OO并且你没有C ++经验,我想

你做得很好。


DW



You can make your function parameters const if you want, but it''s not usual
or necessary. Parameters are passed by value, so functions have their own
copies of them. It doesn''t help the caller of a function know that a
function isn''t going to modify its own copy of what''s passed to it. The
caller doesn''t care.

If you haven''t done OO before and you are inexperienced in C++, I think
you''re doing pretty well.

DW


-----原帖-----

来自:David White < no@email.provided>

新闻组:comp.lang.c ++

发送时间:2003年10月21日星期二下午8:40

主题:Re:Stroustrup先生将如何实现他的解决方案12.7 [2]

----- Original Message -----
From: "David White" <no@email.provided>
Newsgroups: comp.lang.c++
Sent: Tuesday, October 21, 2003 8:40 PM
Subject: Re: How would Mr. Stroustrup implement his solution to 12.7[2]

会发生什么是Window对象, w,将其draw(Shape *)成员调用。 Window :: draw(Shape *)然后传递它的this。指向
Shape :: draw(Window *)。
我不认为这是个好主意。由于Shape对象正在执行
What happens is that a Window object, w, has its "draw(Shape*)"
member called. Window::draw(Shape*) then passes its "this" pointer to
"Shape::draw(Window*)".
I don''t think this is such a good idea. Since the Shape object is doing



绘图,因此Window类不需要了解Shapes。只需将Window对象的引用传递给Shape的draw(),并且特定形状的
重写函数将在Window上绘制。


Window类必须有一个draw()成员,因为这个成员是

包含在问题12.7的最终实现的示例使用中[2] ]。

他表示应该使用一个Window w,如下所示:

w.draw(Circle(w.current(),10));所以,很明显,他打算让解决方案'

Window类有一个draw()成员,它接受一个指向Shape类的指针。

我完全理解你的想法并且也会这样做,但是我希望以他预期的方式完成这个。我对我的设计的一个主要的反对意见是增加了Shape :: draw(Window *),因为在

中我看到围绕Shape示例的所有示例,Shape只有

有一个纯虚拟的Shape :: draw(),这就是为什么我觉得我的解决方案应该不接受
。我常常想知道他是不是要让Window类使用每个Shape对象提供的
迭代器来获得Shape的形状;

即,字符类型和要在屏幕上呈现的位置。截至

这篇文章,我还没有阅读过Shapes提供的任何内容

迭代器。


他使用的例子如下所示说明这个

例子的正确设计:

(src: http://technetcast.ddj.com/str0237/bs-millennium.pdf


class Shape {

public:

virtual void draw()= 0;

virtual void rotate(double)= 0;

};


class Common {Color c; / * ... * /};


class Circle:public Shape,protected Common {/ * ... * /};

尽可能看,他的Shape'的draw()成员不会采取任何行动。这是一个困难的方面让我对我的设计产生了担忧,因为我觉得

必须有一个他打算使用的优雅设计。我添加了

,Shape :: draw()接受一个Window对象是我解决12.7 [2]的方法,

同时遵守要求他们是Window :: draw (形状&);


(注意,我的意思是'不是形状和之前写的形状*)。


我真的希望理解正确的OO设计,并且不希望继续我的学习,直到我理解了一个非常简单的问题。


你可以使你的功能参数const如果你想要,但它不是通常或必要的
。参数按值传递,因此函数有自己的副本。它不会帮助函数的调用者知道
函数不会修改它自己的副本传递给它。
来电者不在乎。


您的评论很有意思。在TC ++ PL中,Stroustrup先生使用非常量int对于const int的参数使用
,但我认为他这样做是为了某些原因我没有了解到的却是使用const int;

是,使用const传递的值参数。我这样做是因为我想要

来确保在我的函数定义中我不会修改传递的原始

值。我知道更改值不会影响调用者,但我这样做是为了让我使用变量不会改变它们。如果这不是一个好主意,请说明原因。我希望正确地做这些事情。

如果你之前没有做过OO并且你没有C ++经验,我想你好吗?做得很好。

DW


the drawing, there''s no need for the Window class to know about Shapes. Just
pass a reference to the Window object to the Shape''s draw(), and the
overridden function in the specific shape will draw itself on the Window.
The Window class has to have a draw() member because such a member is
included in the example use of the final implementation of problem 12.7[2].
He shows that a Window, w, should be used as in the following:
w.draw(Circle(w.current(),10)); So, clearly, he intends for the solutions''
Window class to have a draw() member that takes a pointer to a Shape class.
I completely understand your idea, and would do it that way as well, but I
wish to complete this the way he intended it to be. One of my main
objections to my design was the addition of Shape::draw(Window*) because in
all the examples I have seen surrounding the Shape example, Shape only ever
has a pure virtual Shape::draw(), and that is why I felt my solution should
not be accepted. I often wondered if he mean''t for the Window class to use
iterators supplied by each Shape object in order to get the Shape''s shape;
that is, the character types, and position to be rendered on screen. As of
this writing, I have yet to read anything of his that had Shapes provide
iterators.

He uses examples like the following when illustrating proper design for this
example:
(src: http://technetcast.ddj.com/str0237/bs-millennium.pdf)

class Shape {
public:
virtual void draw() = 0;
virtual void rotate(double) = 0;
};

class Common { Color c; /* ... */ };

class Circle : public Shape, protected Common { /* ... */ };
As you can see, his Shape''s draw() member does not take anything. This is
the difficult aspect causing me the concern with my design, as I feel that
there must be an elegant design that he had intended to be used. My adding
that Shape::draw() take a Window object was my method of solving 12.7[2],
while adhering to the requirement their be Window::draw(Shape&);

(Note, I mean''t Shape& previously when writing Shape*).

I truly wish to understand proper OO design, and do not wish to continue my
study until I have understood what should be a very simple problem.

You can make your function parameters const if you want, but it''s not usual or necessary. Parameters are passed by value, so functions have their own
copies of them. It doesn''t help the caller of a function know that a
function isn''t going to modify its own copy of what''s passed to it. The
caller doesn''t care.
Your comment is interesting. In TC++PL, Mr. Stroustrup uses non-const ints
for parameters over const ints, but I thought he was doing that for some
reason that I had not learned about yet and so was using "const int"; that
is, using const on parameters passed by value. I did this because I wanted
to make sure in my function definitions that I would not modify the original
values that were passed. I understand that changing the values would not
affect the callers, but I did it so that my use of the variables would not
alter them. If that is not a good idea, please indicate why. I wish to do
such things correctly.
If you haven''t done OO before and you are inexperienced in C++, I think
you''re doing pretty well.

DW




非常感谢您的反馈,因为我发现它最有帮助!我很高兴一个经验丰富的人认为我做得很好,因为我没有和朋友谈过这个问题,因此我只对自己有所了解。没有

让任何人就C ++问题进行沟通可能很困难。

次。



Thank you very much for your feedback, as I found it most helpful! I am
glad a more experienced person thinks I am doing alright because I have not
talked to anyone about this and so had only my own opinion of myself. Not
having anyone to communicate with in regards to C++ can be difficult at
times.


周三, 2003年10月22日01:25:48 GMT,Oplec <一个******* @ anonymous.com>写道:
On Wed, 22 Oct 2003 01:25:48 GMT, "Oplec" <an*******@anonymous.com> wrote:
他在说明这个例子的正确设计时使用了如下例子:
(src: http://technetcast.ddj.com/str0237/bs-millennium.pdf

class Shape {
public void
= 0;
虚拟void rotate(double)= 0;
};
class Common {Color c; / * ... * /};

类Circle:public Shape,protected Common {/ * ... * /};

如你所见,他的形状''draw()成员不接受任何东西。这是一个困难的方面,让我对我的设计产生了担忧,因为我觉得必须有一个他打算使用的优雅设计。


不是很优雅,但有各种解决方案_given_界面

以上 - 但我不认为Bjarne意味着要遵循

字母,只是在精神上。


将绘图表面对象传递给''draw''函数的想法是

可能是最优雅和最自然的解决方案。我会通过引用传递它,

不作为指针,因为指针可以为NULL。我将它作为一个

抽象界面传递,而不是作为一个具体的窗口,但这只是细节。


如果你想避免使用''draw''参数,你可以简单地将你的

对象连接到绘图表面(或窗口,如果你想要的话)。这不是很优雅,但很有效。例如,将绘图表面对象传递给''Circle''

或''Line''或任何构造函数,让每个对象保持一个引用。或者

允许更动态的附件和分离。虽然这很丑陋,但是b $ b充满了危险。

我的添加
Shape :: draw()取一个Window对象是我的解决方法12.7 [2],
同时遵守要求他们是Window :: draw(Shape&);
He uses examples like the following when illustrating proper design for this
example:
(src: http://technetcast.ddj.com/str0237/bs-millennium.pdf)

class Shape {
public:
virtual void draw() = 0;
virtual void rotate(double) = 0;
};

class Common { Color c; /* ... */ };

class Circle : public Shape, protected Common { /* ... */ };
As you can see, his Shape''s draw() member does not take anything. This is
the difficult aspect causing me the concern with my design, as I feel that
there must be an elegant design that he had intended to be used.
Not very elegant, but there are various solutions _given_ the interface
above -- but I don''t think Bjarne meant for that to be followed to the
letter, just in spirit.

The idea of passing a drawing surface object to the ''draw'' function is
probably the most elegant and natural solution. I''d pass that by reference,
not as a pointer, because a pointer can be NULL. And I''d pass that as an
abstract interface, not as a concrete ''Window'', but that''s just details.

If you want to avoid an argument to ''draw'' you can simply hook up your
object to the drawing surface (or window, if you want). That''s not very
elegant, but works. E.g., pass the drawing surface object in to the ''Circle''
or ''Line'' or whatever constructor, let each object keep a reference. Or
allow for more dynamic attachment and detachment. Although that''s ugly and
fraught with dangers.
My adding
that Shape::draw() take a Window object was my method of solving 12.7[2],
while adhering to the requirement their be Window::draw(Shape&);




如果你发布了那个文本会有帮助行使。我们并非所有人都有最近一期的TCPPPL版本。我是从1987年开始的,我认为(我确实有更多的

最近版本,但是有人在我不知情的情况下借了它......)。


那说,我能给出的最好建议就是相信自己的直觉。是的,

Bjarne是上帝。不,他不会在这里和那里犯一个小错误

(如果他那时候就没有勘误列表......)。因此,当你应该遵循

文本的精神时,请不要紧跟

。甚至 - 遵循自己的想法和逻辑,无论它走到哪里。


Hth。



It would help if you posted the text of that exercise. Not all of us have
a recent edition of TCPPPL. Mine is from 1987, I think (I did have a more
recent edition, but someone borrowed it without my knowledge...).

That said, the best advice I can give is to trust your instincts. Yes,
Bjarne is God. And no, he''s not above making a small mistake here and there
(if he were then there would be no errata list...). So don''t get stuck on
following the letter of the text when you should be following the spirit of the
text. Or even -- following your own ideas and logic to wherever it leads.

Hth.


这篇关于Stroustrup先生将如何实施他的解决方案12.7 [2]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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