How to initialize const of base class
-
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:
-
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:
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"
-
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"
The constructor of B must have the described signature:confused: (specific for the application)
-
The constructor of B must have the described signature:confused: (specific for the application)
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"
-
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:
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
-
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
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!!!
-
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!!!
-
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:
You can't use the assigment operator to intialize
My_some_const
becauseMy_some_const
is aconst
member of the class and cannot appear on the left side of an assignment statement. The only way to initializeMy_some_const
is via constructor member initialization list. That initializesMy_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) } -
You can't use the assigment operator to intialize
My_some_const
becauseMy_some_const
is aconst
member of the class and cannot appear on the left side of an assignment statement. The only way to initializeMy_some_const
is via constructor member initialization list. That initializesMy_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) }Thanks all for your help