POV-Ray : Newsgroups : povray.programming : Vectors in C++ : Re: Vectors in C++ Server Time
6 Oct 2024 13:51:43 EDT (-0400)
  Re: Vectors in C++  
From: Warp
Date: 17 Dec 2002 05:14:12
Message: <3dfef8f4@news.povray.org>
Micha Riser <mri### [at] gmxnet> wrote:
>> 1) Why do you make the array public?

> The vector is a really basic class which needs to be manipulated 
> "low-level" by many other classes. To allow this I made the array public. 
> If I made it private I would have to add a public method to read and write 
> the elements of the vector anyways. But access could be made a bit more 
> elegant using the '[]' operator.

  Sticking to modularity principles, even though it might feel sometimes
like useless, is usually a good thing.

  By making the implementation of your vector public you are fixing this
implementation. This means that you will not be able to change the
implementation to something better later.
  Granted, in this specific case it's improbable that the implementation
will ever change, but leaving the possibility open never hurts.

  Just look at the std::vector class. It has mostly abstracted its internal
implementation, yet you still can use it largely as if it was a C array.
You can index it using the [] operator. Even though it's not the language's
internal [] operator but it's overloaded, with a current compiler it's
probably not any slower.
  (The reason why I used the word "mostly" above is too long to explain
here...)

  There are some programming guidelines which suggest that if you are
going to make a conglomeration of variables instead of an abstract type,
you should use 'struct' instead of 'class' for clarity. This is a kind
of "documentation" style of the source code.

> I am not really a friend of overloading of + and -. They hide what is 
> really going on when using them.

  There's no difference between "operator+(a,b)" and "function(a,b)".

  Moreover, if you want to only make possible to add and assign at the
same time, use operator+=().

-- 
#macro M(A,N,D,L)plane{-z,-9pigment{mandel L*9translate N color_map{[0rgb x]
[1rgb 9]}scale<D,D*3D>*1e3}rotate y*A*8}#end M(-3<1.206434.28623>70,7)M(
-1<.7438.1795>1,20)M(1<.77595.13699>30,20)M(3<.75923.07145>80,99)// - Warp -


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.