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. Other Discussions
  3. IT & Infrastructure
  4. Initializing a const member in a class

Initializing a const member in a class

Scheduled Pinned Locked Moved IT & Infrastructure
helpc++json
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.
  • J Offline
    J Offline
    jpyp
    wrote on last edited by
    #1

    I have a problem initializing a class member that is defined as a constant. My code lookes like this: in header file: class CMyClass { public: CMyClass(); /* constructor */ struct MyStruct { int field1; const int field2; } stMyStruct; int iVar; ... } in cpp file: ... CMyClass::CMyClass(): stMyStruct.field2(10) { stMyStruct.field1 = 2; iVar = 10; } ... It gives me a parsing error before "." when I compile. It doesn't seem to recognize stMyStruct. But if I substitute stMyStruct.field2(10) with iVar(10), it compiles fine. jpyp

    D 1 Reply Last reply
    0
    • J jpyp

      I have a problem initializing a class member that is defined as a constant. My code lookes like this: in header file: class CMyClass { public: CMyClass(); /* constructor */ struct MyStruct { int field1; const int field2; } stMyStruct; int iVar; ... } in cpp file: ... CMyClass::CMyClass(): stMyStruct.field2(10) { stMyStruct.field1 = 2; iVar = 10; } ... It gives me a parsing error before "." when I compile. It doesn't seem to recognize stMyStruct. But if I substitute stMyStruct.field2(10) with iVar(10), it compiles fine. jpyp

      D Offline
      D Offline
      Dan McCormick
      wrote on last edited by
      #2

      CMyClass cannot directly initialize a member of a contained class. ( remember structs are a special form of class ) You will need to provide a constructor for the contained class MyStruct that at the very minimum initializes the const member and then have CMyClass constructor use it. Try this...

      class CMyClass
      {
      public:
      CMyClass(); /* constructor */

      struct MyStruct
      {
      MyStruct( const int f2 ) :
      field2( f2 )
      {
      }

        int field1;
        const int field2;
      

      } stMyStruct;

      int iVar;
      };

      CMyClass::CMyClass():
      stMyStruct(10)
      {
      stMyStruct.field1 = 2;
      iVar = 10;
      }

      Dan

      Be clear about the difference between your role as a programmer and as a tester. The tester in you must be suspicious, uncompromising, hostile, and compulsively obsessed with destroying, utterly destroying, the programmer's software. ----- Boris Beizer

      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