"Retroactive Union" - can it be done? - c++

I've got two classes: a template class, and a regular class that inherits from it:
template <int N> class Vector
{
float data[N];
//etc. (math, mostly)
};
class Vector3 : public Vector<3>
{
//Vector3-specific stuff, like the cross product
};
Now, I'd like to have x/y/z member variables in the child class (full members, not just getters - I want to be able to set them as well). But to make sure that all the (inherited) math works out, x would have to refer to the same memory as data[0], y to data[1], etc. Essentially, I want a union, but I can't declare one in the base class because I don't know the number of floats in the vector at that point.
So - can this be done? Is there some sort of preprocessor / typedef / template magic that will achieve what I'm looking for?
PS: I'm using g++ 4.6.0 with -std=c++0x, if that helps.
Edit: While references would give the syntax I'm looking for, the ideal solution wouldn't make the class any bigger (And references do - a lot! A Vector<3> is 12 bytes. A Vector3 with references is 40!).

How about:
class Vector3 : public Vector<3>
{
public:
// initialize the references...
Vector3() : x(data[0]), y(data[1]), z(data[2]){}
private:
float& x;
float& y;
float& z;
};
Of course, if you want them to occupy the same space, then that's a different story...
With a little template magic, you can do the following...
#include <iostream>
template <int N, typename UnionType = void*> struct Vector
{
union
{
float data[N];
UnionType field;
};
void set(int i, float f)
{
data[i] = f;
}
// in here, now work with data
void print()
{
for(int i = 0; i < N; ++i)
std::cout << i << ":" << data[i] << std::endl;
}
};
// Define a structure of three floats
struct Float3
{
float x;
float y;
float z;
};
struct Vector3 : public Vector<3, Float3>
{
};
int main(void)
{
Vector<2> v1;
v1.set(0, 0.1);
v1.set(1, 0.2);
v1.print();
Vector3 v2;
v2.field.x = 0.2;
v2.field.y = 0.3;
v2.field.z = 0.4;
v2.print();
}
EDIT: Having read the comment, I realise what I posted before was really no different, so a slight tweak to the previous iteration to provide direct access to the field (which is what I guess you are after) - I guess the difference between this and Rob's solution below is that you don't need all the specializations to implement all the logic again and again...

How about template specialization?
template <int N> class Vector
{
public:
float data[N];
};
template <>
class Vector<1>
{
public:
union {
float data[1];
struct {
float x;
};
};
};
template <>
class Vector<2>
{
public:
union {
float data[2];
struct {
float x, y;
};
};
};
template <>
class Vector<3>
{
public:
union {
float data[3];
struct {
float x, y, z;
};
};
};
class Vector3 : public Vector<3>
{
};
int main() {
Vector3 v3;
v3.x;
v3.data[1];
};
EDIT Okay, here is a different approach, but it introduces an extra identifier.
template <int N> class Data
{
public:
float data[N];
};
template <> class Data<3>
{
public:
union {
float data[3];
struct {
float x, y, z;
};
};
};
template <int N> class Vector
{
public:
Data<N> data;
float sum() { }
float average() {}
float mean() {}
};
class Vector3 : public Vector<3>
{
};
int main() {
Vector3 v3;
v3.data.x = 0; // Note the extra "data".
v3.data.y = v3.data.data[0];
};

Here's one possibility, cribbed from my answer to this question:
class Vector3 : public Vector<3>
{
public:
float &x, &y, &z;
Vector3() : x(data[0]), y(data[1]), z(data[2]) { }
};
This has some problems, like requiring you to define your own copy constructor, assignment operator etc.

You can make the following:
template <int N> struct Vector
{
float data[N];
//etc. (math, mostly)
};
struct Vector3_n : Vector<3>
{
//Vector3-specific stuff, like the cross product
};
struct Vector3_a
{
float x, y, z;
};
union Vector3
{
Vector3_n n;
Vector3_a a;
};
Now:
Vector3 v;
v.n.CrossWhatEver();
std::cout << v.a.x << v.a.y << v.a.z
You could try the anonymous union trick, but that is not standard nor very portable.
But note that with this kind of union it is just too easy to fall into undefined behaviour without even noticing. It will probably mostly work anyway, though.

