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. Need best approach to use a class inside other class

Need best approach to use a class inside other class

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
4 Posts 3 Posters 0 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.
  • N Offline
    N Offline
    Nandu_77b
    wrote on last edited by
    #1

    I have a base class say “ClassBase” and I have derived two classes “Classdrv1” & “Classdrv2” from base class as shown below. Now I am not sure how to use class “Classdrv2” in “Classdrv1”. For this implementation there should not be any code change in base class. What is the best approach for the same? Class ClassBase { } class Classdrv1 : public ClassBase { // need to use classdrv2 here, how? } class Classdrv2 : public ClassBase { } Thanks, Nandu

    CPalliniC A 2 Replies Last reply
    0
    • N Nandu_77b

      I have a base class say “ClassBase” and I have derived two classes “Classdrv1” & “Classdrv2” from base class as shown below. Now I am not sure how to use class “Classdrv2” in “Classdrv1”. For this implementation there should not be any code change in base class. What is the best approach for the same? Class ClassBase { } class Classdrv1 : public ClassBase { // need to use classdrv2 here, how? } class Classdrv2 : public ClassBase { } Thanks, Nandu

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      You may do, for instance:

      class Base
      {
      //...
      };
      class Der2;
      class Der1: public Base
      {
      Der2 * pd2;
      //...
      };
      class Der2: public Base
      {
      //..
      };

      :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      N 1 Reply Last reply
      0
      • CPalliniC CPallini

        You may do, for instance:

        class Base
        {
        //...
        };
        class Der2;
        class Der1: public Base
        {
        Der2 * pd2;
        //...
        };
        class Der2: public Base
        {
        //..
        };

        :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        N Offline
        N Offline
        Nandu_77b
        wrote on last edited by
        #3

        Thanks Pallini, This is one approach, other than this do we have another approach available. Regards, Nandu

        1 Reply Last reply
        0
        • N Nandu_77b

          I have a base class say “ClassBase” and I have derived two classes “Classdrv1” & “Classdrv2” from base class as shown below. Now I am not sure how to use class “Classdrv2” in “Classdrv1”. For this implementation there should not be any code change in base class. What is the best approach for the same? Class ClassBase { } class Classdrv1 : public ClassBase { // need to use classdrv2 here, how? } class Classdrv2 : public ClassBase { } Thanks, Nandu

          A Offline
          A Offline
          Avi Berger
          wrote on last edited by
          #4

          Just how do you plan to use it? You do realize that to use the class, unless you are talking about a static (class) function, you need an object of the class. So how are you getting this object & how long are you keeping it for? How does it relate to the rest of the program? An object of Classdrv2 of its own for the entire lifetime of a Classdrv1 object? An object of Classdrv2 just for use during the execution of a member function of Classdrv1? An object of Classdrv2 provided by a caller for manipulation by a member function of Classdrv1? There are lots of possibilities. Which is right for your situation depends on what you are trying to achieve. If you explained more, you might get more focused answers.

          Please do not read this signature.

          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