D
desktop
In this code I am trying to make a const instance of class B in main.
But I get the error:
test.c++: In function ‘int main()’:
test.c++:34: error: uninitialized const ‘b’
Why should it be initialized? And how do I initialize an object?
#include <iostream>
class A {
public:
void foo() {
printf("non-const method\n");
}
void foo() const {
printf("const method\n");
}
};
class B {
public:
A a;
void callfoo() {
a.foo();
}
void callfoo() const {
a.foo();
}
};
int main() {
const B b;
return 0;
}
But I get the error:
test.c++: In function ‘int main()’:
test.c++:34: error: uninitialized const ‘b’
Why should it be initialized? And how do I initialize an object?
#include <iostream>
class A {
public:
void foo() {
printf("non-const method\n");
}
void foo() const {
printf("const method\n");
}
};
class B {
public:
A a;
void callfoo() {
a.foo();
}
void callfoo() const {
a.foo();
}
};
int main() {
const B b;
return 0;
}