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:32

>>4
Maybe you should be using a real language instead.
http://docs.python.org/reference/datamodel.html#customizing-attribute-access

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