I wrote a way a while back (that also allowed getters/setters), but it was such a non-portable garrish hack that YOU REALLY SHOULD NOT DO THIS. But, I thought I'd throw it out anyway. Basically, it uses a special type with 0 data for each member. Then, that type's member functions grab the this pointer, calculate the position of the parent Vector3, and then use the Vector3s members to access the data. This hack works more or less like a reference, but takes no additional memory, has no reseating issues, and I'm pretty sure this is undefined behavior, so it can cause nasal demons.
class Vector3 : public Vector<3>
{
public:
struct xwrap {
operator float() const;
float& operator=(float b);
float& operator=(const xwrap) {}
}x;
struct ywrap {
operator float() const;
float& operator=(float b);
float& operator=(const ywrap) {}
}y;
struct zwrap {
operator float() const;
float& operator=(float b);
float& operator=(const zwrap) {}
}z;
//Vector3-specific stuff, like the cross product
};
#define parent(member) \
(*reinterpret_cast<Vector3*>(size_t(this)-offsetof(Vector3,member)))
Vector3::xwrap::operator float() const {
return parent(x)[0];
}
float& Vector3::xwrap::operator=(float b) {
return parent(x)[0] = b;
}
Vector3::ywrap::operator float() const {
return parent(y)[1];
}
float& Vector3::ywrap::operator=(float b) {
return parent(y)[1] = b;
}
Vector3::zwrap::operator float() const {
return parent(z)[2];
}
float& Vector3::zwrap::operator=(float b) {
return parent(z)[2] = b;
}

