operator+ overloading inheritance prob

Hi guys,

given a base class "vec" and a child "vec3d" with common data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
template<typename TYPE>
class vec{
public:
  TYPE* val;
  int size;

public:
  vec();
  vec( const vec& toCopyFrom );
  ~vec();
  operator*( const TYPE right){
    vec<TYPE> result( *this );        
    for( int i = 0; i < size; i++ ) result[i] *= right;
    return result; };
};

class vec3d : public vec{
 // no data
public:
  vec3d() : vec(); //and so on and so forth
  vec3d( const vec<TYPE> &toCopyFrom );

  operator*( const TYPE& right){
    return vec3d( vec<TYPE>::operator*(right));
};


How can I cast operator* (line 22) correctly, since *this (line 12) refers to a vec3d object now - which somehow doesn't seem to work.

Best, ZED
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
template<typename TYPE>
class vec{
private: // Incapsulation!
  TYPE* val;
  int size;

public:
	vec() {}
	vec( const vec<TYPE>& toCopyFrom ) {}
	~vec() {}
  vec<TYPE> operator*( const TYPE right){
    vec<TYPE> result( *this );        
    for( int i = 0; i < size; i++ ) result[i] *= right;
    return result; } // Without ';' after '}'!
};

template <typename TYPE>
class vec3d : public vec<TYPE>{
 // no data
public:
  vec3d() : vec() {} //and so on and so forth
  vec3d<TYPE> operator*( const TYPE& right){
    return this->vec<TYPE>::operator*(right);}
};
But if I define it private, I can't access it from vec3d?! Wouldn't protected be a better shot?
Last edited on
actually your solution doesn't seem to work all that well...but I might have found something working for me: adding a constructor vec3d( const vec ) and returning vec3d( this->vec<TYPE>::operator*(right))
I think the real issue here might be that you are not using inheritance correctly.

What is the difference between vec3d and vec? What is their relationship? And why do you want to inherit?
Actual vec3d has some member functions allowing some "math" tricks a vector with undefined length can't be used for (in my case). So vec3d is basically a additional member-function kit. Their relationship is that vec is the base of vec3d. It's the data-model, just vec3d has some "known" attributes. What do you mean I don't use it correctly? Can you explain this a bit more in detail? (Actual the real issue here is that I want to learn how inheritance works, I - probably - could use plain vec for all my members - but I wanna understand it!)
Last edited on
mhm...I'm beginning to fear that this might not be the problem...when tested in a separate program my class vec3d works as intended and all complaining operators do as well...mhm...*searching for real problem*
Last edited on
IN FACT the error arose from lacking const-correctness... :(

ANYWAY: (AGAIN) Having some hard time here getting the grips with C++ (programmed pretty much plain C before...) So thanks for answers and all the best. Good tutorials on inheritance and such are GREATLY APPRECIATED! (I don't mean those vanilla ( 08/15 for my German friends) ones - I read some of them, but they don't tell you the interesting stuff...)
Last edited on
Topic archived. No new replies allowed.