Adding object to class - which option makes more sense ?
-
I am adding a new object to an existing object. In QtCreator, I have an option to add it /pass it as a class new parameter. The other option is to add new object as a .so library. I am asking for (academic) evaluation / discussion of each option, as a C++ task , nothing to do with Qt. I will appreciate your time , however, off subject , immature flaming responses will be as always ignored. Cheers
-
I am adding a new object to an existing object. In QtCreator, I have an option to add it /pass it as a class new parameter. The other option is to add new object as a .so library. I am asking for (academic) evaluation / discussion of each option, as a C++ task , nothing to do with Qt. I will appreciate your time , however, off subject , immature flaming responses will be as always ignored. Cheers
Sorry, I must be hard of thinking, but your question doesn't make much sense to me. By "adding an object to a class" I think in terms of
// original class
class C1 {
int i;
double d;
public:
C1(int x, double y) : i(x), d(y) {}
...
};// new class :
class C2 {
int i;
double d;
std::string str; // <<= "addition" to C1
public:
C2(int x, double y, std::string s) : i(x), d(y), str(s){}
...
};If that's the case, then you've got a couple of choices. If you've got full control of the source, including the definition and implementation of the class, then it might make sense to just add the object to the original class and recompile. That might be the way to go especially if this is a stand-alone app with no, or few, clients depending on it. Alternatively, create a derived class e.g.
// original class
class C1 {
int i;
double d;
public:
C1(int x, double y) : i(x), d(y) {}
...
};// new class :
class C2 : public C1 {
std::string str; // <<= "addition" to C1
public:
C2(int x, double y, std::string z) :
C1(x, y), // instantiate the C1 portion of C2
str(z) // instantiate new object of C2
{}
// add any new functionality needed for a C2 object
};With a derived class, there's no breakage for anything that's using a
C1
. Anything that needs an "improved" C2 has the functionality required. Additionally, anything that expects a C1 can take a C2, but you do not have access to C2's extended functionalit. If you mean something different, maybe you can create a (short) example of what you're trying to accomplish?"A little song, a little dance, a little seltzer down your pants" Chuckles the clown
-
I am adding a new object to an existing object. In QtCreator, I have an option to add it /pass it as a class new parameter. The other option is to add new object as a .so library. I am asking for (academic) evaluation / discussion of each option, as a C++ task , nothing to do with Qt. I will appreciate your time , however, off subject , immature flaming responses will be as always ignored. Cheers
-
Sorry, I must be hard of thinking, but your question doesn't make much sense to me. By "adding an object to a class" I think in terms of
// original class
class C1 {
int i;
double d;
public:
C1(int x, double y) : i(x), d(y) {}
...
};// new class :
class C2 {
int i;
double d;
std::string str; // <<= "addition" to C1
public:
C2(int x, double y, std::string s) : i(x), d(y), str(s){}
...
};If that's the case, then you've got a couple of choices. If you've got full control of the source, including the definition and implementation of the class, then it might make sense to just add the object to the original class and recompile. That might be the way to go especially if this is a stand-alone app with no, or few, clients depending on it. Alternatively, create a derived class e.g.
// original class
class C1 {
int i;
double d;
public:
C1(int x, double y) : i(x), d(y) {}
...
};// new class :
class C2 : public C1 {
std::string str; // <<= "addition" to C1
public:
C2(int x, double y, std::string z) :
C1(x, y), // instantiate the C1 portion of C2
str(z) // instantiate new object of C2
{}
// add any new functionality needed for a C2 object
};With a derived class, there's no breakage for anything that's using a
C1
. Anything that needs an "improved" C2 has the functionality required. Additionally, anything that expects a C1 can take a C2, but you do not have access to C2's extended functionalit. If you mean something different, maybe you can create a (short) example of what you're trying to accomplish?"A little song, a little dance, a little seltzer down your pants" Chuckles the clown
Thank you. I am not that versatile to create derived class - but that is something I like to try. I am pretty stuck with Qt and not sure how to physically accomplish the derivation. At present I am testing the "add" using a library, mainly because the library was tested long time ago.
-
Thank you. I am not that versatile to create derived class - but that is something I like to try. I am pretty stuck with Qt and not sure how to physically accomplish the derivation. At present I am testing the "add" using a library, mainly because the library was tested long time ago.