To finish off an old question: No. It makes me sad, but you can't do it.
You can get close. Things like:
Vector3.x() = 42;
or
Vector3.x(42);
or
Vector3.n.x = 42;
or even
Vector3.x = 42; //At the expense of almost quadrupling the size of Vector3!
are within reach (see the other answers - they're all very good). But my ideal
Vector3.x = 42; //In only 12 bytes...
just isn't doable. Not if you want to inherit all your functions from the base class.
In the end, the code in question ended up getting tweaked quite a bit - it's now strictly 4-member vectors (x, y, z, w), uses SSE for vector math, and has multiple geometry classes (Point, Vector, Scale, etc.), so inheriting core functions is no longer an option for type-correctness reasons. So it goes.
Hope this saves someone else a few days of frustrated searching!

Related

Container of points

I'm writing a class that stores points with X, Y, Z coordinates, where the specific definition of point is templated:
template<class T>
class Foo {
void add_point(T point);
}
Some functions in my class require to access the X,Y,Z components. The issue is that point types (defined by 3rd party libraries) don't have any common interface to access the coordinates. Some of them allow operator[] or operator(), others access by .x or .x().
What approach is better to address that? Should I add another template parameter with a function to access the coordinates? Or should I give up and keep an internal copy of the 3D point in the format I prefer?
Create a template function class who's job is to homogenise the interfaces between all types of point.
Like this:
#include <iostream>
// One kind of point
struct PointA
{
double& operator[](int which) {
return data[which];
};
double data[3];
};
// Another kind of point
struct PointB {
double& x() { return data[0]; }
double& y() { return data[1]; }
double& z() { return data[2]; }
double data[3];
};
// An object which homogenises interfaces
template<class Point>
struct get_coordinate {
double& x() const {
return get_x(p);
}
Point& p;
};
// some overloads
double& get_x(PointA& p) {
return p[0];
}
double& get_x(PointB& p) {
return p.x();
}
template<class Point>
struct Foo
{
void add_point(Point point) { p = point; }
double& x() {
// call through the homogeniser
return get_coordinate<Point>{p}.x();
}
Point p;
};
// test
int main()
{
Foo<PointA> fa;
fa.add_point(PointA{{1,2,3}});
std::cout << fa.x() << std::endl;
Foo<PointB> fb;
fb.add_point(PointB{{1,2,3}});
std::cout << fb.x() << std::endl;
}

Template specific constructor

I'm writing my own vector class and i got a problem.
i defined my class as template,i have definition for each of the vector sizes and i want specific constructor for each of the vector sizes.
here is the code:
template<int size>
ref class Vector
{
internal:
Vector(int _x, int _y, int _z, int _w);
private:
float *m_data = new float[4];
};
and the definitions is :
using Vector2 = Vector<2>;
using Vector3 = Vector<3>;
using Vector4 = Vector<4>;
first of all can i do that? if the answer is yes how?
If you want common interface, define constructor as having 4 arguments and specialize it. Inside, initialize only those members, that are valid for vector of this size:
template <>
Vector<1>::Vector(int _x, int _y, int _z, int _w)
: x(_x) //1D vector has only 'x'
{
}
template <>
Vector<2>::Vector(int _x, int _y, int _z, int _w)
: x(_x)
, y(_y) //2D vector has 'x' and 'y'
{
}
Etc. But that's ugly, forces you you to make some things "common", for example you will hold 4 components even for 2D vector. There are workarounds (templated structure used as member variable, specialized for vector of each size), but that's quite more complicated. Since vectors of different size are in fact different types, I would go for full class specialization:
template<int size>
class Vector;
template<>
class Vector<1>
{
protected:
int x;
public:
Vector(int _x)
: x(_x)
{ }
//other members
};
template<>
class Vector<2>
{
protected:
int x, y;
public:
Vector(int _x, int _y)
: x(_x)
, y(_y)
{ }
//other members
};
Then you can use it this way:
Vector<1> v_1(2);
Vector<2> v_2(4, 6);
//etc...
Also, second solution will allow clients of your vector to instantiate it only for those sizes, that you explicitly allow.
If you really want different behaviour for each template instance, you can do it like so:
//specific version for when 0 is passed as the template argument
template<>
Vector<0>::Vector (int _x, int _y, int _z, int _w)
{
//some Vector<0> related stuff
}
//Vector<1> will use the default version
//specific version for when 2 is passed as the template argument
template<>
Vector<2>::Vector (int _x, int _y, int _z, int _w)
{
//some Vector<2> related stuff
}
With C++11, you may do something like:
template<int size>
class Vector
{
public:
template <typename ...Ts,
typename = typename std::enable_if<size == sizeof...(Ts)>::type>
explicit Vector(Ts... args) : m_data{static_cast<float>(args)...} {}
private:
float m_data[size];
};
using Vector2 = Vector<2>;
using Vector3 = Vector<3>;
using Vector4 = Vector<4>;
int main()
{
Vector2 v2(42, 5);
Vector3 v3(42, 5, 3);
Vector4 v4(42, 5, 51, 69);
}

Adding references to a class to act as accessors

I have added reference variables to a class to act as accessors for an array which is declared as a private member. Basically, I have something like
class someClass {
private:
int a[3];
public:
int &a0;
int &a1;
int &a2;
someClass() : a0(a[0]), a1(a[1]), a2(a[2]) {}
someClass& operator=(const someClass &other) {
std::memcpy(a, other.a, sizeof(a));
return *this;
}
};
However, it does not work as expected always. What am I missing here? Is there a better way to access elements of a as .a0, .a1, etc.
Instead of using references, you should consider using mixed in unions instead:
struct Vector3
{
union {
float a[3];
struct { float x, y, z; };
struct { float r, g, b; };
};
};
// v.a[0] is an alias for v.x
Vector3 v = { 0.0f, 0.1f, 0.2f };
// you can see this clearly since they have the same address:
std::cout << (&v.a[0] == &v.x) << std::endl;
This should provide what you are actually looking to achieve.

How to access members by both name and as an array?

I have a bunch of vector classes. I have a 2D point vec2_t, a 3D point vec3_t and a 4D point vec4_t (you often want these when you have do graphics; this is graphics code, but the question has a generic C++ flavour).
As it is now, I have vec2_t declaring two members x and y; vec3_t subclasses vec2_t and has a third member z; vec4_t subclasses vec3_t and adds a w member.
I have a lot of near-duplicate code for operator overloading computing things like distances, cross products, multiplication by a matrix and so on.
I've had a few bugs where things have been sliced when I've missed to declare an operator explicitly for the subclass and so on. And the duplication bugs me.
Additionally, I want to access these members as an array too; this would be useful for some OpenGL functions that have array parameters.
I imagine that perhaps with a vec_t<int dimensions> template I can make my vector classes without subclassing. However, this introduces two problems:
How do you have a variable number of members that are also array entries, and ensure they align? I don't want to lose my named members; vec.x is far nicer than vec.d[0] or whatever imo and I'd like to keep it if possible
How do you have lots of the more expensive methods in a CPP source file instead of the header file when you take the templating route?
One approach is this:
struct vec_t {
float data[3];
float& x;
float& y;
float& z;
vec_t(): x(data[0]), y(data[1]), z(data[2]) {}
};
Here, it correctly aliases the array members with names, but the compiler I've tested with (GCC) doesn't seem to work out they are just aliases and so the class size is rather large (for something I might have an array of, and want to pass e.g. as a VBO; so size is a big deal) and how would you template-parameterise it so only the vec4_t had a w member?)
A possible solution (I think).
main.cpp:
#include <iostream>
#include "extern.h"
template <int S>
struct vec_t_impl
{
int values[S];
bool operator>(const vec_t_impl<S>& a_v) const
{
return array_greater_than(values, a_v.values, S);
}
void print() { print_array(values, S); }
virtual ~vec_t_impl() {}
};
struct vec_t2 : vec_t_impl<2>
{
vec_t2() : x(values[0]), y(values[1]) {}
int& x;
int& y;
};
struct vec_t3 : vec_t_impl<3>
{
vec_t3() : x(values[0]), y(values[1]), z(values[2]) {}
int& x;
int& y;
int& z;
};
int main(int a_argc, char** a_argv)
{
vec_t3 a;
a.x = 5;
a.y = 7;
a.z = 20;
vec_t3 b;
b.x = 5;
b.y = 7;
b.z = 15;
a.print();
b.print();
cout << (a > b) << "\n";
return 0;
}
extern.h:
extern bool array_greater_than(const int* a1, const int* a2, const size_t size);
extern void print_array(const int* a1, const size_t size);
extern.cpp:
#include <iostream>
bool array_greater_than(const int* a1, const int* a2, const size_t size)
{
for (size_t i = 0; i < size; i++)
{
if (*(a1 + i) > *(a2 + i))
{
return true;
}
}
return false;
}
void print_array(const int* a1, const size_t size)
{
for (size_t i = 0; i < size; i++)
{
if (i > 0) cout << ", ";
std::cout << *(a1 + i);
}
std::cout << '\n';
}
EDIT:
In an attempt to address the size issue you could change the member reference variables to member functions that return a reference.
struct vec_t2 : vec_t_impl<2>
{
int& x() { return values[0]; }
int& y() { return values[1]; }
};
Downside to this is slightly odd code:
vec_t2 a;
a.x() = 5;
a.y() = 7;
Note: Updated and improved the code a lot.
The following code uses a macro to keep the code clean and partial specialization to provide the members. It relies heavily on inheritence, but that makes it very easy to extend it to arbitary dimensions. It's also intended to be as generic as possible, that's why the underlying type is a template parameter:
// forward declaration, needed for the partial specializations
template<unsigned, class> class vec;
namespace vec_detail{
// actual implementation of the member functions and by_name type
// partial specializations do all the dirty work
template<class Underlying, unsigned Dim, unsigned ActualDim = Dim>
struct by_name_impl;
// ultimate base for convenience
// this allows the macro to work generically
template<class Underlying, unsigned Dim>
struct by_name_impl<Underlying, 0, Dim>
{ struct by_name_type{}; };
// clean code after the macro
// only need to change this if the implementation changes
#define GENERATE_BY_NAME(MEMBER, CUR_DIM) \
template<class Underlying, unsigned Dim> \
struct by_name_impl<Underlying, CUR_DIM, Dim> \
: public by_name_impl<Underlying, CUR_DIM - 1, Dim> \
{ \
private: \
typedef vec<Dim, Underlying> vec_type; \
typedef vec_type& vec_ref; \
typedef vec_type const& vec_cref; \
typedef by_name_impl<Underlying, CUR_DIM - 1, Dim> base; \
protected: \
struct by_name_type : base::by_name_type { Underlying MEMBER; }; \
\
public: \
Underlying& MEMBER(){ \
return static_cast<vec_ref>(*this).member.by_name.MEMBER; \
} \
Underlying const& MEMBER() const{ \
return static_cast<vec_cref>(*this).member.by_name.MEMBER; \
} \
}
GENERATE_BY_NAME(x, 1);
GENERATE_BY_NAME(y, 2);
GENERATE_BY_NAME(z, 3);
GENERATE_BY_NAME(w, 4);
// we don't want no pollution
#undef GENERATE_BY_NAME
} // vec_detail::
template<unsigned Dim, class Underlying = int>
class vec
: public vec_detail::by_name_impl<Underlying, Dim>
{
public:
typedef Underlying underlying_type;
underlying_type& operator[](int idx){
return member.as_array[idx];
}
underlying_type const& operator[](int idx) const{
return member.as_array[idx];
}
private:
typedef vec_detail::by_name_impl<Underlying, Dim> base;
friend struct vec_detail::by_name_impl<Underlying, Dim>;
typedef typename base::by_name_type by_name_type;
union{
by_name_type by_name;
underlying_type as_array[Dim];
} member;
};
Usage:
#include <iostream>
int main(){
typedef vec<4, int> vec4i;
// If this assert triggers, switch to a better compiler
static_assert(sizeof(vec4i) == sizeof(int) * 4, "Crappy compiler!");
vec4i f;
f.w() = 5;
std::cout << f[3] << '\n';
}
Of course you can make the union public, if you want to, but I think accessing the members through the function is better.
Note: The above code compiles cleanly without any warnings on MSVC10, GCC 4.4.5 and Clang 3.1 with -Wall -Wextra (/W4 for MSVC) and -std=c++0x (only for static_assert).
This would be one way to do it:
#include<cstdio>
class vec2_t{
public:
float x, y;
float& operator[](int idx){ return *(&x + idx); }
};
class vec3_t : public vec2_t{
public:
float z;
};
Edit: #aix is right in saying that it's non-standard and could cause problems. Perhaps a more appropriate solution would then be:
class vec3_t{
public:
float x, y, z;
float& operator[](int idx){
static vec3_t v;
static int offsets[] = {
((char*) &(v.x)) - ((char*)&v),
((char*) &(v.y)) - ((char*)&v),
((char*) &(v.z)) - ((char*)&v)};
return *( (float*) ((char*)this+offsets[idx]));
}
};
Edit #2: I have an alternative, where it's possible to only write your operators once, and not end up with a bigger class, like so:
#include <cstdio>
#include <cmath>
template<int k>
struct vec{
};
template<int k>
float abs(vec<k> const&v){
float a = 0;
for (int i=0;i<k;i++)
a += v[i]*v[i];
return sqrt(a);
}
template<int u>
vec<u> operator+(vec<u> const&a, vec<u> const&b){
vec<u> result = a;
result += b;
return result;
}
template<int u>
vec<u>& operator+=(vec<u> &a, vec<u> const&b){
for (int i=0;i<u;i++)
a[i] = a[i] + b[i];
return a;
}
template<int u>
vec<u> operator-(vec<u> const&a, vec<u> const&b){
vec<u> result;
for (int i=0;i<u;i++)
result[i] = a[i] - b[i];
return result;
}
template<>
struct vec<2>{
float x;
float y;
vec(float x=0, float y=0):x(x), y(y){}
float& operator[](int idx){
return idx?y:x;
}
float operator[](int idx) const{
return idx?y:x;
}
};
template<>
struct vec<3>{
float x;
float y;
float z;
vec(float x=0, float y=0,float z=0):x(x), y(y),z(z){}
float& operator[](int idx){
return (idx==2)?z:(idx==1)?y:x;
}
float operator[](int idx) const{
return (idx==2)?z:(idx==1)?y:x;
}
};
There are some problems, though:
1) I don't know how you'd go around defining member functions without having to write them (or at least some sort of stub) more than once.
2) It relies on compiler optimizations. I looked at the output from g++ -O3 -S and it seems that the loop gets unrolled and the ?:s get replaced with the proper field accesses. The question is, would this still be handled properly in a real context, say within an algorithm?
A simple solution might be the best here:
struct Type
{
enum { x, y };
int values[2];
};
Type t;
if (t.values[0] == t.values[Type::x])
cout << "Good";
You can also do something like this:
struct Type
{
int values[2];
int x() const {
return values[0];
}
void x(int i) {
values[0] = i;
}
};
If you do not want to write it yourself, you may check some of the libraries suggested on:
C++ Vector Math and OpenGL compatable
If you use one specific compiler, you may use non standard methods, like packing information or nameless structs (Visual Studio):
union Vec3
{
struct {double x, y, z;};
double v[3];
};
On the other hand, casting several member variables to an array seems dangerous because the compiler may change the class layout.
So the logic solution seems to have one array and using methods to access that array. For example:
template<size_t D>
class Vec
{
private:
float data[D];
public: // Constants
static const size_t num_coords = D;
public: // Coordinate Accessors
float& x() { return data[0]; }
const float& x() const { return data[0]; }
float& y() { static_assert(D>1, "Invalid y()"); return data[1]; }
const float& y() const { static_assert(D>1, "Invalid y()"); return data[1]; }
float& z() { static_assert(D>2, "Invalid z()"); return data[2]; }
const float& z() const { static_assert(D>2, "Invalid z()"); return data[2]; }
public: // Vector accessors
float& operator[](size_t index) {return data[index];}
const float& operator[](size_t index) const {return data[index];}
public: // Constructor
Vec() {
memset(data, 0, sizeof(data));
}
public: // Explicit conversion
template<size_t D2>
explicit Vec(const Vec<D2> &other) {
memset(data, 0, sizeof(data));
memcpy(data, other.data, std::min(D, D2));
}
};
Using the above class, you may access the member array using the [] operator, coordinates using the accessor methods x(), y(), z(). Slicing is prevented using explicit conversion constructors. It disables the use of the accessors for lower dimensions using static_assert. If you are not using C++11, you may use Boost.StaticAssert
You can also templatized your methods. You can use for in order to extend them to N dimensions or use recursive calls. For example, in order to compute the square sum:
template<size_t D>
struct Detail
{
template<size_t C>
static float sqr_sum(const Vec<D> &v) {
return v[C]*v[C] + sqr_sum<C-1>(v);
}
template<>
static float sqr_sum<0>(const Vec<D> &v) {
return v[0]*v[0];
}
};
template<size_t D>
float sqr_sum(const Vec<D> &v) {
return Detail<D>::sqr_sum<D-1>(v);
}
The above code can be used:
int main()
{
Vec<3> a;
a.x() = 2;
a.y() = 3;
std::cout << a[0] << " " << a[1] << std::endl;
std::cout << sqr_sum(a) << std::endl;;
return 0;
}
In order to prevent template bloat, you may code your templated methods on a cpp and instantiated them for D=1, 2, 3, 4.
This is yet another approach. The following works on gcc C++11:
#include <stdio.h>
struct Vec4
{
union
{
float raw[4];
struct {
float x;
float y;
float z;
float w;
};
};
};
int main()
{
Vec4 v = { 1.f, 2.f, 3.f, 4.f };
printf("%.2f, %.2f, %.2f, %.2f\n", v.x, v.y, v.z, v.w);
printf("%.2f, %.2f, %.2f, %.2f\n", v.raw[0], v.raw[1], v.raw[2], v.raw[3]);
return 0;
}

