Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

[C++] OOP

Name: Snake4D 2012-04-14 5:20

Hello faggits,

I started to declare my getter and setter like this:

math_vector.h

class V2{
private:
    double coords[2];

public:

    V2();
    V2(double x, double y);
   
    double& x();
    double const& x() const ;
   
    double& y();
    double const& y() const ;
}

inline
double& V2::x() {return coords[0];}

inline
double const& V2::x() const {return coords[0];}

inline   
double& V2::y() {return coords[1];}

inline
double const& V2::y() const {return coords[1];}


so I can use:

V2 c(1.0, 2.0);
V2 d(3.0, 4.0);

c.x() = 15.;
c.y() = d.y();


my question:

1. does the inline method needs to be declared in the .h file or in the .cpp? If I declare it in the .cpp will be then compiled and will not be used by other file as an inline method?

2. I'm getting used to write the setter with the reference is it good?
or is it better to write like this?

//1.
double& x() {return _x;}
//2.
void x(double const& value) {_x = value;}


3. I provide many times both the getter and setter to have full access to the memebers, is it better to have them public?

4. If I want to print out something to the console I often overload the ostream operator <<. There is a little glitch that buggers me: if I declare it as a friend of my class I need to #include <iostream.h> instead of #include<iostream> which some time give problems with the namespaces. So right now I'm declaring it as an outside function and passing in the object as a const& but I cannot access the private members then... How do you deal with that?

Name: Anonymous 2012-04-14 10:19

using C.x() = 15; is more powerful than C.x = 15, but it is not quite as powerful as C.setX(15); C.x() could do something very fancy, like allocate and reserve a memory address for the x value. But it cannot affect in any way the value of 15 that will be assigned. C.setX(15) can do whatever it wants to the value 15, and store it where ever it wants. In this example 15 is just a number, but if it was a handle for large file or something, then C could make a hash of the file and store the hash or something, or maybe compress the file and return a decompressed version when C.x() is called. It all depends on what will work best in the end, but it is nice to have to option to explore these implementations without having to keep the client code up to date with your changes.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List