Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Adding object to class - which option makes more sense ?

Adding object to class - which option makes more sense ?

Scheduled Pinned Locked Moved C / C++ / MFC
c++questiondiscussion
5 Posts 3 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jana_hus
    wrote on last edited by
    #1

    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

    K L 2 Replies Last reply
    0
    • J jana_hus

      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

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      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

      J 1 Reply Last reply
      0
      • J jana_hus

        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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        jana_hus wrote:

        immature flaming responses will be as always ignored.

        as will yours.

        1 Reply Last reply
        0
        • K k5054

          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

          J Offline
          J Offline
          jana_hus
          wrote on last edited by
          #4

          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.

          K 1 Reply Last reply
          0
          • J jana_hus

            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.

            K Offline
            K Offline
            k5054
            wrote on last edited by
            #5

            Googling for something like "C++ derived class tutorial" should give you some useful hits.

            "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups