Private methods in classes and DRY principle

I'm an old C hack from way back. I can make my way around in C++ and know OO, so I've done some rudimentary class writing in C++ that has gotten me by, but there is way too much C++ I don't know and haven't used yet.

So I've scoured the internet (read "Google") for examples of this and the fact there are none and plus the fact I can't seem to get this to WORK leads me to believe I'm doing this the wrong way and I'm beside myself as to what the correct C++ approach would actually be.

Maybe a contrived example:

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
26
27
28
29
30
31
MyClass.h:

class MyClass {
   private:
      bool flag;
      string name;
   public:
      MyClass::MyClass();
      MyClass::MyClass( string );
      virtual MyClass::~MyClass();
}

MyClass.cpp:

#include "MyClass.h"

MyClass::MyClass() {
   int foo = 2;
   int bar = 3;
   flag = (2 > 3);
   name = "";
}

MyClass::MyClass( string aName ) {
   int foo = 2;
   int bar = 3;
   flag = (2 > 3);
   name = aName;
}

MyClass::~MyClass() {}


Now, I've got the same code, essentially in both constructors for setting flag. In Java or Perl OO or Ruby, I would just pull those out into a private method and set the private flag with consolidated code, I was assuming in the same way. Easy:

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
26
27
28
29
30
31
32
33
34
Revised MyClass.h:

class MyClass {
   private:
      bool flag;
      string name;
      bool set_flag();
   public:
      MyClass::MyClass();
      MyClass::MyClass( string );
      virtual MyClass::~MyClass();
}

Revised MyClass.cpp:

#include "MyClass.h"

MyClass::MyClass() {
   flag = set_flag();
   name = "";
}

MyClass::MyClass( string aName ) {
   flag = set_flag();
   name = aName;
}

MyClass::~MyClass() {}

bool MyClass::set_flag() {
   int foo = 2;
   int bar = 3;
   return (2 > 3);
}


I get a stack dump on the return from the set_flag() private method call? I've even just done a "return false" in MyClass::set_flag() and it still doesn't work? I'm not seeing a whole not of examples of this which leads to be believe the approach isn't correct. Again in any other language, this is pretty much the practice. How does one approach centralization of common code local to a class? Again, the example is a bit contrived, but... I hope the point is made.

-eriks
I can't see the problem in those files. How does your main look like?

1
2
3
4
5
6
7
8
9
10
class MyClass {
   private:
      bool flag;
      string name;
      bool set_flag();
   public:
      MyClass();
      MyClass( string );
      virtual ~MyClass();
}


This is the correct way of declaring class methods.
I don't see anything wrong (although, you should have an inclusion guard in the header file). Did you try a clean build?
Last edited on
I'll post the real code tomorrow sometime. Meanwhile, maybe I should try this in Linux. Right now, I'm doing this in Cygwin. But still... Even my real life code is so simple, I was just baffled and couldn't think of anything that could be causing the issue except maybe Cygwin itself (which is why I thought I'd mention it now).
Topic archived. No new replies allowed.