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. How to initialize const of base class

How to initialize const of base class

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
9 Posts 5 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.
  • B Offline
    B Offline
    Boniolopez
    wrote on last edited by
    #1

    Hi, May be very simple question, but I can't find the answer. Please help me. class A{ public: const int My_some_const; int k; A(int k_):k(k){}; } class B:public A{ B(int k):A(k) //<---How to initialize Mysome_const without passing the value througth constructor of A???? } Thanks in advance, Boni :confused:

    R A A 3 Replies Last reply
    0
    • B Boniolopez

      Hi, May be very simple question, but I can't find the answer. Please help me. class A{ public: const int My_some_const; int k; A(int k_):k(k){}; } class B:public A{ B(int k):A(k) //<---How to initialize Mysome_const without passing the value througth constructor of A???? } Thanks in advance, Boni :confused:

      R Offline
      R Offline
      Ryan Binns
      wrote on last edited by
      #2

      Boniolopez wrote: How to initialize Mysome_const without passing the value througth constructor of A???? Why? What's wrong with using the constructor?

      Ryan

      "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

      B 1 Reply Last reply
      0
      • R Ryan Binns

        Boniolopez wrote: How to initialize Mysome_const without passing the value througth constructor of A???? Why? What's wrong with using the constructor?

        Ryan

        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

        B Offline
        B Offline
        Boniolopez
        wrote on last edited by
        #3

        The constructor of B must have the described signature:confused: (specific for the application)

        R 1 Reply Last reply
        0
        • B Boniolopez

          The constructor of B must have the described signature:confused: (specific for the application)

          R Offline
          R Offline
          Ryan Binns
          wrote on last edited by
          #4

          Boniolopez wrote: The constructor of B must have the described signature Sorry, I still don't understand what the problem is. Are you saying you're not allowed to explicity call A's constructor?

          Ryan

          "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

          1 Reply Last reply
          0
          • B Boniolopez

            Hi, May be very simple question, but I can't find the answer. Please help me. class A{ public: const int My_some_const; int k; A(int k_):k(k){}; } class B:public A{ B(int k):A(k) //<---How to initialize Mysome_const without passing the value througth constructor of A???? } Thanks in advance, Boni :confused:

            A Offline
            A Offline
            Alan Chambers
            wrote on last edited by
            #5

            class A { public: const int My_some_const; int k; A(int k_) : k (k) {}; } class B : public A { B(int k) : k(k), My_some_const(1) { }; } Is that what your looking for? That initialises both member variables without going through the base constructor "When I left you I was but the learner, now I am the master" - Darth Vader

            B 1 Reply Last reply
            0
            • A Alan Chambers

              class A { public: const int My_some_const; int k; A(int k_) : k (k) {}; } class B : public A { B(int k) : k(k), My_some_const(1) { }; } Is that what your looking for? That initialises both member variables without going through the base constructor "When I left you I was but the learner, now I am the master" - Darth Vader

              B Offline
              B Offline
              Boniolopez
              wrote on last edited by
              #6

              Hi Alan, I want to do exactly das wat you wrote. Unfortunetly your solution (was my solution too:) is not compilable. It produces error C2758. The same stuff with tamplate produces error C2614. Any help is very appreciated!!!

              2 1 Reply Last reply
              0
              • B Boniolopez

                Hi Alan, I want to do exactly das wat you wrote. Unfortunetly your solution (was my solution too:) is not compilable. It produces error C2758. The same stuff with tamplate produces error C2614. Any help is very appreciated!!!

                2 Offline
                2 Offline
                224917
                wrote on last edited by
                #7

                When looking form base class, it dosen't know someone is going to derive from it and its const will be initilized there.
                There is no spoon. mail

                1 Reply Last reply
                0
                • B Boniolopez

                  Hi, May be very simple question, but I can't find the answer. Please help me. class A{ public: const int My_some_const; int k; A(int k_):k(k){}; } class B:public A{ B(int k):A(k) //<---How to initialize Mysome_const without passing the value througth constructor of A???? } Thanks in advance, Boni :confused:

                  A Offline
                  A Offline
                  Andrzej Markowski
                  wrote on last edited by
                  #8

                  You can't use the assigment operator to intialize My_some_const because My_some_const is a const member of the class and cannot appear on the left side of an assignment statement. The only way to initialize My_some_const is via constructor member initialization list. That initializes My_some_const to 10: class A{ public: const int My_some_const; int k; A(int k_):k(k),My_some_const(10){}; } class B:public A{ B(int k):A(k) }

                  B 1 Reply Last reply
                  0
                  • A Andrzej Markowski

                    You can't use the assigment operator to intialize My_some_const because My_some_const is a const member of the class and cannot appear on the left side of an assignment statement. The only way to initialize My_some_const is via constructor member initialization list. That initializes My_some_const to 10: class A{ public: const int My_some_const; int k; A(int k_):k(k),My_some_const(10){}; } class B:public A{ B(int k):A(k) }

                    B Offline
                    B Offline
                    Boniolopez
                    wrote on last edited by
                    #9

                    Thanks all for your help

                    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