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. A c++ question

A c++ question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
2 Posts 2 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.
  • S Offline
    S Offline
    saad shah
    wrote on last edited by
    #1

    I am declaring a class in a header file something like: /////////////// headerfile A.h //////////// #include "B.h" class A { private: B val; // Blah } As can be seen above, i've declared a class 'A' in its header file. I've created an object of class 'B' as a private member of class 'A'. Class 'B' also got a 'zero argument constructor'. As such will it be get called ??? How can i make sure that 'zero argument constructor' of class B, must be get called before i use it in any method of class A ???

    C 1 Reply Last reply
    0
    • S saad shah

      I am declaring a class in a header file something like: /////////////// headerfile A.h //////////// #include "B.h" class A { private: B val; // Blah } As can be seen above, i've declared a class 'A' in its header file. I've created an object of class 'B' as a private member of class 'A'. Class 'B' also got a 'zero argument constructor'. As such will it be get called ??? How can i make sure that 'zero argument constructor' of class B, must be get called before i use it in any method of class A ???

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      when an object of type A is created, the constructors of all member variables will be called before A's constructor is called. try it.

      class B
      {
      public:
      B()
      {
      printf("B\n");
      }
      };

      class A
      {
      public:
      A()
      {
      printf("A\n");
      }

      B b;
      };

      main()
      {
      A a;
      }

      the output is : B A -c


      A | B - it's not a choice.

      ThumbNailer

      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