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. Instantiating classes within classes

Instantiating classes within classes

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
6 Posts 4 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.
  • K Offline
    K Offline
    kialmur
    wrote on last edited by
    #1

    I am trying to figure out how to instantiate a class that contains another class. For example, if I have a class called TopClass that contains a class called InClass : class TopClass{ TopClass(); TopClass(int xIn); InClass x; } class InClass{ InClass(); InClass(int xIn); int xIn; } Even when I use non-default constructor in TopClass and called the equivalent one in InClass, it seems that the default construct in InClass has already been called to create a vanilla object. Is there any way I can automatically transfer the input variables to InClass when the non-default constructor is created? Or should I simply use pointers and initialize the pointer to a new InClass object the non-default constructor is called in TopClass? Thanks in advance for any info. Kiernan

    M O 2 Replies Last reply
    0
    • K kialmur

      I am trying to figure out how to instantiate a class that contains another class. For example, if I have a class called TopClass that contains a class called InClass : class TopClass{ TopClass(); TopClass(int xIn); InClass x; } class InClass{ InClass(); InClass(int xIn); int xIn; } Even when I use non-default constructor in TopClass and called the equivalent one in InClass, it seems that the default construct in InClass has already been called to create a vanilla object. Is there any way I can automatically transfer the input variables to InClass when the non-default constructor is created? Or should I simply use pointers and initialize the pointer to a new InClass object the non-default constructor is called in TopClass? Thanks in advance for any info. Kiernan

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      I'm not certain I understand correctly but will this not work ?

      /// this should for the appropriate constructor of X to be called.
      TopClass::TopClass( int xIn ) : x( xIn )
      {
      }


      Maximilien Lincourt Your Head A Splode - Strong Bad

      1 Reply Last reply
      0
      • K kialmur

        I am trying to figure out how to instantiate a class that contains another class. For example, if I have a class called TopClass that contains a class called InClass : class TopClass{ TopClass(); TopClass(int xIn); InClass x; } class InClass{ InClass(); InClass(int xIn); int xIn; } Even when I use non-default constructor in TopClass and called the equivalent one in InClass, it seems that the default construct in InClass has already been called to create a vanilla object. Is there any way I can automatically transfer the input variables to InClass when the non-default constructor is created? Or should I simply use pointers and initialize the pointer to a new InClass object the non-default constructor is called in TopClass? Thanks in advance for any info. Kiernan

        O Offline
        O Offline
        oshah
        wrote on last edited by
        #3

        <naughty>I see you skipped the chapter on initialisation lists when you were learning C++</naughty>. Well to be honest, it IS an obscure topic and quite easy to miss when you're learning C++. So I'll let you off... THIS time. Anyway, the answer is to use an initialisation list.

        class TopClass()
        {
        TopClass(int xIn)
        : x(xIn)
        /** The above line tells the compiler to initialise
        x to xIn, using the constructor that takes an int.
        If you don't provide an initialisation list, the
        default constructor is used **/
        {
        }
        InClass x;
        };

        -- modified at 10:24 Monday 20th March, 2006

        K 1 Reply Last reply
        0
        • O oshah

          <naughty>I see you skipped the chapter on initialisation lists when you were learning C++</naughty>. Well to be honest, it IS an obscure topic and quite easy to miss when you're learning C++. So I'll let you off... THIS time. Anyway, the answer is to use an initialisation list.

          class TopClass()
          {
          TopClass(int xIn)
          : x(xIn)
          /** The above line tells the compiler to initialise
          x to xIn, using the constructor that takes an int.
          If you don't provide an initialisation list, the
          default constructor is used **/
          {
          }
          InClass x;
          };

          -- modified at 10:24 Monday 20th March, 2006

          K Offline
          K Offline
          kialmur
          wrote on last edited by
          #4

          Thank you for the help. I guess I did skip that chapter. I am learning C++ after having learned Java, so this is a subtlety that escaped my attention since this problem doesn't really occure in Java due to its lack of definitions. You answer does however bring me to another question. Isn't that the same syntax used used for passing variables to an inherited version of a function? Did I miss the big picture, whereby that syntax basically allows me to pass values to any other valid function? Regards Kiernan

          T O 2 Replies Last reply
          0
          • K kialmur

            Thank you for the help. I guess I did skip that chapter. I am learning C++ after having learned Java, so this is a subtlety that escaped my attention since this problem doesn't really occure in Java due to its lack of definitions. You answer does however bring me to another question. Isn't that the same syntax used used for passing variables to an inherited version of a function? Did I miss the big picture, whereby that syntax basically allows me to pass values to any other valid function? Regards Kiernan

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            using constructor initialisation list in C++ is a bit like calling super() for the base class. but in C++, as multiple inheritence exists, you have to create each base classes then...

            1 Reply Last reply
            0
            • K kialmur

              Thank you for the help. I guess I did skip that chapter. I am learning C++ after having learned Java, so this is a subtlety that escaped my attention since this problem doesn't really occure in Java due to its lack of definitions. You answer does however bring me to another question. Isn't that the same syntax used used for passing variables to an inherited version of a function? Did I miss the big picture, whereby that syntax basically allows me to pass values to any other valid function? Regards Kiernan

              O Offline
              O Offline
              oshah
              wrote on last edited by
              #6

              That syntax is only valid (AFAICT) in a constructor. The syntax is used to initialise any base classes, and any members. By the time you enter the function, all external parameters need to have been initialized (base classes, members, and global variables). Although we already have syntax to initialise global variables, this is the only syntax available to initialise members and base classes.

              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