Click here to Skip to main content
15,899,004 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Winsock Event Select Model Pin
Richard Andrew x647-Jul-14 7:59
professionalRichard Andrew x647-Jul-14 7:59 
AnswerRe: Winsock Event Select Model Pin
Randor 7-Jul-14 7:25
professional Randor 7-Jul-14 7:25 
GeneralRe: Winsock Event Select Model Pin
Richard Andrew x647-Jul-14 7:31
professionalRichard Andrew x647-Jul-14 7:31 
GeneralRe: Winsock Event Select Model Pin
Randor 7-Jul-14 12:19
professional Randor 7-Jul-14 12:19 
GeneralRe: Winsock Event Select Model Pin
Richard Andrew x647-Jul-14 12:22
professionalRichard Andrew x647-Jul-14 12:22 
QuestionArithmetic Operator Overload in Pure Abstract Class Pin
CJ14-Jul-14 11:34
professionalCJ14-Jul-14 11:34 
AnswerRe: Arithmetic Operator Overload in Pure Abstract Class Pin
jschell7-Jul-14 10:10
jschell7-Jul-14 10:10 
AnswerRe: Arithmetic Operator Overload in Pure Abstract Class Pin
Stefan_Lang7-Jul-14 21:43
Stefan_Lang7-Jul-14 21:43 
One way to do it is by adding (pure) virtual accessors that read the elements that you need for the addition, and (pure) virtual factory methods that you can use to create an object of the correct type. The following might work:
C++
class vecbase {
public:
   virtual int size() const = 0;
   virtual int& operator[](int i) = 0;
   virtual int operator[](int i) const = 0;
   virtual vecbase create(int sz, int* values) = 0;

   friend vecbase operator+(const vecbase& a, const vecbase& b) {
      assert(a.size() == b.size());
      int* values = new int[a.size()];
      for (int i = 0; i < a.size(); ++i) {
         values[i] = a[i] + b[i];
      }
      result = create(a.size(), values);
   }
};

Note that I declared the operator as a friend function, rather than a member function. I prefer that approach for binary operators in case I need operators for mixed argument types.

The functions in the derived classes should then change the result types of the overriden functions to the appropriate type:
C++
class vec2 : public vecbase {
private:
   int val[2]
public:
   vec2(int* values) {
      assert(values != nullptr);
      val[0] = values[0];
      val[1] = values[1];
   }
   int size() const { return 2; }
   int& operator[](int i) { return val[i]; }
   int operator[](int i) const { return val[i]; }
   // change return type on this override - it is tstill considered an override!
   vec2 create(int sz, int* values) {
      assert(sz==2);
      return vec2(values);
   }
};

I haven't tested this or even compiled, but this should be good enough to get the idea.

P.S.: just realized that I forgot to release the values array - that one will be a bit tricky, maybe you need additional pure virtual helpers to create and release a sufficiently large initialization array...


P.P.S.: I just realized this could be done much easier - all you need is a pure virtual function that performs the addition, and creates the resulting object as well:
C++
class vecbase {
...
   virtual vecbase add(const vecbase&, const vecbase&) = 0;

   friend vecbase operator+(const vecbase& a, const vecbase& b) {
      return add(a, b);
   }
};
class vec2 : public vecbase {
...
public:
   vec2 add(const vecbase&, const vecbase&) { ... }
};

Of course, that defeats the purpose of implementing the operator in the base class - but then you can't really do that without virtual helper functions anyway.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)


modified 8-Jul-14 4:17am.

QuestionRepaint control component question Pin
econy4-Jul-14 4:16
econy4-Jul-14 4:16 
AnswerRe: Repaint control component question Pin
Richard MacCutchan4-Jul-14 5:46
mveRichard MacCutchan4-Jul-14 5:46 
QuestionHow to trace a program? Pin
GwapoKho3-Jul-14 3:02
GwapoKho3-Jul-14 3:02 
AnswerRe: How to trace a program? Pin
jeron13-Jul-14 5:02
jeron13-Jul-14 5:02 
AnswerRe: How to trace a program? Pin
Stefan_Lang4-Jul-14 0:05
Stefan_Lang4-Jul-14 0:05 
QuestionTBLRD instruction PIC18 Pin
__John_3-Jul-14 1:20
__John_3-Jul-14 1:20 
AnswerRe: TBLRD instruction PIC18 Pin
CPallini3-Jul-14 2:51
mveCPallini3-Jul-14 2:51 
GeneralRe: TBLRD instruction PIC18 Pin
__John_3-Jul-14 4:04
__John_3-Jul-14 4:04 
GeneralRe: TBLRD instruction PIC18 Pin
CPallini3-Jul-14 4:33
mveCPallini3-Jul-14 4:33 
AnswerRe: TBLRD instruction PIC18 Pin
jeron13-Jul-14 4:14
jeron13-Jul-14 4:14 
QuestionLinked List With Two Three Tree Pin
Hamza Bin Amin30-Jun-14 11:06
Hamza Bin Amin30-Jun-14 11:06 
QuestionRe: Linked List With Two Three Tree Pin
David Crow2-Jul-14 3:47
David Crow2-Jul-14 3:47 
GeneralRe: Linked List With Two Three Tree Pin
CPallini2-Jul-14 22:58
mveCPallini2-Jul-14 22:58 
AnswerRe: Linked List With Two Three Tree Pin
Stefan_Lang3-Jul-14 23:56
Stefan_Lang3-Jul-14 23:56 
GeneralMessage Closed Pin
30-Jun-14 8:40
Member 1091480330-Jun-14 8:40 
GeneralRe: STAR TREK PROJECT Pin
David Crow30-Jun-14 9:22
David Crow30-Jun-14 9:22 
GeneralRe: STAR TREK PROJECT Pin
jeron130-Jun-14 9:53
jeron130-Jun-14 9:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.