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: >>12 2012-04-14 20:50

but to be more clear, the pythonic way of implementing C.x = 6 would be c.__setattr__('x', 6). And then __setattr__ would need to examine the 'x' value for the name argument and use it to determine where to store the value of 6. When using a getter or a setter, the data type already knows which field your are getting or setting based upon which getter or setter you have called. It doesn't need to look up the value of a name argument at run time, which is what has to happen in python unless there is some difficult to guarantee optimization of inlining the __setattr__ method and then applying constant folding to the name argument.

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