bool operator() and inheritance

I have the following problem with bool operator() for derived class
Base class
class Point
{
double x, y;
public:
Point(){x=0;y=0;}
...
}
Derived class
class 3DPoint : public Point
{
double z;
public:
3DPoint(double x, double y, double zx) : Point(x,y){z(zz);}
...
}
operator () for derived class
class compareByX
{
bool operator () (const 3DPoint *p1, const 3DPoint *p2) const
{
return p1->x < p2->x; //Compilation error
}
}
Container of points
class List: public list<3DPoint *>
{
...
}
int main()
{
List l;;
l.push_back(new 3DPoint(1,2,3));
l.push_back(new 3DPoint(4,5,6));
sort(l.begin(), l.end(), compareByX);
}
The compilation stops in class compareByX with the following message: can not convert 3DPoint const to Point. I removed const declaration...
class compareByX
{
bool operator () (3DPoint *p1, 3DPoint *p2) const
{
return p1->x < p2->x; //Compilation error
}
}
... and... successful compilation. But I believe that operator () is not well defined. Can you help me, please? Perhaps it would be better to propose more suitable object model... Thanx.
Your x,y,z are private members, and you are trying to access them from outside a class. Either make your points structs, or make your x,y,z public, or provide setters/getters for them.
EDIT:
Couple more things about your code:
Do not inherit your class List from std::list, standard containers are not meant to be used as base classes. If you need a special function that's not available in std::container, provide a free function which can do that, instead of inheriting from it.
Considering the type of question here, implementing your own container is probably not the best idea. Use some standard one, there's plenty of them, and they will most likely fit your needs.
When inheriting one class from another, the base class should normally be virtual.
Point3D isn't kind of Point2D, it's more like Point2D and Point3D are kind of Point. To me this kind of inheritance would make a bit more sense.
And just to stop guessing the compiler error you have there, give a try to this code, I think it's approximately what you're looking for.
#include <algorithm>
#include <iostream>
#include <vector>
class Point
{
public:
Point() {}
virtual ~Point() {}
virtual void some_function_relevant_to_all_points() {}
private:
// maybe some members here
};
class Point2D : public Point
{
public:
Point2D(double x, double y)
: x_(0),
y_(0)
{}
~Point2D() {}
private:
double x_;
double y_;
};
class Point3D : public Point
{
public:
Point3D(double x, double y, double z)
: x_(x),
y_(y),
z_(z)
{}
~Point3D() {}
double get_x() const {return x_;}
double get_y() const {return y_;}
double get_z() const {return z_;}
private:
double x_;
double y_;
double z_;
};
class Compare3DPointByX
{
public:
bool operator()(const Point3D *lhs, const Point3D *rhs) const
{
return lhs->get_x() < rhs->get_x();
}
};
class DeleteElement
{
public:
template <typename T>
void operator()(T *arg)
{
delete arg;
}
};
int main()
{
std::vector<Point3D *> points3d;
points3d.push_back(new Point3D(4,5,6));
points3d.push_back(new Point3D(1,2,3));
std::cout << "point 1:" << points3d[0]->get_x() << "\n";
std::cout << "point 2:" << points3d[1]->get_x() << "\n";
std::sort(points3d.begin(), points3d.end(), Compare3DPointByX());
std::cout << "point 1:" << points3d[0]->get_x() << "\n";
std::cout << "point 2:" << points3d[1]->get_x() << "\n";
std::for_each(points3d.begin(), points3d.end(), DeleteElement());
return 0;
}
Rest of functionality you can add yourself, this example is just to give you the idea, how it might be implemented.
Hope it helps, good luck.
Classes and structure definitions should be followed by a ;
Identifier names cannot start with a digit
I correct the code (thans for checks), the same question....
class Point3D : public Point
{
double z;
public:
Point3D(double x, double y, double zx) : Point(x,y){z(zz);}
...
}
class compareByX
{
bool operator () (Point3D *p1, Point3D *p2) const
{
return p1->getX() < p2->getX(); //Compilation error
}
}
class List: public list<Point3D *>
{
...
}
int main()
{
List l;;
l.push_back(new Point3D(1,2,3));
l.push_back(new Point3D(4,5,6));
sort(l.begin(), l.end(), compareByX);
}
You can't sort a std::list like that, you'll need something that supports random access like a std::vector. Either that or use the sort member function:
l.sort(compareByX());
Note the parantheses I've added to compareByX, you need to construct a functor and that's what the parentheses are for.
Also, you have to make your operator() a public member function so that the sort algorithm can call it. The simplest way to achieve this is making your functor a struct:
struct compareByX
{
bool operator () (Point3D const *p1, Point3D const *p2) const
{
return p1->getX() < p2->getX();
}
};
I suspect that you'll also have to make your getX member function public but that's difficult to tell because you're not showing that part of your code.
Finally, I don't think you need pointers/heap allocation for this particular example. Your program will be faster and more robust if you create your points on the stack.
PS: please post real code next time, it will make it much easier for those trying to help.