Hello all,
I have written a class with many private data members.and i am putting
it in a separate dll file.
Now when i link that file while writing my main program
module,natuarally i have to use the header file of the class developed
by me to access its functionality.
so what should be there in the header file?
Only the public methods declaration / data members of that class or the
whole layout of the class along with its all private members?
(
IMO the private section restricts the accessibility of the class and
the user of the class is abstracted from the exact layout of the
class.So there should be no need to show the internal layout of my
class by putting the private variable inside the header file of that
class.
)
I encountered severe crasing problem when I declare the class without
the private members in the header file? but everything works fine when
i put the exact layout of the class correctly inside the header file.
(i have defined all the public methods inside a separate cpp file with
the scope resolution operator)
You have some very valid concerns. As you have learned, you can't change
the header file without recompiling all the object files that include
it. By putting the data in the header file, even in the private part of
a class, all of the object files (the DLL and *all* programs that depend
on that DLL) will have to be recompiled every time you make any change
in the data.
Edson Manoel gave you the best advice by far. Use the pimpl idiom to
solve this problem:
// file y.h
class X;
class Y {
// remove all the private parts
X* px_;
public:
Y();
Y( const Y& other );
~Y();
Y& operator=( const Y& other );
// ...
};
// file y.cpp
class X {
// everything that was private in Y
};
Y::Y() : px_( new X ) {}
Y::Y( const Y& other ) {
*px_ = other.px_;
}
Y::~Y() { delete px_; }
Y& operator=( const Y& other ) {
*px_ = other.px_;
}
By doing the above, you will be able to add/remove private bits without
having to recompile users of the Y class.
[ See
http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]