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. Regarding Reference variable

Regarding Reference variable

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 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
    kuttiam
    wrote on last edited by
    #1

    Hi all, I have a reference variable in my class. I need to initialize it in the constructor. But, the compiler is giving an error. Can any one help me out from this problem?

    CPalliniC J N 3 Replies Last reply
    0
    • K kuttiam

      Hi all, I have a reference variable in my class. I need to initialize it in the constructor. But, the compiler is giving an error. Can any one help me out from this problem?

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

      Reference member variables must be initialized in the constructor's initialization list, for instance

      class A
      {
      int & iRefNum;

      public:
      A(int & i): iRefNum(i)
      {
      // whatever
      }
      };

      :)

      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

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • K kuttiam

        Hi all, I have a reference variable in my class. I need to initialize it in the constructor. But, the compiler is giving an error. Can any one help me out from this problem?

        J Offline
        J Offline
        Jijo Raj
        wrote on last edited by
        #3

        You've to initialize it in constructor initializer list. For e.g.

        MyClass::MyClass( CString& myString )
        : m_StringReference( myString )
        {
        // Assume m_StringReference is a reference declared in class.
        }

        regards, Jijo.

        _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

        1 Reply Last reply
        0
        • K kuttiam

          Hi all, I have a reference variable in my class. I need to initialize it in the constructor. But, the compiler is giving an error. Can any one help me out from this problem?

          N Offline
          N Offline
          Nibu babu thomas
          wrote on last edited by
          #4

          kuttiam wrote:

          I need to initialize it in the constructor. But, the compiler is giving an error.

          You should initialize a reference variable in the constructor initialization list e.g.

          class MyClass
          {
          public:
          MyClass( int& RefVal ) : m_RefVal( RefVal ) // Ok
          {
          m_RefVal = RefVal;// Error, cannot do this here
          }

          private:
          int& m_RefVal;
          };

          Since a reference variable is much like a constant var it must be initialized like a const var. i.e. initialize where it's declared and in classes we cannot initialize where it's declared hence initialization list is provided.

          Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

          modified on Wednesday, May 28, 2008 5:10 AM

          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