Ken said:
I am familiar with C and Java, I would like to use a style that I have
become accustomed to in Java with C++ but each time I do this I have
name conflict.
Well, then you just have to learn to write C++ the way C++ programmers
do it and not try to write Java in C++.
I'm familiar with Common Lisp, and everything else looks like crap.
I suck it up!
In the past I have just worked around it by using
different names can anyone tell me how to get the following to work:
I want to have a class named v3d (vector 3d), it has the following
private instance variables...
float x, y, z;
It has the following public get and set methods:
//set methods
void x(float x);
void y(float y);
void z(float z);
//get methods
float x(void);
float y(void);
flaot z(void);
There is a roundabout way to do something better. Instead of using the
float type directly, you can create a template class. Instances of that
class behave like objects of the template argument type. they
automatically convert to the type, and you can assign that type to
them.
But the template class has two additional parameters: namely, pointers
to member functions to get and set the value. The template also takes a
pointer to the object as a parameter, so it has an object to invoke
these functions against.
Use of this "SmartMember" template class might look like this:
class MyClass {
public:
SmartMember<MyClass, int> x;
public:
MyClass() : x(this, &MyClass::get_x, &MyClass::set_x) { }
private:
int get_x();
void set_x(const int &);
};
The SmartMember<int> object x stores a pointer to the MyClass instance,
and pointers to two member functions for setting and getting. It has
overloaded operators to do the magic. You can assign int values to x,
or values that can implicitly convert to int. These values get passed
to the set_x() function. You can also explicitly convert x to int,
which will also happen implicitly in many contexts, like function
argument expressions.
MyClass obj;
obj.x = 3; // calls obj.set_x(3);
int y = obj.x; // calls obj.get_x();
I will leave SmartMember<int> as an exercise for the reader. It's
pretty easy. It goes something like this:
template <class CLASS, class TYPE>
class SmartMember {
private:
void (CLASS::* set_func)(const TYPE &);
TYPE (CLASS::* get_func)();
CLASS *obj;
public:
SmartMember(CLASS *o, void (CLASS::* sf)(const TYPE &), TYPE
(CLASS::*gf)())
: set_func(sf), get_func(gf), obj(o) { }
SmartMember &operator = (const TYPE &rhs) { (obj->*set_func)(rhs);
return *this; }
// ... et cetera
};
It would be nice if the get_func and set_func could be made into
template parameters instead, which would cut down the size of object.
The code in a get method...
float x(void){
^^^^^
(void) is obsolete syntax for C compatibility. C compatibility is
irrelevant when you are writing a C++ class member function, since you
will never successfully get that through a C compiler.
In C++, you write ().
return x;
}
Why does my compiler seem to have an issue with v3d::x, v3d::x() and
the x used as an argument in the function?
You can't overload a data member and a function member. You can
overload functions only. So basically you have to rename your x. A
member called m_x will happily coexist with a x() and x(float)
function.
But it's still ugly that it's a function: the language ought to be able
to abstract accesses to what looks like a property x of the object. The
user of the class shouldn't care whether x is a simple data member, or
whether accesses to x are actually function calls that perform
computation.
The SmartMember template thing is a way to do that.