1

I have a C++ class with two constructors (a default one and another with arguments). In order to reuse code, I avoided initializing class members at the constructor level, and I'm doing it in an Initialize method instead, which I am calling from both constructors. This way, I was hopping to minimize code lines and repeated code:

Location::Location(){
    double pos[POSITION_SIZE] = {0};
    this->Initialize(const_cast<char*>(""), const_cast<char*>(""), pos);
}


Location::Location(char *id, char *code, double pos[POSITION_SIZE]){
    this->Initialize(id, code, pos);
}


void Location::Initialize(char *id, char *code, double pos[POSITION_SIZE]){
    strcpy(this->ID, id);
    strcpy(this->code, code);

    this->position[0] = pos[0];
    this->position[1] = pos[1];
    this->position[2] = pos[2];

    this->attribute1 = 0;
    this->attribute2 = 0;
}

header:

class Location{
public:
    Location();
    Location(char *id, char *code, double pos[POSITION_SIZE]);

private:
    // This method initializes the location attributes given as parameters
    void Initialize(char *id, char *code, double pos[POSITION_SIZE]);

    // Name/identifier of the location
    char ID[ID_LENGTH];
    // FIR identifier
    char code[ID_LENGTH];
    // Location's coordinates (lat, lon, alt)
    double position[POSITION_SIZE];
    // Attribute 1
    double attribute1;
    // Attribute 2
    double attribute2;
};

I know that using initialize methods is a bad praxis when used because old school coding style or avoiding the usage of exceptions at constructor for example. But my goal here was reducing code, so unless some guru of stackoverflow says the opposite, I think it is not wrong (but I'm here to learn, so please destroy all my convictions).

The problem is that I'm getting a warning for not initializing class members within the cosntructor. The compiler doesn't like them to get initialized at the Initialize method. So, any way of making the compiler happy? Should I forget aboput Initialize method usage?

6
  • please show the declaration of the class - and seriously consider using std::string instead of fiddling with char* const_cast and strcpy (unless you're using some weird standard library which doesn't have string). This code is begging for problems. Commented Jan 15, 2015 at 8:33
  • @stijn Yeah, I prefer std::string, but sadly I'm inheriting a lot of awful code made by someone that learned C instead of C++, and I can't afford migrating it all. I'll update with class declaration now Commented Jan 15, 2015 at 8:41
  • Compiles fine here (VS2013, gcc 4.8.3) - except that FIR is not declared anywhere, and I had to guess POSITION_SIZE and ID_LENGTH. Please post exact code causing the problem and state your compiler/version Commented Jan 15, 2015 at 8:57
  • @stijn It is not a problem, but a warning: "Member attribute1 is not declared in this constructor". The FIR is a typo when trying to addapt current code to the given example. The exact code is just this, with more class members, all of them being initialized at the Initialize method. I just want to know how to avoid the warning message (which is about attribute1 at the defautl constructor and about attribute2 at the other constructor, which is interesting), or if I am doing some bad praxis here and should rearrange and forget about the Initialize method Commented Jan 15, 2015 at 9:15
  • "not declared in this constructor" doesn't give a sintgle search hit. Really wonder what compiler you are using. Commented Jan 15, 2015 at 9:32

1 Answer 1

4

I would use constructor delegation, something like:

#include <iostream>
using namespace std;

class foo
{
public:
    foo()
    : foo(1, "2", 3.) // delegate to the other constructor with defaults...
    { }

    foo(int a, std::string b, double c)
    : _a(a), _b(b), _c(c)
    { }

private:
    int _a;
    std::string _b;
    double _c;
};

int main() {
    foo f1{};
    foo f2{1, "3", 4.};
    return 0;
}

With the caveat that you can use atleast c++11...

Sign up to request clarification or add additional context in comments.

2 Comments

I am not allowed to use C++11 :(. Is constructor delegation available at C++03?
No. Unfortunately there is no clean way to do this, you have several options, all of which are still messy, 1. inheritance - collect all your stuff in to a base class, then use base class constructor, 2. pimpl (hide your implementation and construct the implementation with the attributes..) etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.