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. the : operator

the : operator

Scheduled Pinned Locked Moved C / C++ / MFC
help
6 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.
  • W Offline
    W Offline
    Waldermort
    wrote on last edited by
    #1

    Could somebody explain how this works and what it does. I know it can be used to declare a base class, but I have now come across it in another location and I can't work out what it does. class Error { public: Error(const int nErrCode, char* szErrMess); Error(const Error& e); private: int mnErrCode; char* mpszErrMess; }; Error::Error(const int nErrCode,char* szErrMess) : mnErrCode(nErrCode) { } Error::Error(const Error& e) : mnErrCode(e.mnErrCode) { }

    B 1 Reply Last reply
    0
    • W Waldermort

      Could somebody explain how this works and what it does. I know it can be used to declare a base class, but I have now come across it in another location and I can't work out what it does. class Error { public: Error(const int nErrCode, char* szErrMess); Error(const Error& e); private: int mnErrCode; char* mpszErrMess; }; Error::Error(const int nErrCode,char* szErrMess) : mnErrCode(nErrCode) { } Error::Error(const Error& e) : mnErrCode(e.mnErrCode) { }

      B Offline
      B Offline
      BadKarma
      wrote on last edited by
      #2

      Hi, I believe you mean the two following occurances

      waldermort wrote:

      Error::Error(const int nErrCode,char* szErrMess) : mnErrCode(nErrCode) ... Error::Error(const Error& e) : mnErrCode(e.mnErrCode)

      When using the colon ':' after a constructor you open an initialisation list. This means that member like mnErrCode are initialised with the given error code even before you enter the scope of the constructor.

      codito ergo sum

      W 1 Reply Last reply
      0
      • B BadKarma

        Hi, I believe you mean the two following occurances

        waldermort wrote:

        Error::Error(const int nErrCode,char* szErrMess) : mnErrCode(nErrCode) ... Error::Error(const Error& e) : mnErrCode(e.mnErrCode)

        When using the colon ':' after a constructor you open an initialisation list. This means that member like mnErrCode are initialised with the given error code even before you enter the scope of the constructor.

        codito ergo sum

        W Offline
        W Offline
        Waldermort
        wrote on last edited by
        #3

        So it's basically the same as placing mnErrCode = nErrCode; inside the c'tor? I have to ask, what is the point of doing this?

        J B 2 Replies Last reply
        0
        • W Waldermort

          So it's basically the same as placing mnErrCode = nErrCode; inside the c'tor? I have to ask, what is the point of doing this?

          J Offline
          J Offline
          Justin Tay
          wrote on last edited by
          #4

          In this case, probably just convenience. But lets say you have a member that is a class object that doesn't have a default constructor.

          1 Reply Last reply
          0
          • W Waldermort

            So it's basically the same as placing mnErrCode = nErrCode; inside the c'tor? I have to ask, what is the point of doing this?

            B Offline
            B Offline
            BadKarma
            wrote on last edited by
            #5

            supose the following situation

            class A
            {
            public:
            A(int i)
            {
            _i = i;
            }
            
            int _i;
            };
            
            // class a has no default constructor
            
            class B
            {
            public:
            B() {}
            
            A _a;
            };
            
            // the Class B won't compile because there is no default constructor for _a
            // solution the initialisation list
            
            class C
            {
            public:
            C() : _a(1) {}
            
            A _a;
            };
            

            Now when maken an object of the type C the default constructor (of C) will initialize _a and calling the correct constructor of the member _a You might want to check the following http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6[^]

            codito ergo sum

            W 1 Reply Last reply
            0
            • B BadKarma

              supose the following situation

              class A
              {
              public:
              A(int i)
              {
              _i = i;
              }
              
              int _i;
              };
              
              // class a has no default constructor
              
              class B
              {
              public:
              B() {}
              
              A _a;
              };
              
              // the Class B won't compile because there is no default constructor for _a
              // solution the initialisation list
              
              class C
              {
              public:
              C() : _a(1) {}
              
              A _a;
              };
              

              Now when maken an object of the type C the default constructor (of C) will initialize _a and calling the correct constructor of the member _a You might want to check the following http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6[^]

              codito ergo sum

              W Offline
              W Offline
              Waldermort
              wrote on last edited by
              #6

              Ahhh, I understand perfectly now. Thankyou for the explanation. Infact I now remember seeing and using this before, when using STL containers within a class, I remember first having to initialize them like this before being able to use them. -- modified at 20:20 Friday 25th August, 2006

              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